From d49f56e0227563b62119cbe6d034366a80fcc940 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Wed, 24 Aug 2016 17:17:59 -0400 Subject: [PATCH] Markdown metadata parser --- controllers/pages.js | 5 ++--- models/markdown.js | 35 ++++++++++++++++++++++------------- views/pages/view.pug | 6 +++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/controllers/pages.js b/controllers/pages.js index 26783209..c66d2dba 100644 --- a/controllers/pages.js +++ b/controllers/pages.js @@ -13,9 +13,8 @@ router.get('/', (req, res) => { fs.readFileAsync("repo/Storage/Redis.md", "utf8").then(function(contents) { let pageData = mark.parse(contents); - if(!pageData.title) { - pageData.title = 'Redis'; - pageData.subtitle = 'An open source, in-memory data structure store, used as database, cache and message broker.'; + if(!pageData.meta.title) { + pageData.meta.title = 'Redis.md'; } res.render('pages/view', { pageData }); }); diff --git a/models/markdown.js b/models/markdown.js index b3a8f322..ec0e7d5f 100644 --- a/models/markdown.js +++ b/models/markdown.js @@ -52,13 +52,9 @@ var mkdown = md({ // Rendering rules mkdown.renderer.rules.emoji = function(token, idx) { - return ''; + return ''; }; -mkdown.inline.ruler.push('internal_link', (state) => { - -}); - /** * Parse markdown content and build TOC tree * @@ -74,28 +70,28 @@ const parseTree = (content) => { for (let i = 0; i < tokens.length; i++) { if (tokens[i].type !== "heading_close") { - continue; + continue; } const heading = tokens[i - 1]; const heading_close = tokens[i]; if (heading.type === "inline") { - let content = ""; - let anchor = ""; - if (heading.children && heading.children[0].type === "link_open") { + let content = ""; + let anchor = ""; + if (heading.children && heading.children[0].type === "link_open") { content = heading.children[1].content; anchor = slug(content, {lower: true}); - } else { + } else { content = heading.content anchor = slug(heading.children.reduce((acc, t) => acc + t.content, ""), {lower: true}); - } + } - tocArray.push({ + tocArray.push({ content, anchor, level: +heading_close.tag.substr(1, 1) - }); + }); } } @@ -159,10 +155,23 @@ const parseContent = (content) => { }; +const parseMeta = (content) => { + + let commentMeta = new RegExp('','g'); + let results = {}, match; + while(match = commentMeta.exec(content)) { + results[_.toLower(match[1])] = _.trim(match[2]); + } + + return results; + +}; + module.exports = { parse(content) { return { + meta: parseMeta(content), html: parseContent(content), tree: parseTree(content) }; diff --git a/views/pages/view.pug b/views/pages/view.pug index 41b31fc3..59d35a5d 100644 --- a/views/pages/view.pug +++ b/views/pages/view.pug @@ -37,9 +37,9 @@ block content .column - h1.title#title= pageData.title - if pageData.subtitle - h2.subtitle= pageData.subtitle + h1.title#title= pageData.meta.title + if pageData.meta.subtitle + h2.subtitle= pageData.meta.subtitle .content.mkcontent != pageData.html