31 lines
1020 B
JavaScript
31 lines
1020 B
JavaScript
import fs from 'fs'
|
|
import { JSDOM } from 'jsdom'
|
|
import { parseExternalContext } from './externalContext.js'
|
|
|
|
const ConfigFile = fs.readFileSync('./config.json')
|
|
const Config = JSON.parse(ConfigFile)
|
|
|
|
function generateToc(dom) {
|
|
// Get all ToC anchors from the DOM
|
|
const tocAnchors = dom.window.document.querySelectorAll(".toc-anchor")
|
|
|
|
// Generate links to lead to those anvhors.
|
|
let tocLinks = []
|
|
tocAnchors.forEach(anchor => tocLinks.push(`<a href="#${anchor.id}" class="${anchor.classList[1].replace("-anchor", "")}">${anchor.getAttribute('data-orig-text')}</a>`))
|
|
|
|
return tocLinks
|
|
}
|
|
|
|
export function generateContext(prerenderedNjk, externalContext = undefined) {
|
|
// Create a 'virtual DOM' for analysis.
|
|
const dom = new JSDOM(prerenderedNjk)
|
|
|
|
// Generate respective parts of the context.
|
|
return {
|
|
serverName: Config.serverName,
|
|
tocLinks: generateToc(dom),
|
|
externalMeta: externalContext ? parseExternalContext(externalContext) : undefined
|
|
}
|
|
}
|
|
|