Skip to content

Commit

Permalink
fix($shared-utils): use String.normalize to generate slugs (vuejs#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
larionov committed Sep 12, 2019
1 parent c82cc10 commit a3ce114
Show file tree
Hide file tree
Showing 4 changed files with 3,507 additions and 2,612 deletions.
22 changes: 22 additions & 0 deletions packages/@vuepress/shared-utils/__tests__/slugify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import slugify from '../src/slugify'

describe('slugify', () => {
test('should slugify', () => {
const asserts: Record<string, string> = {
'Привет': 'привет',
'Лед üäöß': 'лед-uaoß',
'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])
})
})
})
1 change: 0 additions & 1 deletion packages/@vuepress/shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
],
"dependencies": {
"chalk": "^2.3.2",
"diacritics": "^1.3.0",
"escape-html": "^1.0.3",
"fs-extra": "^5.0.0",
"globby": "^8.0.1",
Expand Down
9 changes: 6 additions & 3 deletions packages/@vuepress/shared-utils/src/slugify.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// string.js slugify drops non ascii chars so we have to
// use a custom implementation here
// @ts-ignore
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
Loading

0 comments on commit a3ce114

Please sign in to comment.