lbry.tech/app/views/redirect.js

110 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-07-12 17:21:42 +02:00
"use strict";
2018-11-30 21:46:22 +01:00
// I M P O R T S
2018-07-12 17:21:42 +02:00
2018-10-12 00:16:11 +02:00
import fm from "front-matter";
2018-08-29 01:57:18 +02:00
import fs from "graceful-fs";
import html from "choo/html";
import raw from "choo/html/raw";
2018-10-10 19:56:35 +02:00
// U T I L S
import markdown from "~component/markdown";
2019-01-18 19:15:17 +01:00
import page404 from "./404";
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-11-30 21:46:22 +01:00
export default (state, emit) => { // eslint-disable-line
2019-01-30 23:15:10 +01:00
const partialPath = state.route === "resources/*" ?
`resources/${state.params.wildcard}` :
state.params.wildcard;
const path = `./documents/${partialPath}.md`;
2018-07-12 17:21:42 +02:00
2018-10-12 00:10:32 +02:00
if (!fs.existsSync(path))
2019-01-18 19:15:17 +01:00
return page404();
2018-07-12 17:21:42 +02:00
const markdownFile = fs.readFileSync(path, "utf-8");
2018-07-12 17:21:42 +02:00
const markdownFileDetails = fm(markdownFile);
2019-02-18 21:59:27 +01:00
const title = markdownFileDetails.attributes.title;
const description = markdownFileDetails.attributes.description || "";
if (markdownFileDetails.attributes.meta) {
const customMetadata = {};
for (const key in markdownFileDetails.attributes.meta) {
2019-07-10 19:34:19 +02:00
if (Object.prototype.hasOwnProperty.call(markdownFileDetails.attributes.meta, 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
2019-02-18 21:59:27 +01:00
// below is evil, I just inherited it -- Jeremy
state.lbry = {
title: title,
description: description
};
// below should be refactored into components
2018-07-16 23:06:37 +02:00
let pageScript = "";
2018-10-06 22:53:01 +02:00
2019-01-31 16:35:20 +01:00
switch(true) {
2019-02-05 00:42:52 +01:00
case partialPath === "developer-program":
pageScript = renderClientScript("devprogram-scripts");
break;
2019-01-31 16:35:20 +01:00
case partialPath === "glossary":
2019-02-05 00:42:52 +01:00
pageScript = renderClientScript("glossary-scripts");
2019-01-31 16:35:20 +01:00
break;
case partialPath === "overview":
2019-02-05 00:42:52 +01:00
pageScript = renderClientScript("ecosystem-scripts");
2019-01-31 16:35:20 +01:00
break;
case partialPath === "playground":
2019-02-05 00:42:52 +01:00
pageScript = renderClientScript("playground-scripts");
2019-01-31 16:35:20 +01:00
break;
default:
break;
}
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">
2019-02-15 17:50:56 +01:00
<h1 class="page__header__title" itemprop="name headline">${title}</h1>
2018-07-12 17:21:42 +02:00
</div>
</div>
</header>
<section class="page__content" itemprop="articleBody">
<div class="inner-wrap">
${markdown(path)}
2018-07-16 23:06:37 +02:00
${raw(pageScript)}
2018-07-12 17:21:42 +02:00
</div>
</section>
</article>
`;
};
2019-02-05 00:42:52 +01:00
// H E L P E R
function renderClientScript(clientScriptFileName) {
return `
<script>
${fs.readFileSync((`${process.cwd()}/app/components/client/${clientScriptFileName}.js`), "utf-8")}
</script>
`;
}