From b35e451a2428e24e82ae3c500283a4e83e76cc8e Mon Sep 17 00:00:00 2001 From: Sergey Larionov Date: Wed, 19 Aug 2020 11:10:43 +0700 Subject: [PATCH] fix($shared-utils): replace diacritics with regex (#1855) --- .../shared-utils/__tests__/slugify.spec.ts | 30 +++++++++++++++++++ .../@vuepress/shared-utils/src/slugify.ts | 9 ++++-- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 packages/@vuepress/shared-utils/__tests__/slugify.spec.ts diff --git a/packages/@vuepress/shared-utils/__tests__/slugify.spec.ts b/packages/@vuepress/shared-utils/__tests__/slugify.spec.ts new file mode 100644 index 0000000000..517fea6cb5 --- /dev/null +++ b/packages/@vuepress/shared-utils/__tests__/slugify.spec.ts @@ -0,0 +1,30 @@ +import slugify from '../src/slugify' + +describe('slugify', () => { + test('should slugify', () => { + const asserts: Record = { + 'Привет': 'привет', + 'Лед üäöß': 'лед-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]) + }) + }) +}) diff --git a/packages/@vuepress/shared-utils/src/slugify.ts b/packages/@vuepress/shared-utils/src/slugify.ts index 1a1c8dbb12..05883e0072 100644 --- a/packages/@vuepress/shared-utils/src/slugify.ts +++ b/packages/@vuepress/shared-utils/src/slugify.ts @@ -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, '-')