File tree 6 files changed +46
-6
lines changed
6 files changed +46
-6
lines changed Original file line number Diff line number Diff line change @@ -113,19 +113,26 @@ Provide config options to the used theme. The options will vary depending on the
113
113
114
114
## Markdown
115
115
116
+ ### markdown.slugify
117
+
118
+ - Type: ` Function `
119
+ - Default: [ source] ( https://github.com/vuejs/vuepress/blob/master/lib/markdown/slugify.js )
120
+
121
+ Function for transforming header texts into slugs. This affects the ids/links generated for header anchors, table of contents and sidebar links.
122
+
116
123
### markdown.anchor
117
124
118
125
- Type: ` Object `
119
126
- Default: ` { permalink: true, permalinkBefore: true, permalinkSymbol: '#' } `
120
127
121
- Options for [ markdown-it-anchor] ( https://github.com/valeriangalliat/markdown-it-anchor ) .
128
+ Options for [ markdown-it-anchor] ( https://github.com/valeriangalliat/markdown-it-anchor ) . (Note: prefer ` markdown.slugify ` if you want to customize header ids.)
122
129
123
130
### markdown.toc
124
131
125
132
- Type: ` Object `
126
133
- Default: ` { includeLevel: [2, 3] } `
127
134
128
- Options for [ markdown-it-table-of-contents] ( https://github.com/Oktavilla/markdown-it-table-of-contents ) .
135
+ Options for [ markdown-it-table-of-contents] ( https://github.com/Oktavilla/markdown-it-table-of-contents ) . (Note: prefer ` markdown.slugify ` if you want to customize header ids.)
129
136
130
137
### markdown.config
131
138
Original file line number Diff line number Diff line change @@ -7,8 +7,12 @@ const containers = require('./containers')
7
7
const emoji = require ( 'markdown-it-emoji' )
8
8
const anchor = require ( 'markdown-it-anchor' )
9
9
const toc = require ( 'markdown-it-table-of-contents' )
10
+ const _slugify = require ( './slugify' )
11
+
12
+ module . exports = ( { markdown = { } } = { } ) => {
13
+ // allow user config slugify
14
+ const slugify = markdown . slugify || _slugify
10
15
11
- module . exports = ( { markdown = { } } ) => {
12
16
const md = require ( 'markdown-it' ) ( {
13
17
html : true ,
14
18
typographer : true ,
@@ -24,11 +28,13 @@ module.exports = ({ markdown = {}}) => {
24
28
// 3rd party plugins
25
29
. use ( emoji )
26
30
. use ( anchor , Object . assign ( {
31
+ slugify,
27
32
permalink : true ,
28
33
permalinkBefore : true ,
29
34
permalinkSymbol : '#'
30
35
} , markdown . anchor ) )
31
36
. use ( toc , Object . assign ( {
37
+ slugify,
32
38
includeLevel : [ 2 , 3 ]
33
39
} , markdown . toc ) )
34
40
@@ -48,5 +54,8 @@ module.exports = ({ markdown = {}}) => {
48
54
}
49
55
}
50
56
57
+ // expose slugify
58
+ md . slugify = slugify
59
+
51
60
return md
52
61
}
Original file line number Diff line number Diff line change
1
+ // string.js slugify drops non ascii chars so we have to
2
+ // use a custom implementation here
3
+ const removeDiacritics = require ( 'diacritics' ) . remove
4
+ // eslint-disable-next-line no-control-regex
5
+ const rControl = / [ \u0000 - \u001f ] / g
6
+ const rSpecial = / [ \s ~ ` ! @ # $ % ^ & * ( ) \- _ + = [ \] { } | \\ ; : " ' < > , . ? / ] + / g
7
+
8
+ module . exports = function slugify ( str ) {
9
+ return removeDiacritics ( str )
10
+ // Remove control characters
11
+ . replace ( rControl , '' )
12
+ // Replace special characters
13
+ . replace ( rSpecial , '-' )
14
+ // Remove continous separators
15
+ . replace ( / \- { 2 , } / g, '-' )
16
+ // Remove prefixing and trailing separtors
17
+ . replace ( / ^ \- + | \- + $ / g, '' )
18
+ // lowercase
19
+ . toLowerCase ( )
20
+ }
Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ exports.parseFrontmatter = content => {
45
45
46
46
const LRU = require ( 'lru-cache' )
47
47
const cache = LRU ( { max : 1000 } )
48
+ const md = require ( '../markdown' ) ( )
48
49
49
50
exports . extractHeaders = ( content , include = [ ] ) => {
50
51
const key = content + include . join ( ',' )
@@ -53,8 +54,6 @@ exports.extractHeaders = (content, include = []) => {
53
54
return hit
54
55
}
55
56
56
- const md = require ( './markdown' ) ( { } )
57
- const S = require ( 'string' )
58
57
const tokens = md . parse ( content , { } )
59
58
60
59
const res = [ ]
@@ -64,7 +63,7 @@ exports.extractHeaders = (content, include = []) => {
64
63
res . push ( {
65
64
level : parseInt ( t . tag . slice ( 1 ) , 10 ) ,
66
65
title,
67
- slug : S ( title ) . slugify ( ) . s
66
+ slug : md . slugify ( title )
68
67
} )
69
68
}
70
69
} )
Original file line number Diff line number Diff line change 50
50
"connect-history-api-fallback" : " ^1.5.0" ,
51
51
"copy-webpack-plugin" : " ^4.5.1" ,
52
52
"css-loader" : " ^0.28.11" ,
53
+ "diacritics" : " ^1.3.0" ,
53
54
"es6-promise" : " ^4.2.4" ,
54
55
"escape-html" : " ^1.0.3" ,
55
56
"file-loader" : " ^1.1.11" ,
Original file line number Diff line number Diff line change @@ -1381,6 +1381,10 @@ detect-libc@^1.0.2:
1381
1381
version "1.0.3"
1382
1382
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
1383
1383
1384
+ diacritics@^1.3.0 :
1385
+ version "1.3.0"
1386
+ resolved "https://registry.yarnpkg.com/diacritics/-/diacritics-1.3.0.tgz#3efa87323ebb863e6696cebb0082d48ff3d6f7a1"
1387
+
1384
1388
diff@^3.2.0 :
1385
1389
version "3.5.0"
1386
1390
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
You can’t perform that action at this time.
0 commit comments