2018-10-08 16:36:09 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-10-12 00:10:32 +02:00
|
|
|
|
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
// I M P O R T S
|
2018-10-12 00:10:32 +02:00
|
|
|
|
2018-10-08 16:36:09 +02:00
|
|
|
import decamelize from "decamelize";
|
|
|
|
import exists from "fs-exists-sync";
|
|
|
|
import fm from "front-matter";
|
2018-10-12 00:10:32 +02:00
|
|
|
import fs from "graceful-fs";
|
2018-10-08 16:36:09 +02:00
|
|
|
import html from "choo/html";
|
2019-01-30 00:29:14 +01:00
|
|
|
import markdownAnchor from "markdown-it-anchor";
|
|
|
|
import markdownSup from "@module/markdown-it-sup";
|
2018-10-08 16:36:09 +02:00
|
|
|
import path from "path";
|
|
|
|
import raw from "choo/html/raw";
|
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
// U T I L S
|
2018-10-12 00:10:32 +02:00
|
|
|
|
2018-10-08 16:36:09 +02:00
|
|
|
const numberRegex = /^[0-9]/g;
|
|
|
|
const md = require("markdown-it")({
|
|
|
|
html: true,
|
|
|
|
typographer: true
|
2019-01-30 00:29:14 +01:00
|
|
|
}).use(markdownSup)
|
|
|
|
.use(markdownAnchor, {
|
2018-10-08 16:36:09 +02:00
|
|
|
slugify: stringToSlugify => {
|
|
|
|
let finalString = stringToSlugify
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/\s\/\s/g, "-")
|
|
|
|
.replace(/\s/g, "-")
|
|
|
|
.replace(/%/g, "")
|
|
|
|
.replace(/\(/g, "")
|
|
|
|
.replace(/\)/g, "")
|
|
|
|
.replace(/,/g, "");
|
|
|
|
|
|
|
|
if (finalString.match(numberRegex)) finalString = `_${finalString}`;
|
|
|
|
return finalString;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-12 00:10:32 +02:00
|
|
|
// E X P O R T
|
2018-10-08 16:36:09 +02:00
|
|
|
|
2018-10-12 00:10:32 +02:00
|
|
|
export default path => {
|
2018-10-08 16:36:09 +02:00
|
|
|
const markdownFile = fs.readFileSync(path, "utf-8");
|
|
|
|
const markdownFileDetails = fm(markdownFile);
|
|
|
|
const renderedMarkdown = md.render(markdownFileDetails.body);
|
|
|
|
const updatedMarkdown = wikiFinder(partialFinder(renderedMarkdown));
|
|
|
|
|
|
|
|
return html`
|
2018-10-12 02:26:02 +02:00
|
|
|
${raw(updatedMarkdown)}
|
2018-10-08 16:36:09 +02:00
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
2018-10-12 00:10:32 +02:00
|
|
|
|
|
|
|
|
2018-10-12 00:16:11 +02:00
|
|
|
// H E L P E R S
|
2018-10-12 00:10:32 +02:00
|
|
|
|
2018-10-08 16:36:09 +02:00
|
|
|
function partialFinder(markdownBody) {
|
|
|
|
const regexToFindPartials = /<\w+ ?\/>/g;
|
|
|
|
const partials = markdownBody.match(regexToFindPartials);
|
|
|
|
|
2018-10-16 16:12:15 +02:00
|
|
|
if (partials) {
|
|
|
|
for (const partial of partials) {
|
|
|
|
const filename = decamelize(partial, "-").replace("<", "")
|
|
|
|
.replace("/>", "")
|
|
|
|
.trim();
|
2018-11-30 21:46:22 +01:00
|
|
|
const fileExistsTest = exists(`./app/components/${filename}.js`);
|
2018-10-16 16:12:15 +02:00
|
|
|
|
|
|
|
if (!fileExistsTest)
|
|
|
|
markdownBody = markdownBody.replace(partial, "");
|
|
|
|
|
|
|
|
else {
|
|
|
|
const partialFunction = require(path.join(__dirname, "..", `./components/${filename}.js`));
|
|
|
|
|
2018-12-03 22:29:06 +01:00
|
|
|
if (filename === "glossary-toc") markdownBody = markdownBody.replace(partial, partialFunction.default);
|
2018-10-16 16:12:15 +02:00
|
|
|
else markdownBody = markdownBody.replace(partial, `</div>${partialFunction.default()}<div class="page__markup">`);
|
|
|
|
}
|
2018-10-08 16:36:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 02:26:02 +02:00
|
|
|
return ("<div class=\"page__markup\">" + markdownBody + "</div>").replace(/<div class="page__markup">\s*<\/div>/, "");
|
2018-10-08 16:36:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function wikiFinder(markdownBody) {
|
|
|
|
return markdownBody.replace(/\[\[([\w\s/-]+)\]\]/g, (match, p1) => {
|
|
|
|
const label = p1.trim();
|
|
|
|
const url = encodeURI("/glossary#" + label.replace(/\s+/g, "-"));
|
|
|
|
|
|
|
|
return label ?
|
2018-10-12 00:10:32 +02:00
|
|
|
`<a class="link--glossary" href="${url}">${label}</a>` :
|
2018-10-08 16:36:09 +02:00
|
|
|
match.input;
|
|
|
|
});
|
|
|
|
}
|