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(`${anchor.getAttribute('data-orig-text')}`)) 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 } }