From 53c84892fe04c44df22f07d2b53bb3871529a216 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 29 Apr 2018 16:31:19 -0500 Subject: [PATCH] feat: header extraction improvement (close: #238) (#271) * feat: sidebar header extraction improvements (close: #238) * chore: add more markdown tokens to be removed * fix: typo --- lib/util/index.js | 11 ++++------- lib/util/parseHeaders.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 lib/util/parseHeaders.js diff --git a/lib/util/index.js b/lib/util/index.js index 01ef6ac77f..67588591da 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -1,7 +1,4 @@ -const parseEmojis = str => { - const emojiData = require('markdown-it-emoji/lib/data/full.json') - return str.replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder) -} +const parseHeaders = require('./parseHeaders') exports.normalizeHeadTag = tag => { if (typeof tag === 'string') { @@ -35,11 +32,11 @@ exports.inferTitle = function (frontmatter) { return 'Home' } if (frontmatter.data.title) { - return parseEmojis(frontmatter.data.title) + return parseHeaders(frontmatter.data.title) } const match = frontmatter.content.trim().match(/^#+\s+(.*)/) if (match) { - return parseEmojis(match[1]) + return parseHeaders(match[1]) } } @@ -71,7 +68,7 @@ exports.extractHeaders = (content, include = [], md) => { const res = [] tokens.forEach((t, i) => { if (t.type === 'heading_open' && include.includes(t.tag)) { - const title = parseEmojis(tokens[i + 1].content) + const title = parseHeaders(tokens[i + 1].content) const slug = t.attrs.find(([name]) => name === 'id')[1] res.push({ level: parseInt(t.tag.slice(1), 10), diff --git a/lib/util/parseHeaders.js b/lib/util/parseHeaders.js new file mode 100644 index 0000000000..8fbf18ac95 --- /dev/null +++ b/lib/util/parseHeaders.js @@ -0,0 +1,29 @@ +const parseEmojis = str => { + const emojiData = require('markdown-it-emoji/lib/data/full.json') + return str.replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder) +} + +const unescapeHtml = html => html + .replace(/"/g, '"') + .replace(/'/g, '\'') + .replace(/:/g, ':') + .replace(/</g, '<') + .replace(/>/g, '>') + +const removeMarkdownToken = str => str + .replace(/`(.*)`/, '$1') // `` + .replace(/\[(.*)\]\(.*\)/, '$1') // []() + .replace(/\*\*(.*)\*\*/, '$1') // ** + .replace(/\*(.*)\*/, '$1') // * + .replace(/_(.*)_/, '$1') // _ + +// put here to avoid circular references +const compose = (...processors) => { + if (processors.length === 0) return input => input + if (processors.length === 1) return processors[0] + return processors.reduce((prev, next) => { + return (...args) => next(prev(...args)) + }) +} + +module.exports = compose(unescapeHtml, parseEmojis, removeMarkdownToken)