import fs from 'node:fs/promises' import path from 'node:path' import nunjucks from 'nunjucks' import HighlightJS from 'highlight.js' import readPage from '../read-page/index.js' import googleFonts from './util/google-fonts.js' import featherIcons from './util/feather-icons.js' const pages = { '/': './pages/index.tce', '/about/': './pages/about.tce', '/docs/javascript/': './pages/docs/javascript.tce', '/docs/c/': './pages/docs/c.tce' } async function render() { // await fs.rm('dist', { recursive: true }) for (const [urlPath, filePath] of Object.entries(pages)) { const env = nunjucks.configure('renderer/', { autoescape: false }) env.addFilter('googleFonts', async (val, cb) => { try { cb(null, await googleFonts(val)) } catch (e) { cb(e) } }, true) env.addGlobal('featherIcons', featherIcons) HighlightJS.registerLanguage('terrace', () => ({ name: 'Terrace', contains: [ { className: 'keyword', begin: /^\s*(.*?)(?:\s|$)/, relevance: 1 } ] })) env.addFilter("highlight", (value, language) => { return HighlightJS.highlight(value, { language }).value }) const page = await readPage(filePath) const result = await new Promise((resolve, reject) => { env.render('layout.njk', { page, url: urlPath, file: filePath }, (err, res) => { if (err) return reject(err) resolve(res) }) }) await fs.mkdir(path.join('dist', urlPath), { recursive: true }) await fs.writeFile(path.join('dist', urlPath, 'index.html'), result) } // await fs.cp('node_modules/highlight.js/styles/atom-one-dark.css', ''); await fs.cp('./public/', './dist/public/', { recursive: true }) await fs.cp('./favicon.ico', './dist/favicon.ico', { recursive: true }) } render()