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)