mirror of
https://github.com/inexorabletash/jsbasic.git
synced 2025-02-19 09:30:46 +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 = [];
|
var html = [];
|
||||||
el.innerHTML = "";
|
el.innerHTML = "";
|
||||||
|
|
||||||
var level = 1, unique = 10000;
|
var level = 1, unique = 1;
|
||||||
[].forEach.call(document.querySelectorAll('h2, h3'), function(c) {
|
[].forEach.call(document.querySelectorAll('h2, h3'), function(c) {
|
||||||
var newLevel = parseInt(c.nodeName.charAt(1), 10);
|
var newLevel = parseInt(c.nodeName.charAt(1), 10);
|
||||||
if (newLevel > level) {
|
if (newLevel > level) {
|
||||||
@ -617,11 +617,18 @@ function which implements the logic for walking over the array.<p>
|
|||||||
}
|
}
|
||||||
level = newLevel;
|
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>');
|
html.push('<li><a href="#' + key + '">' + c.innerHTML + '</a>');
|
||||||
|
|
||||||
c.setAttribute('id', key);
|
c.id = key;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user