2018-10-08 16:36:09 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-10-12 00:10:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
// P A C K A G E S
|
|
|
|
|
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";
|
|
|
|
import path from "path";
|
|
|
|
import raw from "choo/html/raw";
|
|
|
|
import { require as local } from "app-root-path";
|
|
|
|
|
2018-10-12 00:10:32 +02:00
|
|
|
// V A R I A B L E
|
|
|
|
|
2018-10-08 16:36:09 +02:00
|
|
|
const numberRegex = /^[0-9]/g;
|
2018-10-12 00:10:32 +02:00
|
|
|
|
|
|
|
// U T I L
|
|
|
|
|
2018-10-08 16:36:09 +02:00
|
|
|
const md = require("markdown-it")({
|
|
|
|
html: true,
|
|
|
|
typographer: true
|
2018-10-12 00:10:32 +02:00
|
|
|
}).use(local("/app/modules/markdown-it-sup"))
|
2018-10-08 16:36:09 +02:00
|
|
|
.use(require("markdown-it-anchor"), {
|
|
|
|
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();
|
|
|
|
const fileExistsTest = exists(`./app/components/${filename}.js`); // `local` results in error if used here and file !exist
|
|
|
|
|
|
|
|
if (!fileExistsTest)
|
|
|
|
markdownBody = markdownBody.replace(partial, "");
|
|
|
|
|
|
|
|
else {
|
|
|
|
const partialFunction = require(path.join(__dirname, "..", `./components/${filename}.js`));
|
|
|
|
|
|
|
|
if (filename === "glossary-toc") markdownBody = markdownBody.replace(partial, partialFunction);
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|