mirror of
https://github.com/marktext/marktext.git
synced 2025-05-03 00:01:19 +08:00
36 lines
735 B
JavaScript
36 lines
735 B
JavaScript
/**
|
|
* Slugger generates header id
|
|
*/
|
|
|
|
function Slugger () {
|
|
this.seen = {}
|
|
}
|
|
|
|
/**
|
|
* Convert string to unique id
|
|
*/
|
|
|
|
Slugger.prototype.slug = function (value) {
|
|
let slug = value
|
|
.toLowerCase()
|
|
.trim()
|
|
// remove html tags
|
|
.replace(/<[!\/a-z].*?>/ig, '') // eslint-disable-line no-useless-escape
|
|
// remove unwanted chars
|
|
.replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
|
|
.replace(/\s/g, '-')
|
|
|
|
if (this.seen.hasOwnProperty(slug)) {
|
|
const originalSlug = slug
|
|
do {
|
|
this.seen[originalSlug]++
|
|
slug = originalSlug + '-' + this.seen[originalSlug]
|
|
} while (this.seen.hasOwnProperty(slug))
|
|
}
|
|
this.seen[slug] = 0
|
|
|
|
return slug
|
|
}
|
|
|
|
export default Slugger
|