Skip to content

Commit

Permalink
feat: header extraction improvement (close: #238) (#271)
Browse files Browse the repository at this point in the history
* feat: sidebar header extraction improvements (close: #238)

* chore: add more markdown tokens to be removed

* fix: typo
  • Loading branch information
ulivz authored and yyx990803 committed Apr 29, 2018
1 parent f0b33e1 commit 53c8489
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/util/index.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down Expand Up @@ -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])
}
}

Expand Down Expand Up @@ -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),
Expand Down
29 changes: 29 additions & 0 deletions lib/util/parseHeaders.js
Original file line number Diff line number Diff line change
@@ -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(/&lt;/g, '<')
.replace(/&gt;/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)

0 comments on commit 53c8489

Please sign in to comment.