summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-06-11 14:48:39 +0100
committerMichael Williamson <mike@zwobble.org>2014-06-11 14:48:39 +0100
commitb4e617af3d447f7addd2901d9abaaceb1f9b1d9a (patch)
tree1d705bbf7776ad547b475e43c52611bfc07d4696
parent0af8351be2b11f5f912ef091c4ce0f117171d5d5 (diff)
Add uniquifier to footnote IDs
-rw-r--r--lib/document-to-html.js13
-rw-r--r--tests/document-to-html.tests.js14
-rw-r--r--tests/mammoth.tests.js11
3 files changed, 27 insertions, 11 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index bec48b0..d7b6a86 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -31,6 +31,11 @@ function DocumentConversion(options) {
var footnoteIds = [];
options = options || {};
+ var generateUniquifier = options.generateUniquifier || function() {
+ return Math.floor(Math.random() * 1000000000000000);
+ };
+ var uniquifier = generateUniquifier();
+
var defaultParagraphStyle = htmlPaths.topLevelElement("p");
var styleMap = options.styleMap || [];
@@ -128,6 +133,10 @@ function DocumentConversion(options) {
}).fail(callback);
}
+ function footnoteUid(id) {
+ return uniquifier + "-" + id;
+ }
+
var elementConverters = {
"document": function(document, html, messages, callback) {
convertElements(document.children, html, messages, function(error) {
@@ -173,7 +182,7 @@ function DocumentConversion(options) {
},
"footnoteReference": function(element, html, messages, callback) {
html.open(htmlPaths.element("sup"));
- html.open(htmlPaths.element("a", {href: "#footnote-" + element.footnoteId}));
+ html.open(htmlPaths.element("a", {href: "#footnote-" + footnoteUid(element.footnoteId)}));
footnoteIds.push(element.footnoteId);
html.text("[" + (footnoteNumber++) + "]");
html.close();
@@ -181,7 +190,7 @@ function DocumentConversion(options) {
callback();
},
"footnote": function(element, html, messages, callback) {
- html.open(htmlPaths.element("li", {id: "footnote-" + element.id}));
+ html.open(htmlPaths.element("li", {id: "footnote-" + footnoteUid(element.id)}));
var footnoteHtml = new HtmlGenerator();
convertElements(element.body, footnoteHtml, messages, function(err) {
if (err) {
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index a8cf0ab..51ee625 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -202,9 +202,11 @@ describe('DocumentConverter', function() {
footnoteId: "4",
body: paragraphOfText("Who's there?")
});
- var converter = new DocumentConverter();
+ var converter = new DocumentConverter({
+ generateUniquifier: function() { return 42; }
+ });
return converter.convertToHtml(footnoteReference).then(function(result) {
- assert.equal(result.value, '<sup><a href="#footnote-4">[1]</a></sup>');
+ assert.equal(result.value, '<sup><a href="#footnote-42-4">[1]</a></sup>');
});
});
@@ -227,10 +229,12 @@ describe('DocumentConverter', function() {
}
);
- var converter = new DocumentConverter();
+ var converter = new DocumentConverter({
+ generateUniquifier: function() { return 42; }
+ });
return converter.convertToHtml(document).then(function(result) {
- var expectedOutput = '<p>Knock knock<sup><a href="#footnote-4">[1]</a></sup></p>' +
- '<ol><li id="footnote-4"><p>Who\'s there?</p></li></ol>';
+ var expectedOutput = '<p>Knock knock<sup><a href="#footnote-42-4">[1]</a></sup></p>' +
+ '<ol><li id="footnote-42-4"><p>Who\'s there?</p></li></ol>';
assert.equal(result.value, expectedOutput);
});
});
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index 30fba34..b1bb04e 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -93,10 +93,13 @@ describe('mammoth', function() {
// TODO: generate links back to main body
// TODO: don't duplicate footnotes with multiple references
var docxPath = path.join(__dirname, "test-data/footnotes.docx");
- return mammoth.convertToHtml({path: docxPath}).then(function(result) {
- var expectedOutput = '<p>Ouch<sup><a href="#footnote-1">[1]</a></sup>.<sup><a href="#footnote-2">[2]</a></sup></p>' +
- '<ol><li id="footnote-1"><p> A tachyon walks into a bar.</p></li>' +
- '<li id="footnote-2"><p> Fin.</p></li></ol>'
+ var options = {
+ generateUniquifier: function() { return 42; }
+ };
+ return mammoth.convertToHtml({path: docxPath}, options).then(function(result) {
+ var expectedOutput = '<p>Ouch<sup><a href="#footnote-42-1">[1]</a></sup>.<sup><a href="#footnote-42-2">[2]</a></sup></p>' +
+ '<ol><li id="footnote-42-1"><p> A tachyon walks into a bar.</p></li>' +
+ '<li id="footnote-42-2"><p> Fin.</p></li></ol>'
assert.equal(result.value, expectedOutput);
// TODO: get rid of warnings
//~ assert.deepEqual(result.messages, []);