lbry.tech/app/views/redirect.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-07-12 10:21:42 -05:00
"use strict";
2018-11-30 14:46:22 -06:00
// I M P O R T S
2018-07-12 10:21:42 -05:00
2018-10-11 17:16:11 -05:00
import fm from "front-matter";
2018-08-28 18:57:18 -05:00
import fs from "graceful-fs";
import html from "choo/html";
import raw from "choo/html/raw";
2018-10-10 12:56:35 -05:00
// U T I L S
2018-11-30 14:46:22 -06:00
import markdown from "../components/markdown";
import redirect404 from "../modules/redirect-404";
2018-07-12 10:21:42 -05:00
2018-10-01 15:47:10 -05:00
2018-07-13 16:58:24 -05:00
// E X P O R T
2018-07-12 10:21:42 -05:00
2018-11-30 14:46:22 -06:00
export default (state, emit) => { // eslint-disable-line
const partialPath = state.route === "resources/*" ? `resources/${state.params.wildcard}` : state.params.wildcard;
const path = `./documents/${partialPath}.md`;
2018-07-12 10:21:42 -05:00
2018-10-11 17:10:32 -05:00
if (!fs.existsSync(path))
2018-10-01 15:47:10 -05:00
return redirect404(state);
2018-07-12 10:21:42 -05:00
const markdownFile = fs.readFileSync(path, "utf-8");
2018-07-12 10:21:42 -05:00
const markdownFileDetails = fm(markdownFile);
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]];
}
}
// below seems evil
state.lbry = customMetadata;
}
2018-07-12 10:21:42 -05:00
// below should be refactored into components
2018-07-16 17:06:37 -04:00
let pageScript = "";
2018-10-06 15:53:01 -05:00
if (partialPath === "glossary")
pageScript = "<script>" + fs.readFileSync(`${process.cwd()}/app/components/client/glossary-scripts.js`, "utf-8") + "</script>";
if (partialPath === "overview")
pageScript = "<script>" + fs.readFileSync(`${process.cwd()}/app/components/client/ecosystem-scripts.js`, "utf-8") + "</script>";
if (partialPath === "playground")
pageScript = "<script>" + fs.readFileSync(`${process.cwd()}/app/components/client/playground-scripts.js`, "utf-8") + "</script>";
2018-07-12 10:21:42 -05: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">
${markdown(path)}
2018-07-16 17:06:37 -04:00
${raw(pageScript)}
2018-07-12 10:21:42 -05:00
</div>
</section>
</article>
`;
};