feat: slugify filter from Jekyll, #443

This commit is contained in:
Yang Jun
2024-05-13 23:54:07 +08:00
committed by Jun Yang
parent 50253a98ca
commit 47ddc1193b
8 changed files with 215 additions and 4 deletions
+41
View File
@@ -8,3 +8,44 @@ export const cgi_escape = (x: string) => encodeURIComponent(stringify(x))
export const uri_escape = (x: string) => encodeURI(stringify(x))
.replace(/%5B/g, '[')
.replace(/%5D/g, ']')
const rSlugifyDefault = /[^\p{M}\p{L}\p{Nd}]+/ug
const rSlugifyReplacers = {
'raw': /\s+/g,
'default': rSlugifyDefault,
'pretty': /[^\p{M}\p{L}\p{Nd}._~!$&'()+,;=@]+/ug,
'ascii': /[^A-Za-z0-9]+/g,
'latin': rSlugifyDefault,
'none': null
}
export function slugify (str: string, mode: keyof typeof rSlugifyReplacers = 'default', cased = false): string {
str = stringify(str)
const replacer = rSlugifyReplacers[mode]
if (replacer) {
if (mode === 'latin') str = removeAccents(str)
str = str.replace(replacer, '-').replace(/^-|-$/g, '')
}
return cased ? str : str.toLowerCase()
}
function removeAccents (str: string): string {
return str.replace(/[àáâãäå]/g, 'a')
.replace(/[æ]/g, 'ae')
.replace(/[ç]/g, 'c')
.replace(/[èéêë]/g, 'e')
.replace(/[ìíîï]/g, 'i')
.replace(/[ð]/g, 'd')
.replace(/[ñ]/g, 'n')
.replace(/[òóôõöø]/g, 'o')
.replace(/[ùúûü]/g, 'u')
.replace(/[ýÿ]/g, 'y')
.replace(/[ß]/g, 'ss')
.replace(/[œ]/g, 'oe')
.replace(/[þ]/g, 'th')
.replace(/[ẞ]/g, 'SS')
.replace(/[Œ]/g, 'OE')
.replace(/[Þ]/g, 'TH')
}