2022.2

Table Of Contents
1. Addthefollowingvariable:var linknumber = 1;.Thisvariableisusedtogenerateunique
IDsforelementsthatdon'thaveanID.
2. Beforethefirstlinethatstartswith'toc',addthefollowing:
var anchorId = '';
if (this.attr("id")) {
anchorId = this.attr("id");
}
else {
anchorId = 'generatedID-' + linknumber;
linknumber++;
this.attr("id", anchorId);
}
Thiscodetakestheelement'sID.Ifanelementdoesn'thaveanID,thescriptgeneratesanew,
uniqueIDforit.
3. Replacetheline:
toc += '<span class="text">' + text + '</span>';
withthisline:
toc += '<a class="text" href="#' + anchorId + '">' + text + '</a>';
4. Thenewlineaddsahyperlink:<a...href>withtheelement'sIDtothetableofcontents.
Step 4: Styling the table of contents
EachcomponentofthetableofcontentsinsertedinStep3iswrappedina<span>elementthathasa
class,forexample:"dots"orevenmultipleclasses:"toc-entryh1".(Classnamesareseparatedbya
space.)
TheseclassescanbeusedtostylethetableofcontentswithCSS.TheprinciplesofstylingwithCSS
areexplainedinanothertopic:"StylingtemplateswithCSSfiles"onpage672.
AddthefollowingstylestothestylesheetforthePrintcontext(see"Step1:editCSS"onpage676)to
alignthepagenumbertotherightandfillthespacebetweenthetextandthepagenumberwithleader
dots.
p.toc-entry {
display: flex;
margin: 0 0 0.25em 0;
}
p.toc-entry .text {
flex-shrink: 0;
}
p.toc-entry .dots {
flex-shrink: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: clip;
}
p.toc-entry .dots::after {
Page 855