Skip to content

Commit c2eaff3

Browse files
committed
fix: wrong sidebar slugs and anchor link at content (close: #645)
1 parent 9b4b9ba commit c2eaff3

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

lib/markdown/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ const emoji = require('markdown-it-emoji')
1111
const anchor = require('markdown-it-anchor')
1212
const toc = require('markdown-it-table-of-contents')
1313
const _slugify = require('./slugify')
14-
const { parseHeaders, removeTailHtml } = require('../util/parseHeaders')
15-
const { compose } = require('../util/shared')
14+
const { parseHeaders } = require('../util/parseHeaders')
1615

1716
module.exports = ({ markdown = {}} = {}) => {
1817
// allow user config slugify
19-
const slugify = markdown.slugify || compose(removeTailHtml, _slugify)
18+
const slugify = markdown.slugify || _slugify
2019

2120
const md = require('markdown-it')({
2221
html: true,

lib/util/parseHeaders.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ exports.removeTailHtml = (str) => {
2121
return String(str).replace(/\s*?<.*>\s*$/g, '')
2222
}
2323

24-
// only remove some md tokens.
24+
// Only remove some md tokens.
2525
exports.parseHeaders = compose(
2626
unescapeHtml,
2727
parseEmojis,
2828
removeMarkdownToken
2929
)
3030

31-
// also clean html in headers.
31+
// Also clean the tail html in headers.
32+
// Since we want to support tailed badge in headers.
33+
// See: https://vuepress.vuejs.org/guide/using-vue.html#badge
3234
exports.deeplyParseHeaders = compose(
35+
exports.removeTailHtml,
3336
exports.parseHeaders,
34-
exports.removeTailHtml
3537
)

test/markdown/slugify.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Md } from './util'
2+
import anchor from 'markdown-it-anchor'
3+
import slugify from '@/markdown/slugify.js'
4+
5+
const mdS = Md().use(anchor, {
6+
slugify,
7+
permalink: true,
8+
permalinkBefore: true,
9+
permalinkSymbol: '#'
10+
})
11+
12+
const slugifyAsserts = {
13+
/* markdown: id */
14+
'# a b': 'a-b',
15+
'# a-b': 'a-b',
16+
'# `<a>`': 'a',
17+
'# `<a>`b': 'a-b',
18+
'# `<a>` b': 'a-b'
19+
}
20+
21+
describe('slugify', () => {
22+
test('should convert headers correctly', () => {
23+
for (const input in slugifyAsserts) {
24+
const output = mdS.render(input)
25+
expect(getHeading(output)).toBe(slugifyAsserts[input])
26+
}
27+
})
28+
})
29+
30+
function getHeading (output) {
31+
return output.match(/id=\\?"([^"]*)\\?"/)[1]
32+
}

test/util/parseHeaders.spec.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
parseHeaders,
3-
removeTailHtml
3+
removeTailHtml,
4+
deeplyParseHeaders
45
} from '@/util/parseHeaders'
56

67
describe('parseHeaders', () => {
@@ -32,6 +33,14 @@ describe('parseHeaders', () => {
3233
})
3334

3435
test('should remove tail html correctly', () => {
35-
expect(removeTailHtml('# H1 <div></div>>')).toBe('# H1')
36+
expect(removeTailHtml('# H1 <Comp></Comp>')).toBe('# H1')
37+
expect(removeTailHtml('# H1 <Comp a="b"></Comp>')).toBe('# H1')
38+
expect(removeTailHtml('# H1 <Comp/>')).toBe('# H1')
39+
expect(removeTailHtml('# H1 <Comp a="b"/>')).toBe('# H1')
40+
})
41+
42+
test('should deeplyParseHeaders transformed as expected', () => {
43+
expect(deeplyParseHeaders('# `H1` <Comp></Comp>')).toBe('# H1')
44+
expect(deeplyParseHeaders('# *H1* <Comp/>')).toBe('# H1')
3645
})
3746
})

0 commit comments

Comments
 (0)