diff options
Diffstat (limited to 'src/build/index.mjs')
-rw-r--r-- | src/build/index.mjs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/build/index.mjs b/src/build/index.mjs new file mode 100644 index 0000000..84d4d9a --- /dev/null +++ b/src/build/index.mjs @@ -0,0 +1,83 @@ +import fs from 'node:fs'; +import mustache from 'mustache'; +import {u} from 'unist-builder'; +import {select} from 'hast-util-select'; +import {toString as hastToString} from 'mdast-util-to-string'; + +import {toHtmlRaw, toString} from './to-html.mjs'; +import loadSVG from './load-svg.mjs'; +import listArticles from './list-articles.mjs'; +import getRSS from './rss.mjs'; + +const loadMD = (listFile, suffix) => { + const listContent = []; + for (const file of listFile) { + const content = fs.readFileSync(`${suffix}/${file}`, 'utf8'); + const htmlContent = toHtmlRaw(content); + const htmlRender = toString(htmlContent); + + const titleHtml = select('h1', htmlContent); + const intro = select('p', htmlContent); + const logo = select('img', htmlContent); + logo.properties.src = `${suffix}/${logo.properties.src}`; + titleHtml.children[0].properties.href = `${suffix}/${file.slice(0, -3)}.html`; + + const title = hastToString(titleHtml); + const domTitle = title.replace(/\s+/g, '-').toLowerCase(); // Maybe encodeURI + console.log(`Create : ${title}`); + + listContent.push({ + name: file.slice(0, -3), + content: htmlRender, + intro: toString(u('root', [titleHtml, intro])), + introDesc: hastToString(intro), + imageUrl: logo.properties.src, + title, + domTitle, + }); + } + + return listContent; +}; + +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 partials = { + header: headerTmpl, + leftPanel: leftPanelTmpl, +}; + +const svg = loadSVG(); + +const articles = loadMD(listArticles, 'articles'); + +for (const article of articles) { + const context = { + svg, + title: `${article.title} - ache`, + content: article.content, + domTitle: article.domTitle, + }; + const output = mustache.render(articleTmpl, context, partials); + + fs.writeFileSync(`articles/${article.name}.html`, output); +} + +console.log('Create : rss.xml'); +const xmlFeed = getRSS(articles); +fs.writeFileSync('rss.xml', xmlFeed); + +{ + const context = { + title: 'ache: Blog personnel', + svg, + articles, + }; + + console.log('Create : Home page'); + const output = mustache.render(indexTmpl, context, partials); + fs.writeFileSync('index.html', output); +} |