Skip to content

Commit 53c8489

Browse files
ulivzyyx990803
authored andcommitted
feat: header extraction improvement (close: #238) (#271)
* feat: sidebar header extraction improvements (close: #238) * chore: add more markdown tokens to be removed * fix: typo
1 parent f0b33e1 commit 53c8489

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/util/index.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
const parseEmojis = str => {
2-
const emojiData = require('markdown-it-emoji/lib/data/full.json')
3-
return str.replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder)
4-
}
1+
const parseHeaders = require('./parseHeaders')
52

63
exports.normalizeHeadTag = tag => {
74
if (typeof tag === 'string') {
@@ -35,11 +32,11 @@ exports.inferTitle = function (frontmatter) {
3532
return 'Home'
3633
}
3734
if (frontmatter.data.title) {
38-
return parseEmojis(frontmatter.data.title)
35+
return parseHeaders(frontmatter.data.title)
3936
}
4037
const match = frontmatter.content.trim().match(/^#+\s+(.*)/)
4138
if (match) {
42-
return parseEmojis(match[1])
39+
return parseHeaders(match[1])
4340
}
4441
}
4542

@@ -71,7 +68,7 @@ exports.extractHeaders = (content, include = [], md) => {
7168
const res = []
7269
tokens.forEach((t, i) => {
7370
if (t.type === 'heading_open' && include.includes(t.tag)) {
74-
const title = parseEmojis(tokens[i + 1].content)
71+
const title = parseHeaders(tokens[i + 1].content)
7572
const slug = t.attrs.find(([name]) => name === 'id')[1]
7673
res.push({
7774
level: parseInt(t.tag.slice(1), 10),

lib/util/parseHeaders.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const parseEmojis = str => {
2+
const emojiData = require('markdown-it-emoji/lib/data/full.json')
3+
return str.replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder)
4+
}
5+
6+
const unescapeHtml = html => html
7+
.replace(/"/g, '"')
8+
.replace(/'/g, '\'')
9+
.replace(/:/g, ':')
10+
.replace(/&lt;/g, '<')
11+
.replace(/&gt;/g, '>')
12+
13+
const removeMarkdownToken = str => str
14+
.replace(/`(.*)`/, '$1') // ``
15+
.replace(/\[(.*)\]\(.*\)/, '$1') // []()
16+
.replace(/\*\*(.*)\*\*/, '$1') // **
17+
.replace(/\*(.*)\*/, '$1') // *
18+
.replace(/_(.*)_/, '$1') // _
19+
20+
// put here to avoid circular references
21+
const compose = (...processors) => {
22+
if (processors.length === 0) return input => input
23+
if (processors.length === 1) return processors[0]
24+
return processors.reduce((prev, next) => {
25+
return (...args) => next(prev(...args))
26+
})
27+
}
28+
29+
module.exports = compose(unescapeHtml, parseEmojis, removeMarkdownToken)

0 commit comments

Comments
 (0)