Skip to content

Commit e81eedd

Browse files
committed
add cross references
1 parent 6b136a5 commit e81eedd

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

book.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
* following format:
8383
*
8484
* "Table chapter.number"
85+
*
86+
* enableCrossReferences: true -- This converts the inner part all internal
87+
* links within the flow element into the page number of the page the referred
88+
* element is placed on.
8589
*
8690
* bulkPagesToAdd: 50 -- This is the initial number of pages of each flowable
8791
* part (section, chapter). After this number is added, adjustments are made by
@@ -239,6 +243,7 @@
239243
'enableTableOfFigures': false,
240244
'enableTableOfTables': false,
241245
'enableMarginNotes': false,
246+
'enableCrossReferences': true,
242247
'bulkPagesToAdd': 50,
243248
'pagesToAddIncrementRatio': 1.4,
244249
'frontmatterContents': '',
@@ -889,6 +894,22 @@
889894
pagination.adjustMarginNotesPositionsPerPage(allMarginNoteContainers[i]);
890895
}
891896
};
897+
898+
pagination.findAllCrossReferences = function () {
899+
/* Find all links that point to places within the same document
900+
*/
901+
var flowElement = eval(pagination.config('flowElement')), allReferences = flowElement.querySelectorAll('a'), referredElement, pageNumber, i;
902+
903+
for (i=0; i < allReferences.length;i++) {
904+
if (allReferences[i].getAttribute('href')[0]==='#') {
905+
referredElement = flowElement.querySelector(allReferences[i].getAttribute('href'));
906+
pageNumber = pagination.findPage(referredElement).querySelector('.pagination-pagenumber').innerHTML;
907+
if (allReferences[i].innerHTML !== pageNumber) {
908+
allReferences[i].innerHTML = pageNumber;
909+
}
910+
}
911+
}
912+
};
892913

893914
pagination.headersAndToc = function (bodyObjects) {
894915
/* Go through all pages of all flowObjects and add page headers and
@@ -1114,10 +1135,13 @@
11141135
tot = pagination.tot(bodyObjects);
11151136
fmObject.rawdiv.replaceChild(tot, oldTot);
11161137
}
1138+
if (pagination.config('enableCrossReferences')) {
1139+
pagination.findAllCrossReferences();
1140+
}
11171141
};
11181142
document.body.addEventListener('bodyLayoutUpdated', function() {
11191143
// We have to set a time out of zero to make sure fonts have been applied, etc. before toc and tof are being calculated.
1120-
// TODO:mIdeally, this shouldn't be needed.
1144+
// TODO: Ideally, this shouldn't be needed.
11211145
setTimeout(redoToc, 0);
11221146

11231147
});
@@ -1127,6 +1151,9 @@
11271151
document.body.dispatchEvent(pagination.events.bodyLayoutUpdated);
11281152
});
11291153
}
1154+
if (pagination.config('enableCrossReferences')) {
1155+
pagination.findAllCrossReferences();
1156+
}
11301157
document.dispatchEvent(pagination.events.layoutFlowFinished);
11311158
};
11321159

test3.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h1>Some text part</h1>
3737
non habent claritatem insitam; est usus legentis in iis qui facit eorum
3838
claritatem. Investigationes demonstraverunt lectores legere me lius quod
3939
ii legunt saepius<span class="pagination-footnote"><span><span>And this is a third footnote.</span></span></span>.</div>
40-
<div>3 Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
40+
<div id="important">3 Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
4141
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut
4242
wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
4343
lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure
@@ -2127,7 +2127,7 @@ <h1>Some text part</h1>
21272127
</div>
21282128
<h1>Another text part</h1>
21292129
<div contenteditable="true">
2130-
<div>17 Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
2130+
<div>See page <a href="#important">SOMETHING</a> 17 Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
21312131
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
21322132
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
21332133
lobortis nisl ut aliquip ex ea commodo consequat<span class="pagination-footnote"><span><span>This is a footnote.</span></span></span>. Duis autem vel eum iriure

0 commit comments

Comments
 (0)