lbry.tech/views/pages/page.js

170 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-07-12 17:21:42 +02:00
"use strict";
// P A C K A G E S
const decamelize = require("decamelize");
const dedent = require("dedent");
const exists = require("fs-exists-sync");
const fm = require("front-matter");
const fs = require("graceful-fs");
const html = require("choo-async/html");
const local = require("app-root-path").require;
const numberRegex = /^[0-9]/g;
2018-07-12 17:21:42 +02:00
const md = require("markdown-it")({
html: true,
typographer: true
2018-07-26 00:04:21 +02:00
}).use(local("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;
}
})
.use(require("markdown-it-wikilinks")({
makeAllLinksAbsolute: true,
baseURL: "/glossary#",
uriSuffix: "",
htmlAttributes: {
class: "wikilink"
}
}));
2018-07-12 17:21:42 +02:00
const raw = require("nanohtml/raw");
2018-07-13 23:58:24 +02:00
// E X P O R T
2018-07-12 17:21:42 +02:00
2018-07-13 23:58:24 +02:00
module.exports = exports = () => async state => {
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
if (!fs.existsSync(`./documents/${path}.md`)) {
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">404</h1>
</div>
</div>
</header>
<section class="page__content" itemprop="articleBody">
<div class="inner-wrap">
2018-07-12 23:33:54 +02:00
<p>The page you are looking for does not exist.</p>
2018-07-12 17:21:42 +02:00
</div>
</section>
</article>
`;
}
const markdownFile = fs.readFileSync(`./documents/${path}.md`, "utf-8");
2018-07-12 17:21:42 +02:00
const markdownFileDetails = fm(markdownFile);
const renderedMarkdown = md.render(partialFinder(markdownFileDetails.body));
2018-07-27 00:18:26 +02:00
let newMetadata = "";
if (markdownFileDetails.attributes.meta) newMetadata = markdownFileDetails.attributes.meta;
2018-07-12 17:21:42 +02:00
2018-07-16 23:06:37 +02:00
let pageScript = "";
if (path === "overview") pageScript = "<script>" + fs.readFileSync("./views/partials/ecosystem-scripts.js", "utf-8") + "</script>";
if (path === "glossary") pageScript = "<script>" + fs.readFileSync("./views/partials/glossary-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">
${raw(renderedMarkdown)}
2018-07-16 23:06:37 +02:00
${raw(pageScript)}
2018-07-27 00:18:26 +02:00
${newMetadata.length ? raw(updateMetadata(newMetadata)) : ""}
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
function createMetaTags(metaObject) {
/**
NOTE:
For Markdown files, the custom yaml should look like this:
meta:
- description: Description goes here
This does not currently work with parameters like "og:image"
// https://github.com/lbryio/lbry.tech/issues/30
*/
let html = "";
for (const metaProperty in metaObject) {
html += `document.getElementsByTagName("meta")["${metaProperty}"].content = "${metaObject[metaProperty]}";\n`;
}
return html;
}
2018-07-12 17:21:42 +02:00
function partialFinder(markdownBody) {
const regexToFindPartials = /<\w+\/>/g;
const partials = markdownBody.match(regexToFindPartials);
if (!partials) return markdownBody;
2018-07-12 17:21:42 +02:00
for (const partial of partials) {
const filename = decamelize(partial, "-").replace("<", "").replace("/>", "");
const fileExistsTest = exists(`./views/partials/${filename}.js`); // `local` results in error if used here and file !exist
if (fileExistsTest) {
const partialFunction = local(`/views/partials/${filename}.js`);
2018-07-12 17:21:42 +02:00
if (filename === "ecosystem") {
const Ecosystem = new partialFunction;
2018-07-12 17:21:42 +02:00
markdownBody = markdownBody.replace(partial, Ecosystem.render());
}
else markdownBody = markdownBody.replace(partial, partialFunction);
2018-07-12 17:21:42 +02:00
}
}
return dedent(markdownBody); // partials get rendered as code snippets w/o `dedent`
}
2018-07-27 00:18:26 +02:00
function updateMetadata(metadataDetails) {
const generatedMetadata = [];
for (const metadataDetail of metadataDetails) {
generatedMetadata.push(createMetaTags(metadataDetail));
}
return dedent`
<script>
${generatedMetadata.join("")}
</script>
`;
}