import fs from 'fs' import njk from 'nunjucks' import { JSDOM } from 'jsdom' // Load in config const ConfigFile = fs.readFileSync('./config.json') const Config = JSON.parse(ConfigFile) function generateToc(dom) { const tocAnchors = dom.window.document.querySelectorAll(".toc-anchor") let tocLinks = [] // This basically creates a proper link for the ToC. :))) tocAnchors.forEach(anchor => tocLinks.push(`${anchor.getAttribute('data-orig-text')}`)) return tocLinks } function generateContext(renderedNjk) { const dom = new JSDOM(renderedNjk) return { serverName: Config.serverName, tocLinks: generateToc(dom) } } export function njkRenderer(path) { const njkFile = fs.readFileSync(path).toString() const renderedNjk = njk.renderString(njkFile) const context = generateContext(renderedNjk) return njk.renderString(njkFile, context) }