lbry.tech/app/views/redirect.js

138 lines
4.2 KiB
JavaScript
Raw Normal View History

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";
import raw from "choo/html/raw";
// V A R I A B L E S
const numberRegex = /^[0-9]/g;
2018-10-01 22:47:10 +02:00
const redirect404 = local("app/modules/redirect-404");
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"))
.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-07-12 17:21:42 +02:00
2018-10-01 22:47:10 +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;
2018-10-06 22:53:01 +02:00
2018-07-30 19:30:50 +02:00
if (state.route === "resources/*") path = `resources/${state.params.wildcard}`;
else path = state.params.wildcard;
2018-07-12 17:21:42 +02:00
2018-10-01 22:47:10 +02:00
if (!fs.existsSync(`./documents/${path}.md`))
return redirect404(state);
2018-07-12 17:21:42 +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);
const updatedMarkdown = wikiFinder(partialFinder(renderedMarkdown));
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-10-06 22:53:01 +02:00
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-10-03 22:27:13 +02:00
if (path === "playground") pageScript = "<script>" + fs.readFileSync("./app/components/client/playground-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">
<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);
if (!partials) return markdownBody;
2018-07-12 17:21:42 +02:00
for (const partial of partials) {
2018-10-06 22:53:01 +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-10-01 22:47:10 +02:00
if (!fileExistsTest)
2018-09-28 22:16:29 +02:00
markdownBody = markdownBody.replace(partial, "");
2018-10-01 22:47:10 +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
}
function wikiFinder(markdownBody) {
return markdownBody.replace(/\[\[([\w\s/-]+)\]\]/g, (match, p1) => {
2018-10-01 22:47:10 +02:00
const label = p1.trim();
const url = encodeURI("/glossary#" + label.replace(/\s+/g, "-"));
2018-10-01 01:43:35 +02:00
return label ?
2018-10-01 22:47:10 +02:00
`<a href="${url}" class="link--glossary">${label}</a>` :
2018-10-01 01:43:35 +02:00
match.input;
2018-10-01 22:47:10 +02:00
});
}