1
0
mirror of https://github.com/andre-wojtowicz/elearning-ims-cp-template.git synced 2024-11-27 16:20:27 +01:00
elearning-ims-cp-template/materials/js/auto-toc.js

38 lines
911 B
JavaScript

//view-source:magnetiq.com/exports/toc.htm
autoTOC = function () {
var toc = "";
var level = 0;
document.body.innerHTML =
document.body.innerHTML.replace(
/<h([\d])>([^<]+)<\/h([\d])>/gi,
function (str, openLevel, titleText, closeLevel) {
if (openLevel != closeLevel) {
return str;
}
if (openLevel > level) {
toc += (new Array(openLevel - level + 1)).join("<ol>");
} else if (openLevel < level) {
toc += (new Array(level - openLevel + 1)).join("</ol>");
}
level = parseInt(openLevel);
var anchor = titleText.replace(/ /g, "_");
toc += "<li><a href=\"#" + anchor + "\">" + titleText
+ "</a></li>";
return "<h" + openLevel + " id=\"" + anchor + "\">"
+ titleText + "</h" + closeLevel + ">";
}
);
if (level) {
toc += (new Array(level + 1)).join("</ul>");
}
document.getElementById("toc").innerHTML += toc;
};
autoTOC();