mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
* Don't replace leading digit * Install css.escape * Escape leading digit of the anchor id
37 lines
644 B
JavaScript
37 lines
644 B
JavaScript
import {hasOwn} from '../util/core'
|
|
|
|
let cache = {}
|
|
const re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g
|
|
|
|
function lower(string) {
|
|
return string.toLowerCase()
|
|
}
|
|
|
|
export function slugify(str) {
|
|
if (typeof str !== 'string') {
|
|
return ''
|
|
}
|
|
|
|
let slug = str
|
|
.trim()
|
|
.replace(/[A-Z]+/g, lower)
|
|
.replace(/<[^>\d]+>/g, '')
|
|
.replace(re, '')
|
|
.replace(/\s/g, '-')
|
|
.replace(/-+/g, '-')
|
|
let count = cache[slug]
|
|
|
|
count = hasOwn.call(cache, slug) ? count + 1 : 0
|
|
cache[slug] = count
|
|
|
|
if (count) {
|
|
slug = slug + '-' + count
|
|
}
|
|
|
|
return slug
|
|
}
|
|
|
|
slugify.clear = function () {
|
|
cache = {}
|
|
}
|