Skip to content

Commit

Permalink
fix($shared-utils): replace diacritics with regex (#1855)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Larionov committed Aug 19, 2020
1 parent ede84fe commit b35e451
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/@vuepress/shared-utils/__tests__/slugify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import slugify from '../src/slugify'

describe('slugify', () => {
test('should slugify', () => {
const asserts: Record<string, string> = {
'Привет': 'привет',
'Лед üäöß': 'лед-uaoß',
'hangul 가': 'hangul-가',
'ع': 'ع',
'džℍΩ': 'dzhω',
'カi⁹': 'カi9',
// ㌀ -> アパート'
'㌀': decodeURIComponent('%E3%82%A2%E3%83%8F%E3%82%9A%E3%83%BC%E3%83%88'),
'¼': '_1⁄4',
'džℍΩカi⁹¼': 'dzhωカi91⁄4',
'Iлtèrnåtïonɑlíƶatï߀ԉ': 'iлternationɑliƶati߀ԉ',
'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òлɢùê.':
'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',
'ᴎᴑᴅᴇȷʂ': 'ᴎᴑᴅᴇȷʂ',
'hambúrguer': 'hamburguer',
'hŒllœ': 'hœllœ',
'Fußball': 'fußball',
'ABCDEFGHIJKLMNOPQRSTUVWXYZé': 'abcdefghijklmnopqrstuvwxyze'
}

Object.keys(asserts).forEach(input => {
expect(slugify(input)).toBe(asserts[input])
})
})
})
9 changes: 6 additions & 3 deletions packages/@vuepress/shared-utils/src/slugify.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// string.js slugify drops non ascii chars so we have to
// use a custom implementation here
import { remove as removeDiacritics } from 'diacritics'

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

export = function slugify (str: string): string {
return removeDiacritics(str)
// Remove control characters
// Split accented characters into components
return str.normalize('NFKD')
// Remove accents
.replace(rCombining, '')
// Remove control characters
.replace(rControl, '')
// Replace special characters
.replace(rSpecial, '-')
Expand Down

0 comments on commit b35e451

Please sign in to comment.