docsify/src/core/render/slugify.js
Atsushi Tanaka 90a35ab952
Fix #895 (#896)
* Don't replace leading digit

* Install css.escape

* Escape leading digit of the anchor id
2020-02-05 22:23:05 +05:30

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 = {}
}