From 3a1c038b78ad5bdc39829cf21c6a43c206b7a5ed Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Fri, 6 Aug 2021 14:46:16 +0200 Subject: [PATCH] Added support for plain markdown files. --- content/pages/docs/intro.njk | 4 -- content/pages/docs/test.md | 6 +++ content/templates/external_md.njk | 29 ++++++++++++ index.js | 4 +- libs/externalContext.js | 16 +++++++ libs/{mdRenderer.js => markedRenderer.js} | 2 +- libs/njkRenderer.js | 35 --------------- libs/requestHandler.js | 9 +++- libs/siteRenderer.js | 54 +++++++++++++++++++++++ 9 files changed, 116 insertions(+), 43 deletions(-) create mode 100644 content/pages/docs/test.md create mode 100644 content/templates/external_md.njk create mode 100644 libs/externalContext.js rename libs/{mdRenderer.js => markedRenderer.js} (88%) delete mode 100644 libs/njkRenderer.js create mode 100644 libs/siteRenderer.js diff --git a/content/pages/docs/intro.njk b/content/pages/docs/intro.njk index 7de61a4..ba78939 100644 --- a/content/pages/docs/intro.njk +++ b/content/pages/docs/intro.njk @@ -93,9 +93,5 @@ Default: `qwik` # Usage - Ow damn. I need to write this don't I? It will come later it's 00:30, and I am not feeling like writing - more now... But I could advise looking through the [nunjacks documentation](https://mozilla.github.io/nunjucks/) - and that could probably get you going a bit. Also, if you dare read the source code of the `content`-folder, - you could probably figure out how stuf works as well. {% endblock %} \ No newline at end of file diff --git a/content/pages/docs/test.md b/content/pages/docs/test.md new file mode 100644 index 0000000..d2e18b4 --- /dev/null +++ b/content/pages/docs/test.md @@ -0,0 +1,6 @@ +%%- +title: Hello World +header: Test Article +-%% + +# Hello wonderful world!! \ No newline at end of file diff --git a/content/templates/external_md.njk b/content/templates/external_md.njk new file mode 100644 index 0000000..67de38a --- /dev/null +++ b/content/templates/external_md.njk @@ -0,0 +1,29 @@ + + + + {% include "templates/defaultTags.njk" %} + + + {{ externalMeta.title }} + + +
+
+ {{ externalMeta.header }} +
+ +
+
+

Table of Contents

+ {% for tocLink in tocLinks %} + {{ tocLink | safe }} + {% endfor %} +
+
+ + {% markdown %} + {{ externalMarkdown }} + {% endmarkdown %} +
+ + \ No newline at end of file diff --git a/index.js b/index.js index a3ad0b4..ba015a6 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ import marked from 'marked' import hljs from 'highlight.js' import { requestHandler } from './libs/requestHandler.js' -import { mdRenderer } from './libs/mdRenderer.js' +import { markedRenderer } from './libs/markedRenderer.js' // Load in config @@ -44,7 +44,7 @@ marked.setOptions({ gfm: true }) -marked.use({ renderer: mdRenderer }) +marked.use({ renderer: markedRenderer }) njkMarkdown.register(njkEnv, marked) diff --git a/libs/externalContext.js b/libs/externalContext.js new file mode 100644 index 0000000..8e5d2dc --- /dev/null +++ b/libs/externalContext.js @@ -0,0 +1,16 @@ +export function parseExternalContext(externalContext) { + // Remove start and end tag + externalContext = externalContext.replace(/%%-\n|-%%\n/g, "") + + let parsedContext = {} + externalContext.split("\n").forEach(line => { + // If the line is falsey; leave. + if (!line) return + + // Assign properties to parsedContext and give them values. + line = line.split(/:/) + parsedContext[line[0]] = line[1].replace(/^\s/, "") + }) + + return parsedContext +} \ No newline at end of file diff --git a/libs/mdRenderer.js b/libs/markedRenderer.js similarity index 88% rename from libs/mdRenderer.js rename to libs/markedRenderer.js index 78daf3f..a12719f 100644 --- a/libs/mdRenderer.js +++ b/libs/markedRenderer.js @@ -1,4 +1,4 @@ -export const mdRenderer = { +export const markedRenderer = { heading(text, level) { return ` diff --git a/libs/njkRenderer.js b/libs/njkRenderer.js deleted file mode 100644 index 0b46880..0000000 --- a/libs/njkRenderer.js +++ /dev/null @@ -1,35 +0,0 @@ -import fs from 'fs' -import njk from 'nunjucks' -import { JSDOM } from 'jsdom' - -// Load in config -const ConfigFile = fs.readFileSync('./config.json') -const Config = JSON.parse(ConfigFile) - -function generateToc(dom) { - const tocAnchors = dom.window.document.querySelectorAll(".toc-anchor") - let tocLinks = [] - - // This basically creates a proper link for the ToC. :))) - tocAnchors.forEach(anchor => tocLinks.push(`${anchor.getAttribute('data-orig-text')}`)) - - return tocLinks -} - -function generateContext(renderedNjk) { - const dom = new JSDOM(renderedNjk) - - return { - serverName: Config.serverName, - tocLinks: generateToc(dom) - } -} - -export function njkRenderer(path) { - const njkFile = fs.readFileSync(path).toString() - const renderedNjk = njk.renderString(njkFile) - - const context = generateContext(renderedNjk) - - return njk.renderString(njkFile, context) -} \ No newline at end of file diff --git a/libs/requestHandler.js b/libs/requestHandler.js index cc899de..8dd7787 100644 --- a/libs/requestHandler.js +++ b/libs/requestHandler.js @@ -1,5 +1,5 @@ import fs from 'fs' -import { njkRenderer } from './njkRenderer.js' +import { mdRenderer, njkRenderer } from './siteRenderer.js' export function requestHandler(req, res, Config) { if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.njk`)) @@ -8,5 +8,12 @@ export function requestHandler(req, res, Config) { if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.njk`)) return res.send(njkRenderer(`./${Config.contentDir}/pages/${req.path}/index.njk`)) + if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.md`)) + return res.send(mdRenderer(`./${Config.contentDir}/pages/${req.path}.md`)) + + if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.md`)) + return res.send(mdRenderer(`./${Config.contentDir}/pages/${req.path}/index.md`)) + + return res.status(404).send(njkRenderer(`./${Config.contentDir}/errors/404.njk`)) } \ No newline at end of file diff --git a/libs/siteRenderer.js b/libs/siteRenderer.js new file mode 100644 index 0000000..5168c70 --- /dev/null +++ b/libs/siteRenderer.js @@ -0,0 +1,54 @@ +import fs from 'fs' +import njk from 'nunjucks' +import { JSDOM } from 'jsdom' +import { parseExternalContext } from './externalContext.js' + +// Load in config +const ConfigFile = fs.readFileSync('./config.json') +const Config = JSON.parse(ConfigFile) + +function generateToc(dom) { + const tocAnchors = dom.window.document.querySelectorAll(".toc-anchor") + let tocLinks = [] + + // This basically creates a proper link for the ToC. :))) + tocAnchors.forEach(anchor => tocLinks.push(`${anchor.getAttribute('data-orig-text')}`)) + + return tocLinks +} + +function generateContext(renderedNjk, externalContext = undefined) { + const dom = new JSDOM(renderedNjk) + + return { + serverName: Config.serverName, + tocLinks: generateToc(dom), + externalMeta: externalContext ? parseExternalContext(externalContext) : undefined + } +} + +export function njkRenderer(path) { + const njkFile = fs.readFileSync(path).toString() + const renderedNjk = njk.renderString(njkFile) + + const context = generateContext(renderedNjk) + + 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' + const externalMarkdown = externalMarkdownFile.replace(/^%%-\n[\S\s]*-%%\n/, "") + const externalContext = externalMarkdownFile.match(/^%%-\n[\S\s]*-%%\n/)[0] + + const renderedNjk = njk.renderString(njkFile, { externalMarkdown }) + + let context = generateContext(renderedNjk, externalContext) + context.externalMarkdown = externalMarkdown + + return njk.renderString(njkFile, context) +} \ No newline at end of file