File tree 4 files changed +50
-8
lines changed
4 files changed +50
-8
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,11 @@ const emoji = require('markdown-it-emoji')
11
11
const anchor = require ( 'markdown-it-anchor' )
12
12
const toc = require ( 'markdown-it-table-of-contents' )
13
13
const _slugify = require ( './slugify' )
14
- const { parseHeaders, removeTailHtml } = require ( '../util/parseHeaders' )
15
- const { compose } = require ( '../util/shared' )
14
+ const { parseHeaders } = require ( '../util/parseHeaders' )
16
15
17
16
module . exports = ( { markdown = { } } = { } ) => {
18
17
// allow user config slugify
19
- const slugify = markdown . slugify || compose ( removeTailHtml , _slugify )
18
+ const slugify = markdown . slugify || _slugify
20
19
21
20
const md = require ( 'markdown-it' ) ( {
22
21
html : true ,
Original file line number Diff line number Diff line change @@ -21,15 +21,17 @@ exports.removeTailHtml = (str) => {
21
21
return String ( str ) . replace ( / \s * ?< .* > \s * $ / g, '' )
22
22
}
23
23
24
- // only remove some md tokens.
24
+ // Only remove some md tokens.
25
25
exports . parseHeaders = compose (
26
26
unescapeHtml ,
27
27
parseEmojis ,
28
28
removeMarkdownToken
29
29
)
30
30
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
32
34
exports . deeplyParseHeaders = compose (
35
+ exports . removeTailHtml ,
33
36
exports . parseHeaders ,
34
- exports . removeTailHtml
35
37
)
Original file line number Diff line number Diff line change
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 ( / i d = \\ ? " ( [ ^ " ] * ) \\ ? " / ) [ 1 ]
32
+ }
Original file line number Diff line number Diff line change 1
1
import {
2
2
parseHeaders ,
3
- removeTailHtml
3
+ removeTailHtml ,
4
+ deeplyParseHeaders
4
5
} from '@/util/parseHeaders'
5
6
6
7
describe ( 'parseHeaders' , ( ) => {
@@ -32,6 +33,14 @@ describe('parseHeaders', () => {
32
33
} )
33
34
34
35
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' )
36
45
} )
37
46
} )
You can’t perform that action at this time.
0 commit comments