Skip to content

Commit 86e9d40

Browse files
committed
docs: better slugify
1 parent 98e9af6 commit 86e9d40

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/docs/.vitepress/config/shared.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ if (process.env.NETLIFY) {
1010
console.log('Netlify build', process.env.CONTEXT)
1111
}
1212

13+
const rControl = /[\u0000-\u001f]/g
14+
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g
15+
const rCombining = /[\u0300-\u036F]/g
16+
17+
/**
18+
* Default slugification function
19+
*/
20+
export const slugify = (str: string): string =>
21+
str
22+
.normalize('NFKD')
23+
// Remove accents
24+
.replace(rCombining, '')
25+
// Remove control characters
26+
.replace(rControl, '')
27+
// Replace special characters
28+
.replace(rSpecial, '-')
29+
// ensure it doesn't start with a number
30+
.replace(/^(\d)/, '_$1')
31+
1332
const productionHead: HeadConfig[] = [
1433
[
1534
'script',
@@ -37,7 +56,7 @@ export const sharedConfig = defineConfig({
3756
},
3857

3958
anchor: {
40-
slugify: s => s.replace(/\s/g, '-'),
59+
slugify,
4160
},
4261
},
4362

0 commit comments

Comments
 (0)