2018.2

Table Of Contents
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 the
next page.)
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 "Examples" on
page1127).
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.
Note
Whether page numbering restarts in each section, depends on the settings for page numbering; see
"Configuring page numbers" on page421. By default, page numbering starts with page 1 for each
section.
If the pagination process has changed the page numbers, the TOC needs to be updated as
well.
To do that, the script first has to collect the page numbers from the table of contents. This is
where the number class comes in handy: var $numbers = query('.number');.
Note that this query() function, as opposed to merge.context.query(), only searches the
current section (see "query()" on page1158).
Then, the level 1 and 2 headings are collected from all Print sections again using
merge.context.query("h1, h2").
The callback function in each() retrieves the heading's new page number. It then uses the
index number of the heading in the result set to get the corresponding entry in the TOC: var
entry = $numbers.get( index ); (see get(index)), and replaces it with the new page number.
Page 770