Added support for plain markdown files.

This commit is contained in:
BurnyLlama 2021-08-06 14:46:16 +02:00
parent d4e616bc54
commit 3a1c038b78
9 changed files with 116 additions and 43 deletions

View File

@ -93,9 +93,5 @@
Default: `qwik` Default: `qwik`
# Usage # 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 %} {% endblock %}

View File

@ -0,0 +1,6 @@
%%-
title: Hello World
header: Test Article
-%%
# Hello wonderful world!!

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
{% include "templates/defaultTags.njk" %}
<link rel="stylesheet" href="/assets/css/article.css">
<link rel="stylesheet" href="/assets/css/syntax.css">
<title>{{ externalMeta.title }}</title>
</head>
<body>
<article>
<header>
{{ externalMeta.header }}
</header>
<div class="toc-container">
<div class="toc">
<p class="toc-title">Table of Contents</p>
{% for tocLink in tocLinks %}
{{ tocLink | safe }}
{% endfor %}
</div>
</div>
{% markdown %}
{{ externalMarkdown }}
{% endmarkdown %}
</article>
</body>
</html>

View File

@ -8,7 +8,7 @@ import marked from 'marked'
import hljs from 'highlight.js' import hljs from 'highlight.js'
import { requestHandler } from './libs/requestHandler.js' import { requestHandler } from './libs/requestHandler.js'
import { mdRenderer } from './libs/mdRenderer.js' import { markedRenderer } from './libs/markedRenderer.js'
// Load in config // Load in config
@ -44,7 +44,7 @@ marked.setOptions({
gfm: true gfm: true
}) })
marked.use({ renderer: mdRenderer }) marked.use({ renderer: markedRenderer })
njkMarkdown.register(njkEnv, marked) njkMarkdown.register(njkEnv, marked)

16
libs/externalContext.js Normal file
View File

@ -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
}

View File

@ -1,4 +1,4 @@
export const mdRenderer = { export const markedRenderer = {
heading(text, level) { heading(text, level) {
return ` return `
<a name="${text.replace(/\s/g, "-")}" data-orig-text="${text}" class="toc-anchor toc-anchor-h${level}"></a> <a name="${text.replace(/\s/g, "-")}" data-orig-text="${text}" class="toc-anchor toc-anchor-h${level}"></a>

View File

@ -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(`<a href="#${anchor.name}" class="${anchor.classList[1].replace("-anchor", "")}">${anchor.getAttribute('data-orig-text')}</a>`))
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)
}

View File

@ -1,5 +1,5 @@
import fs from 'fs' import fs from 'fs'
import { njkRenderer } from './njkRenderer.js' import { mdRenderer, njkRenderer } from './siteRenderer.js'
export function requestHandler(req, res, Config) { export function requestHandler(req, res, Config) {
if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.njk`)) 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`)) if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.njk`))
return res.send(njkRenderer(`./${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`)) return res.status(404).send(njkRenderer(`./${Config.contentDir}/errors/404.njk`))
} }

54
libs/siteRenderer.js Normal file
View File

@ -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(`<a href="#${anchor.name}" class="${anchor.classList[1].replace("-anchor", "")}">${anchor.getAttribute('data-orig-text')}</a>`))
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)
}