-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathmake-ids.js
More file actions
24 lines (22 loc) · 655 Bytes
/
make-ids.js
File metadata and controls
24 lines (22 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* eslint no-var: off */
function makeIds() {
// eslint-disable-line
var content = document.querySelector(".js-toc-content")
var headings = content.querySelectorAll("h1, h2, h3, h4, h5, h6, h7")
var headingMap = {}
Array.prototype.forEach.call(headings, function (heading) {
var id = heading.id
? heading.id
: heading.innerText
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-]/g, "")
headingMap[id] = !isNaN(headingMap[id]) ? ++headingMap[id] : 0
if (headingMap[id]) {
heading.id = id + "-" + headingMap[id]
} else {
heading.id = id
}
})
}