lbry.tech/app/components/markdown.js

98 lines
2.5 KiB
JavaScript
Raw Normal View History

"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
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";
import html from "choo/html";
2019-01-30 23:15:10 +01:00
import m from "markdown-it";
2019-01-30 00:29:14 +01:00
import markdownAnchor from "markdown-it-anchor";
import markdownSup from "@module/markdown-it-sup";
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
const numberRegex = /^[0-9]/g;
2019-01-30 23:15:10 +01:00
const md = m({
html: true,
typographer: true
2019-01-30 00:29:14 +01:00
}).use(markdownSup)
.use(markdownAnchor, {
slugify: stringToSlugify => {
let finalString = stringToSlugify
.toLowerCase()
.replace(/\s\/\s/g, "-")
.replace(/\s/g, "-")
.replace(/%/g, "")
.replace(/\(/g, "")
.replace(/\)/g, "")
.replace(/,/g, "");
2019-01-30 23:15:10 +01:00
if (finalString.match(numberRegex))
finalString = `_${finalString}`;
return finalString;
}
});
2018-10-12 00:10:32 +02:00
// E X P O R T
2018-10-12 00:10:32 +02:00
export default path => {
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-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
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`));
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-12 02:26:02 +02:00
return ("<div class=\"page__markup\">" + markdownBody + "</div>").replace(/<div class="page__markup">\s*<\/div>/, "");
}
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>` :
match.input;
});
}