qwik-site/libs/generateContext.js

31 lines
1020 B
JavaScript
Raw Normal View History

2021-08-09 17:44:53 +02:00
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 = []
2022-08-13 17:16:25 +02:00
tocAnchors.forEach(anchor => tocLinks.push(`<a href="#${anchor.id}" class="${anchor.classList[1].replace("-anchor", "")}">${anchor.getAttribute('data-orig-text')}</a>`))
2021-08-09 17:44:53 +02:00
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
}
}