From dc3bcb5014a5eb0872857914ee3ea3f2de8fd24c Mon Sep 17 00:00:00 2001 From: ache Date: Fri, 2 Sep 2022 23:38:36 +0200 Subject: Add tags support --- src/build/index.mjs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) (limited to 'src/build/index.mjs') diff --git a/src/build/index.mjs b/src/build/index.mjs index c7e1cf7..74a7113 100644 --- a/src/build/index.mjs +++ b/src/build/index.mjs @@ -12,6 +12,28 @@ import listArticles from './list-articles.mjs'; import getRSS from './rss.mjs'; import toml from '@ltd/j-toml'; +function getArticleYear(article) { + if(article.metaData.pubDate.getFullYear) { + return article.metaData.pubDate.getFullYear(); + } else if(article.metaData.pubDate.getUTCFullYear) { + return article.metaData.pubDate.getUTCFullYear(); + } + + return 0; +} +function getArticleDate(article) { + if(article.metaData.pubDate.getDate) { + return article.metaData.pubDate.getFullYear() * 100 + article.metaData.pubDate.getDate(); + } else if(article.metaData.pubDate.getUTCDate) { + return article.metaData.pubDate.getUTCFullYear() * 100 + article.metaData.pubDate.getDate(); + } + + return 0; +} +function cmpArticles(a, b) { + return getArticleDate(b) - getArticleDate(a); +} + const loadMD = (listFile, suffix) => { const listContent = []; for (const file of listFile) { @@ -41,6 +63,7 @@ const loadMD = (listFile, suffix) => { const readMore = h('a', 'Lire plus...'); readMore.properties.href = `${suffix}/${file.slice(0, -3)}`; + const pubYear = getArticleYear({metaData}); listContent.push({ name: file.slice(0, -3), @@ -49,8 +72,10 @@ const loadMD = (listFile, suffix) => { introDesc: hastToString(intro), imageUrl: logo.properties.src, metaData, + pubYear, title, domTitle, + url: baseUrl + domTitle, }); } @@ -61,6 +86,7 @@ const leftPanelTmpl = fs.readFileSync('src/templates/left.tmpl', 'utf8'); const headerTmpl = fs.readFileSync('src/templates/header.tmpl', 'utf8'); const articleTmpl = fs.readFileSync('src/templates/article.tmpl', 'utf8'); const indexTmpl = fs.readFileSync('src/templates/index.tmpl', 'utf8'); +const tagTmpl = fs.readFileSync('src/templates/tag.tmpl', 'utf8'); const baseUrl = 'https://ache.one/'; const partials = { @@ -72,21 +98,54 @@ const svg = loadSVG(); const articles = loadMD(listArticles, 'articles'); +const tagsArticle = new Map(); + for (const article of articles) { const context = { svg, title: `${article.title} - ache`, - canonical: baseUrl + article.domTitle, + canonical: article.url, content: article.content, domTitle: article.domTitle, + metaData: article.metaData, }; const output = mustache.render(articleTmpl, context, partials); console.log(`Create : ${article.title}`); fs.writeFileSync(`articles/${article.name}.html`, output); + + for (const tag of article.metaData.tags) { + if (tagsArticle.has(tag)) { + tagsArticle.get(tag).push(article); + } else { + tagsArticle.set(tag, [article]); + } + } +} + +try { + fs.mkdirSync('tag'); +} catch { + fs.rmSync('tag', {force: true, recursive: true}); + fs.mkdirSync('tag'); +} + +for (const [tag, articles] of tagsArticle.entries()) { + console.log(`Create tag page : ${tag}.xml`); + articles.sort(cmpArticles); + + const context = { + svg, + title: `ache - Tag: ${tag}`, + tag, + articles, + }; + + const output = mustache.render(tagTmpl, context, partials); + fs.writeFileSync(`tag/${tag}.html`, output); } -console.log('Create : rss.xml'); +console.log('Create RSS Flux: rss.xml'); const xmlFeed = getRSS(articles, baseUrl); fs.writeFileSync('rss.xml', xmlFeed); -- cgit v1.2.3