2021.2

Table Of Contents
What the script does
First the script creates a variable to hold the table of contents: toc.
Then it collects all <h1> and <h2> elements - in other words, level 1 and 2 headings. The
merge.context.query(selector) function searches across all Print sections (see "query
(selector)" on page1446).
The query returns a result set. Each of the elements in the result set goes through the callback
function defined in each() (see "each()" on page1285).
The callback function gets the element's page number, text and HTMLtag name:
var pageNo = this.info().pageNo;
var text = this.text();
var level = this.tagName();
Note that the info() function can also be used to get an element's sheet number, the section it is
located in, and the total page count and sheet count of that section (see "PaginationInfo" on
page1447 and "info()" on page1452). In this case only the page number is used.
Then the callback function adds an entry to the variable that holds the table of contents, using
the retrieved info.
toc += '<p class="toc-entry ' + level + '">';
toc += '<span class="text">' + text + '</span>';
toc += '<span class="dots"></span>';
toc += '<span class="number">' + pageNo + '</span></p>';
The HTML tag name is added as a class. This can be used in a CSS file to style the entries in
the table of contents according to their level. (See "Step 4: Styling the table of contents" on
page935.)
The empty span between the heading's text and page number has the class dots. This is used
to put dots between heading and page number.
The number class (for the page number) is not only used in CSS, but also later on in the script.
The table of contents is inserted in the section with: results.html(toc ); (see "html()" on
page1388).
The table of contents may get too long for a single page and affect the page numbers in other
sections. In that case it is necessary to re-paginate the content; merge.section.paginate(); does
the trick.
Page 933