Browse Source

Add routes and templates for tag pages

master
Perception 4 months ago
parent
commit
c015ae06df
4 changed files with 117 additions and 0 deletions
  1. +25
    -0
      routes/tag/:name.js
  2. +23
    -0
      routes/tag/index.js
  3. +38
    -0
      templates/tag-index.ejs
  4. +31
    -0
      templates/tag.ejs

+ 25
- 0
routes/tag/:name.js View File

@ -0,0 +1,25 @@
const { send, json } = require('micro')
const Database = require('better-sqlite3')
const pagination = require('pagination-utility').PaginationUtility
const qs = require('qs')
const ucfirst = require('ucfirst')
const page = require('../../page')
const ds = require('../../index')
const db = ds.db()
const PER_PAGE = 100
module.exports.GET = async (req, res) => {
const name = req.params.name.replace(/-/g, ' ')
const tag = name
const path = `/tag/${req.params.name}?`
const query = qs.parse(req.query)
const p = query.p ? parseInt(query.p) : 1
const {limit, offset} = pagination.getValuesToPaginate({ currentPage: p, perPage: PER_PAGE })
const articles = await ds.getArticlesByTag(db, name, limit, offset)
const count = await ds.countArticlesByTag(db, name)
const totalPages = pagination.getTotalPages({ totalItems: count, perPage: PER_PAGE })
const out = await page.render('tag', { tag, articles, path, p, count, totalPages })
send(res, 200, out)
}

+ 23
- 0
routes/tag/index.js View File

@ -0,0 +1,23 @@
const { send, json } = require('micro')
const Database = require('better-sqlite3')
const pagination = require('pagination-utility').PaginationUtility
const qs = require('qs')
const page = require('../../page')
const ds = require('../../index')
const db = ds.db()
const PER_PAGE = 300
module.exports.GET = async (req, res) => {
const path = `/tag?`
const query = qs.parse(req.query)
const p = query.p ? parseInt(query.p) : 1
const {limit, offset} = pagination.getValuesToPaginate({ currentPage: p, perPage: PER_PAGE })
const count = await ds.countTags(db)
const tags = await ds.getTagCloud(db, limit, offset)
const totalPages = pagination.getTotalPages({ totalItems: count, perPage: PER_PAGE })
const out = await page.render('tag-index', { tags, path, p, count, totalPages })
console.log({ path, p, count, totalPages })
send(res, 200, out)
}

+ 38
- 0
templates/tag-index.ejs View File

@ -0,0 +1,38 @@
<%- include('_header') -%>
<!-- Primary Page Layout
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<%- include('_search') %>
<%
// break a list into 3 even lists
function thirds(list) {
if (list.length <= 50) {
return [list, [], []]
}
const boundary = Math.round(list.length / 3)
a = boundary
b = boundary * 2
c = boundary * 3
return [list.slice(0, a), list.slice(a, b), list.slice(b, c)]
}
%>
<section id="tag-index" class="clean" style="margin-top: 5%" >
<div class="container">
<div class="row">
<% const parts = thirds(tags) %>
<% parts.forEach((ts) => { %>
<div class="four columns" >
<% ts.forEach((t) => { %>
<h6><a href="/tag/<%- t.name.replace(/\s+/g, '-') %>"><%- t.name %></a> (<span class="count"><%- t.c %></span>)</h6>
<% }) %>
</div>
<% }) %>
</div>
</div>
</section>
<%- include('_pagination') %>
<%- include('_footer') -%>

+ 31
- 0
templates/tag.ejs View File

@ -0,0 +1,31 @@
<%- include('_header') -%>
<!-- Primary Page Layout
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<%- include('_search') %>
<section id="search-results" style="margin-top: 5%" >
<div class="container">
<div class="row">
<div class="two columns" >
<h4><%- tag %></h4>
</div>
<div class="ten columns" >
<% let last = '' %>
<% articles.forEach((art) => { %>
<% if (last != art.month) { %>
<h4><%- art.month %></h4>
<% last = art.month %>
<% } %>
<div class="article-link">
<h6><a href="https://dailystormer.su<%- art.slug %>"><%- art.title %></a> by <%- art.author %></h6>
</div>
<% }); %>
</div>
</div>
</div>
</section>
<%- include('_pagination') %>
<%- include('_footer') -%>

Loading…
Cancel
Save