2021-08-06 14:46:16 +02:00
|
|
|
import fs from 'fs'
|
|
|
|
import njk from 'nunjucks'
|
2021-08-07 10:46:48 +02:00
|
|
|
import { generateContext } from './generateContext.js'
|
2021-08-06 14:46:16 +02:00
|
|
|
|
|
|
|
// Load in config
|
|
|
|
const ConfigFile = fs.readFileSync('./config.json')
|
|
|
|
const Config = JSON.parse(ConfigFile)
|
|
|
|
|
|
|
|
export function njkRenderer(path) {
|
2021-08-07 10:46:48 +02:00
|
|
|
// Read in the template
|
2021-08-06 14:46:16 +02:00
|
|
|
const njkFile = fs.readFileSync(path).toString()
|
2021-08-07 10:46:48 +02:00
|
|
|
// Pre-render it for analysis while rendering context
|
|
|
|
const prerenderedNjk = njk.renderString(njkFile)
|
2021-08-06 14:46:16 +02:00
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
// Generate proper context and make a final render of the template.
|
|
|
|
const context = generateContext(prerenderedNjk)
|
2021-08-06 14:46:16 +02:00
|
|
|
return njk.renderString(njkFile, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mdRenderer(path) {
|
|
|
|
// Load in the njk template and markdown
|
|
|
|
const njkFile = fs.readFileSync(`${Config.contentDir}/templates/external_md.njk`).toString()
|
|
|
|
const externalMarkdownFile = fs.readFileSync(path).toString()
|
|
|
|
|
|
|
|
// Separate the actual markdown from potential 'externalContext'
|
2021-08-07 10:16:13 +02:00
|
|
|
const externalMarkdown = externalMarkdownFile.replace(/^%%-\n[\S\s]*-%%\n/, "").replace(/```\w*[\s\S]*?```/g, match => match.replace(/ /g, "•"))
|
2021-08-08 19:00:30 +02:00
|
|
|
const externalContext = externalMarkdownFile.match(/^%%-\n[\S\s]*-%%\n/)
|
2021-08-06 14:46:16 +02:00
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
// Pre-render the template for analysis during context generation.
|
|
|
|
const prerenderedNjk = njk.renderString(njkFile, { externalMarkdown })
|
2021-08-06 14:46:16 +02:00
|
|
|
|
2021-08-07 10:46:48 +02:00
|
|
|
// Generate the context and add the externalMarkdown to the context, then render the template.
|
|
|
|
let context = generateContext(prerenderedNjk, externalContext)
|
2021-08-06 14:46:16 +02:00
|
|
|
context.externalMarkdown = externalMarkdown
|
|
|
|
return njk.renderString(njkFile, context)
|
|
|
|
}
|