35 lines
988 B
JavaScript
35 lines
988 B
JavaScript
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(`<a href="#${anchor.name}" class="${anchor.classList[1].replace("-anchor", "")}">${anchor.getAttribute('data-orig-text')}</a>`))
|
|
|
|
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)
|
|
} |