File tree Expand file tree Collapse file tree 4 files changed +50
-8
lines changed Expand file tree Collapse file tree 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')
1111const anchor = require ( 'markdown-it-anchor' )
1212const toc = require ( 'markdown-it-table-of-contents' )
1313const _slugify = require ( './slugify' )
14- const { parseHeaders, removeTailHtml } = require ( '../util/parseHeaders' )
15- const { compose } = require ( '../util/shared' )
14+ const { parseHeaders } = require ( '../util/parseHeaders' )
1615
1716module . 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 ,
Original file line number Diff line number Diff 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.
2525exports . 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
3234exports . deeplyParseHeaders = compose (
35+ exports . removeTailHtml ,
3336 exports . parseHeaders ,
34- exports . removeTailHtml
3537)
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 11import {
22 parseHeaders ,
3- removeTailHtml
3+ removeTailHtml ,
4+ deeplyParseHeaders
45} from '@/util/parseHeaders'
56
67describe ( '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} )
You can’t perform that action at this time.
0 commit comments