lbry.tech/app/views/redirect.js

167 lines
4.9 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-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;
}
})
.use(require("markdown-it-wikilinks")({
makeAllLinksAbsolute: true,
baseURL: "/glossary#",
uriSuffix: "",
htmlAttributes: {
class: "wikilink"
}
}));
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
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 page__markup" itemprop="articleBody">
2018-07-12 17:21:42 +02:00
<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);
2018-08-24 23:58:04 +02:00
const renderedMarkdown = md.render(markdownFileDetails.body);
const updatedMarkdown = partialFinder(renderedMarkdown);
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 = "";
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">
<div class="page__markup">${raw(updatedMarkdown)}</div>
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:
2018-08-25 00:30:58 +02:00
- description: Description goes here
2018-07-27 00:18:26 +02:00
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("/>", "");
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
if (fileExistsTest) {
2018-08-30 23:53:52 +02:00
const partialFunction = require(path.join(__dirname, "..", `./components/${filename}.js`));
2018-09-26 18:42:41 +02:00
const markdownHtml = filename === "glossary-toc" ? partialFunction : partialFunction.default(); // kill special case
markdownBody = markdownBody.replace(partial, `</div>${markdownHtml}<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-07-27 00:18:26 +02:00
function updateMetadata(metadataDetails) {
const generatedMetadata = [];
for (const metadataDetail of metadataDetails) {
generatedMetadata.push(createMetaTags(metadataDetail));
}
2018-08-24 23:58:04 +02:00
return html`
<script>${generatedMetadata.join("")}</script>
2018-07-27 00:18:26 +02:00
`;
}