2019.2

Table Of Contents
// Build the TOC
var toc = '';
merge.context.query("h1, h2").each(function() {
var pageNo = this.info().pageNo;
var text = this.text();
var level = this.tagName();
toc += '<p class="toc-entry ' + level + '">';
toc += '<span class="text">' + text + '</span>';
toc += '<span class="dots"></span>';
toc += '<span class="number">' + pageNo + '</span></p>';
});
results.html( toc );
// Repaginate the result
merge.section.paginate();
// Update the page numbers
var $numbers = query('.number');
merge.context.query("h1, h2").each(function( index ) {
var pageNo = this.info().pageNo;
var entry = $numbers.get( index );
if( entry ) {
entry.text( pageNo );
}
});
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 page1388).
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1327).
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();
Page 903