From 097bfc65c774e6d65b9ef04ec34bcd87245621a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9D=E3=83=BC=E3=83=AB=20=E3=82=A6=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=83=96?= Date: Fri, 28 Sep 2018 16:20:51 -0500 Subject: [PATCH] Custom metadata is now possible for Markdown pages --- app/components/head.js | 16 ++++++-------- app/views/redirect.js | 48 ++++++++++++------------------------------ 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/app/components/head.js b/app/components/head.js index 9695bb3..3f038c2 100644 --- a/app/components/head.js +++ b/app/components/head.js @@ -24,27 +24,25 @@ module.exports = exports = (state, emit) => { if (state.title !== title) emit(state.events.DOMTITLECHANGE, title); state.page = state.page || { }; - // TODO: - // - Support custom metadata (descriptions and whatnot) + const newMetadata = state.lbry; return html` - ${title} + ${newMetadata && newMetadata.title ? newMetadata.title : title} - - + - - - + + + - + diff --git a/app/views/redirect.js b/app/views/redirect.js index f721e0a..0ac7d63 100644 --- a/app/views/redirect.js +++ b/app/views/redirect.js @@ -79,8 +79,19 @@ module.exports = exports = (state, emit) => { // eslint-disable-line const markdownFileDetails = fm(markdownFile); const renderedMarkdown = md.render(markdownFileDetails.body); const updatedMarkdown = partialFinder(renderedMarkdown); - let newMetadata = ""; - if (markdownFileDetails.attributes.meta) newMetadata = markdownFileDetails.attributes.meta; + + 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; + } let pageScript = ""; if (path === "glossary") pageScript = ""; @@ -101,7 +112,6 @@ module.exports = exports = (state, emit) => { // eslint-disable-line
${raw(updatedMarkdown)}
${raw(pageScript)} - ${newMetadata.length ? raw(updateMetadata(newMetadata)) : ""}
@@ -112,27 +122,6 @@ module.exports = exports = (state, emit) => { // eslint-disable-line // 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; -} - function partialFinder(markdownBody) { const regexToFindPartials = /<\w+\/>/g; const partials = markdownBody.match(regexToFindPartials); @@ -157,14 +146,3 @@ function partialFinder(markdownBody) { return markdownBody; } - -function updateMetadata(metadataDetails) { - const generatedMetadata = []; - - for (const metadataDetail of metadataDetails) - generatedMetadata.push(createMetaTags(metadataDetail)); - - return html` - - `; -}