2021-08-08 18:32:09 +02:00
|
|
|
import fs from 'fs'
|
2021-08-07 10:46:48 +02:00
|
|
|
import { JSDOM } from 'jsdom'
|
|
|
|
import { parseExternalContext } from './externalContext.js'
|
|
|
|
|
2021-08-08 18:32:09 +02:00
|
|
|
const ConfigFile = fs.readFileSync('./config.json')
|
|
|
|
const Config = JSON.parse(ConfigFile)
|
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
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.name}" 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|