Skip to content

Commit b35e451

Browse files
fix($shared-utils): replace diacritics with regex (#1855)
1 parent ede84fe commit b35e451

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import slugify from '../src/slugify'
2+
3+
describe('slugify', () => {
4+
test('should slugify', () => {
5+
const asserts: Record<string, string> = {
6+
'Привет': 'привет',
7+
'Лед üäöß': 'лед-uaoß',
8+
'hangul 가': 'hangul-가',
9+
'ع': 'ع',
10+
'džℍΩ': 'dzhω',
11+
'カi⁹': 'カi9',
12+
// ㌀ -> アパート'
13+
'㌀': decodeURIComponent('%E3%82%A2%E3%83%8F%E3%82%9A%E3%83%BC%E3%83%88'),
14+
'¼': '_1⁄4',
15+
'džℍΩカi⁹¼': 'dzhωカi91⁄4',
16+
'Iлtèrnåtïonɑlíƶatï߀ԉ': 'iлternationɑliƶati߀ԉ',
17+
'Båcòл ípѕùm ðoɭ߀r ѕït aϻèt âùþê aԉᏧ߀üïlɭê ƃëéf culρá fïlèt ϻiǥnòn cuρiᏧatat ut êлim tòлɢùê.':
18+
'bacoл-ipѕum-ðoɭ߀r-ѕit-aϻet-auþe-aԉꮷ߀uilɭe-ƃeef-culρa-filet-ϻiǥnon-cuρiꮷatat-ut-eлim-toлɢue',
19+
'ᴎᴑᴅᴇȷʂ': 'ᴎᴑᴅᴇȷʂ',
20+
'hambúrguer': 'hamburguer',
21+
'hŒllœ': 'hœllœ',
22+
'Fußball': 'fußball',
23+
'ABCDEFGHIJKLMNOPQRSTUVWXYZé': 'abcdefghijklmnopqrstuvwxyze'
24+
}
25+
26+
Object.keys(asserts).forEach(input => {
27+
expect(slugify(input)).toBe(asserts[input])
28+
})
29+
})
30+
})

packages/@vuepress/shared-utils/src/slugify.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// string.js slugify drops non ascii chars so we have to
22
// use a custom implementation here
3-
import { remove as removeDiacritics } from 'diacritics'
43

54
// eslint-disable-next-line no-control-regex
65
const rControl = /[\u0000-\u001f]/g
76
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g
7+
const rCombining = /[\u0300-\u036F]/g
88

99
export = function slugify (str: string): string {
10-
return removeDiacritics(str)
11-
// Remove control characters
10+
// Split accented characters into components
11+
return str.normalize('NFKD')
12+
// Remove accents
13+
.replace(rCombining, '')
14+
// Remove control characters
1215
.replace(rControl, '')
1316
// Replace special characters
1417
.replace(rSpecial, '-')

0 commit comments

Comments
 (0)