diff --git a/build.js b/build.js index c45db23..cd85d79 100644 --- a/build.js +++ b/build.js @@ -1,68 +1,92 @@ -import{readFileSync,writeFileSync,mkdirSync,cpSync,readdirSync,existsSync,statSync}from'fs' -import{join,extname}from'path' -import matter from'gray-matter' -import{marked}from'marked' +import { readFileSync, writeFileSync, mkdirSync, cpSync, readdirSync, existsSync, statSync } from 'fs' +import { join } from 'path' +import matter from 'gray-matter' +import { marked } from 'marked' -const ARTICLES='articles',DIST='dist',TEMPLATES='templates',STATIC='static' +const ARTICLES = 'articles' +const DIST = 'dist' +const TEMPLATES = 'templates' +const STATIC = 'static' -const template=n=>readFileSync(join(TEMPLATES,n),'utf-8') -const articleTmpl=template('article.html') -const homeTmpl=template('home.html') +const template = n => readFileSync(join(TEMPLATES, n), 'utf-8') +const articleTmpl = template('article.html') +const homeTmpl = template('home.html') -mkdirSync(DIST,{recursive:true}) +const render = (tpl, data) => + tpl.replace(/{{\s*([a-zA-Z0-9_]+)\s*}}/g, (_, key) => `${data[key] ?? ''}`) -if(existsSync(STATIC)) - cpSync(STATIC,DIST,{recursive:true}) +const fmtDateLong = d => + d ? new Date(d).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) : '' -const articles=[] +const fmtDateIso = d => (d ? new Date(d).toISOString().split('T')[0] : '') -for(const slug of readdirSync(ARTICLES)){ - const dir=join(ARTICLES,slug) - if(!statSync(dir).isDirectory()) continue +mkdirSync(DIST, { recursive: true }) - const mdPath=join(dir,'index.md') - if(!existsSync(mdPath)) continue +if (existsSync(STATIC)) cpSync(STATIC, DIST, { recursive: true }) - const{data,content}=matter(readFileSync(mdPath,'utf-8')) - const html=marked(content) +const articles = [] - const outDir=join(DIST,slug) - mkdirSync(outDir,{recursive:true}) +for (const slug of readdirSync(ARTICLES)) { + const dir = join(ARTICLES, slug) + if (!statSync(dir).isDirectory()) continue - // copy co-located assets - for(const f of readdirSync(dir)){ - if(f==='index.md') continue - cpSync(join(dir,f),join(outDir,f)) + const mdPath = join(dir, 'index.md') + if (!existsSync(mdPath)) continue + + const { data, content } = matter(readFileSync(mdPath, 'utf-8')) + const html = marked(content) + + const outDir = join(DIST, slug) + mkdirSync(outDir, { recursive: true }) + + for (const f of readdirSync(dir)) { + if (f === 'index.md') continue + cpSync(join(dir, f), join(outDir, f), { recursive: true }) } - const page=articleTmpl - .replace('{{title}}',data.title||slug) - .replace('{{date}}',data.date?new Date(data.date).toLocaleDateString('en-US',{month:'long',day:'numeric',year:'numeric'}):'') - .replace('{{date_iso}}',data.date?new Date(data.date).toISOString().split('T')[0]:'') - .replace('{{tags}}', (data.tags||[]).map(t=>`${t}`).join(' ')) - .replace('{{excerpt}}',data.excerpt||'') - .replace('{{slug}}',slug) - .replace('{{body}}',html) + const page = render(articleTmpl, { + title: data.title || slug, + date: fmtDateLong(data.date), + date_iso: fmtDateIso(data.date), + tags: (data.tags || []) + .map( + t => + `${t}` + ) + .join(' '), + excerpt: data.excerpt || '', + slug, + body: html + }) - writeFileSync(join(outDir,'index.html'),page) - articles.push({slug,...data}) + writeFileSync(join(outDir, 'index.html'), page) + articles.push({ slug, ...data }) console.log(`ā ${slug}`) } -articles.sort((a,b)=>new Date(b.date)-new Date(a.date)) +articles.sort((a, b) => new Date(b.date) - new Date(a.date)) -const articleListHtml=articles.map(a=>` +const articleListHtml = articles + .map( + a => ` ${a.title} - ${new Date(a.date).toLocaleDateString('en-US',{month:'short',day:'numeric'})} + ${new Date(a.date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} - ${a.excerpt?`${a.excerpt}`:''} - ${(a.tags||[]).map(t=>`${t}`).join('')} + ${a.excerpt ? `${a.excerpt}` : ''} + ${(a.tags || []) + .map( + t => + `${t}` + ) + .join('')} - `).join('\n') + ` + ) + .join('\n') -const home=homeTmpl.replace('{{articles}}',articleListHtml) -writeFileSync(join(DIST,'index.html'),home) +const home = render(homeTmpl, { articles: articleListHtml }) +writeFileSync(join(DIST, 'index.html'), home) console.log(`\nš index.html ā ${articles.length} article(s)`)
${a.excerpt}