mirror of
https://github.com/inexorabletash/jsbasic.git
synced 2024-11-27 12:49:56 +00:00
More sensible ID generation for TOC
This commit is contained in:
parent
d5bd6ef417
commit
de7a258471
@ -607,7 +607,7 @@ function which implements the logic for walking over the array.<p>
|
||||
var html = [];
|
||||
el.innerHTML = "";
|
||||
|
||||
var level = 1, unique = 10000;
|
||||
var level = 1, unique = 1;
|
||||
[].forEach.call(document.querySelectorAll('h2, h3'), function(c) {
|
||||
var newLevel = parseInt(c.nodeName.charAt(1), 10);
|
||||
if (newLevel > level) {
|
||||
@ -617,11 +617,18 @@ function which implements the logic for walking over the array.<p>
|
||||
}
|
||||
level = newLevel;
|
||||
|
||||
var key = 'key_' + unique++;
|
||||
function makeID(string) {
|
||||
var id = String(string).replace(/[^a-zA-Z0-9]/g, '-');
|
||||
while (document.getElementById(id))
|
||||
id += '_' + unique++;
|
||||
id = id.replace(/-+/g, '-');
|
||||
return id;
|
||||
}
|
||||
var key = makeID(c.textContent);
|
||||
|
||||
html.push('<li><a href="#' + key + '">' + c.innerHTML + '</a>');
|
||||
|
||||
c.setAttribute('id', key);
|
||||
c.id = key;
|
||||
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user