2018-07-12 17:21:42 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P A C K A G E S
|
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
import decamelize from "decamelize";
|
|
|
|
import exists from "fs-exists-sync";
|
|
|
|
import fm from "front-matter";
|
|
|
|
import fs from "graceful-fs";
|
|
|
|
import html from "choo/html";
|
2018-08-30 23:53:52 +02:00
|
|
|
import path from "path";
|
2018-08-29 01:57:18 +02:00
|
|
|
import { require as local } from "app-root-path";
|
2018-09-30 21:56:48 +02:00
|
|
|
import redirects from '../data/redirects.json';
|
|
|
|
import redirect from "../modules/redirect";
|
|
|
|
import Page404 from "./404.js";
|
2018-08-29 01:57:18 +02:00
|
|
|
import raw from "choo/html/raw";
|
|
|
|
|
|
|
|
// V A R I A B L E S
|
|
|
|
|
2018-07-20 17:57:25 +02:00
|
|
|
const numberRegex = /^[0-9]/g;
|
2018-07-13 20:03:21 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
const md = require("markdown-it")({
|
|
|
|
html: true,
|
|
|
|
typographer: true
|
2018-08-29 01:57:18 +02:00
|
|
|
}).use(local("app/modules/markdown-it-sup"))
|
2018-07-20 17:57:25 +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-09-30 23:59:49 +02:00
|
|
|
});
|
2018-07-12 17:21:42 +02:00
|
|
|
|
2018-07-13 23:58:24 +02:00
|
|
|
// E X P O R T
|
2018-07-12 17:21:42 +02:00
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
module.exports = exports = (state, emit) => { // eslint-disable-line
|
2018-07-30 19:30:50 +02:00
|
|
|
let path;
|
|
|
|
|
|
|
|
if (state.route === "resources/*") path = `resources/${state.params.wildcard}`;
|
|
|
|
else path = state.params.wildcard;
|
2018-07-12 17:21:42 +02:00
|
|
|
|
2018-07-13 20:03:21 +02:00
|
|
|
if (!fs.existsSync(`./documents/${path}.md`)) {
|
2018-09-30 21:56:48 +02:00
|
|
|
const redirectUrl = redirects[path] || redirects["/" + path]
|
|
|
|
if (redirectUrl) {
|
|
|
|
redirect(redirectUrl)
|
|
|
|
} else {
|
|
|
|
return Page404();
|
|
|
|
}
|
2018-07-12 17:21:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 20:03:21 +02:00
|
|
|
const markdownFile = fs.readFileSync(`./documents/${path}.md`, "utf-8");
|
2018-07-12 17:21:42 +02:00
|
|
|
const markdownFileDetails = fm(markdownFile);
|
2018-08-24 23:58:04 +02:00
|
|
|
const renderedMarkdown = md.render(markdownFileDetails.body);
|
2018-09-30 23:59:49 +02:00
|
|
|
const updatedMarkdown = wikiFinder(partialFinder(renderedMarkdown));
|
2018-09-28 23:20:51 +02:00
|
|
|
|
|
|
|
if (markdownFileDetails.attributes.meta) {
|
|
|
|
const customMetadata = {};
|
|
|
|
|
|
|
|
for (const key in markdownFileDetails.attributes.meta) {
|
|
|
|
if (markdownFileDetails.attributes.meta.hasOwnProperty(key)) {
|
|
|
|
customMetadata[Object.keys(markdownFileDetails.attributes.meta[key])[0]] =
|
|
|
|
markdownFileDetails.attributes.meta[key][Object.keys(markdownFileDetails.attributes.meta[key])[0]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.lbry = customMetadata;
|
|
|
|
}
|
2018-07-12 17:21:42 +02:00
|
|
|
|
2018-07-16 23:06:37 +02:00
|
|
|
let pageScript = "";
|
2018-08-30 23:53:52 +02:00
|
|
|
if (path === "glossary") pageScript = "<script>" + fs.readFileSync("./app/components/client/glossary-scripts.js", "utf-8") + "</script>";
|
|
|
|
if (path === "overview") pageScript = "<script>" + fs.readFileSync("./app/components/client/ecosystem-scripts.js", "utf-8") + "</script>";
|
2018-09-27 18:11:26 +02:00
|
|
|
if (path === "playground") pageScript = "<script>" + fs.readFileSync("./app/components/client/tour-scripts.js", "utf-8") + "</script>";
|
2018-07-12 17:21:42 +02:00
|
|
|
|
|
|
|
return html`
|
|
|
|
<article class="page" itemtype="http://schema.org/BlogPosting">
|
|
|
|
<header class="page__header">
|
|
|
|
<div class="page__header-wrap">
|
|
|
|
<div class="inner-wrap">
|
|
|
|
<h1 class="page__header__title" itemprop="name headline">${markdownFileDetails.attributes.title}</h1>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</header>
|
|
|
|
|
|
|
|
<section class="page__content" itemprop="articleBody">
|
|
|
|
<div class="inner-wrap">
|
2018-09-25 18:34:13 +02:00
|
|
|
<div class="page__markup">${raw(updatedMarkdown)}</div>
|
2018-07-16 23:06:37 +02:00
|
|
|
${raw(pageScript)}
|
2018-07-12 17:21:42 +02:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-27 00:18:26 +02:00
|
|
|
// H E L P E R S
|
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
function partialFinder(markdownBody) {
|
2018-09-24 23:58:31 +02:00
|
|
|
const regexToFindPartials = /<\w+ ?\/>/g;
|
2018-07-12 17:21:42 +02:00
|
|
|
const partials = markdownBody.match(regexToFindPartials);
|
|
|
|
|
2018-07-13 18:28:24 +02:00
|
|
|
if (!partials) return markdownBody;
|
2018-07-12 17:21:42 +02:00
|
|
|
|
|
|
|
for (const partial of partials) {
|
2018-09-24 23:58:31 +02:00
|
|
|
const filename = decamelize(partial, "-").replace("<", "").replace("/>", "").trim();
|
2018-08-30 23:53:52 +02:00
|
|
|
const fileExistsTest = exists(`./app/components/${filename}.js`); // `local` results in error if used here and file !exist
|
2018-07-12 17:21:42 +02:00
|
|
|
|
2018-09-28 22:16:29 +02:00
|
|
|
if (!fileExistsTest) {
|
|
|
|
markdownBody = markdownBody.replace(partial, "");
|
2018-09-24 23:58:31 +02:00
|
|
|
} else {
|
2018-08-30 23:53:52 +02:00
|
|
|
const partialFunction = require(path.join(__dirname, "..", `./components/${filename}.js`));
|
2018-09-28 21:31:59 +02:00
|
|
|
|
|
|
|
if (filename === "glossary-toc") markdownBody = markdownBody.replace(partial, partialFunction);
|
|
|
|
else markdownBody = markdownBody.replace(partial, `</div>${partialFunction.default()}<div class="page__markup">`);
|
2018-07-12 17:21:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 23:58:04 +02:00
|
|
|
return markdownBody;
|
2018-07-12 17:21:42 +02:00
|
|
|
}
|
2018-09-30 23:59:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
function wikiFinder(markdownBody) {
|
|
|
|
return markdownBody.replace(/\[\[([\w\s/-]+)\]\]/g, (match, p1) => {
|
|
|
|
const label = p1.trim(),
|
|
|
|
href = encodeURI("/glossary#" + label.replace(/\s+/g, '-'));
|
|
|
|
|
|
|
|
return label ?
|
|
|
|
`<a href="${href}" class="link--glossary">${label}</a>` :
|
|
|
|
match.input;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|