aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-01-25 17:17:34 +0000
committerMichael Williamson <mike@zwobble.org>2015-01-25 17:17:34 +0000
commit52c4d01e4a0e35738e8c1db35a5a4a7ab981803d (patch)
tree0cce833e77942311d25a7c92828d7f5802be14b5
parent2de4641bed73f04d0c1ac345e132c1a79fc48f14 (diff)
Use prefix to generate IDs
-rw-r--r--lib/document-to-html.js27
-rw-r--r--lib/documents.js4
-rw-r--r--lib/docx/document-xml-reader.js2
-rw-r--r--tests/document-to-html.tests.js12
-rw-r--r--tests/docx/document-xml-reader.tests.js2
-rw-r--r--tests/mammoth.tests.js20
6 files changed, 33 insertions, 34 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index eec329f..b012ea9 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -33,10 +33,9 @@ function DocumentConversion(options) {
var noteReferences = [];
options = options || {};
- var generateUniquifier = options.generateUniquifier || function() {
- return Math.floor(Math.random() * 1000000000000000);
- };
- var uniquifier = generateUniquifier();
+ var idPrefix = options.idPrefix === undefined ?
+ Math.floor(Math.random() * 1000000000000000) :
+ options.idPrefix;
var defaultParagraphStyle = htmlPaths.topLevelElement("p");
@@ -140,8 +139,12 @@ function DocumentConversion(options) {
});
});
- function noteUid(id) {
- return uniquifier + "-" + id;
+ function noteHtmlId(note) {
+ return idPrefix + "-" + note.noteType + "-" + note.noteId;
+ }
+
+ function noteRefHtmlId(note) {
+ return idPrefix + "-" + note.noteType + "-ref-" + note.noteId;
}
function convertTable(element, html, messages, callback) {
@@ -235,12 +238,10 @@ function DocumentConversion(options) {
});
},
"noteReference": function(element, html, messages, callback) {
- var uid = noteUid(element.noteId);
-
html.open(htmlPaths.element("sup"));
html.open(htmlPaths.element("a", {
- href: "#" + element.noteType + "-" + uid,
- id: element.noteType + "-ref-" + uid
+ href: "#" + noteHtmlId(element),
+ id: noteRefHtmlId(element)
}));
noteReferences.push(element);
html.text("[" + (noteNumber++) + "]");
@@ -249,16 +250,14 @@ function DocumentConversion(options) {
callback();
},
"note": function(element, html, messages, callback) {
- var uid = noteUid(element.id);
-
- html.open(htmlPaths.element("li", {id: element.noteType + "-" + uid}));
+ html.open(htmlPaths.element("li", {id: noteHtmlId(element)}));
var footnoteHtml = new HtmlGenerator();
convertElements(element.body, footnoteHtml, messages, function(err) {
if (err) {
callback(err);
} else {
footnoteHtml.text(" ");
- footnoteHtml.open(htmlPaths.element("a", {href: "#" + element.noteType + "-ref-" + uid}));
+ footnoteHtml.open(htmlPaths.element("a", {href: "#" + noteRefHtmlId(element)}));
footnoteHtml.text("↑");
footnoteHtml.closeAll();
html.append(footnoteHtml);
diff --git a/lib/documents.js b/lib/documents.js
index 5ec8aaf..78bea1a 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -88,7 +88,7 @@ function NoteReference(options) {
function Notes(notes) {
this._notes = _.indexBy(notes, function(note) {
- return noteKey(note.noteType, note.id);
+ return noteKey(note.noteType, note.noteId);
});
}
@@ -104,7 +104,7 @@ function Note(options) {
return {
type: types.note,
noteType: options.noteType,
- id: options.id,
+ noteId: options.noteId,
body: options.body
};
}
diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js
index 1d14758..12016b0 100644
--- a/lib/docx/document-xml-reader.js
+++ b/lib/docx/document-xml-reader.js
@@ -34,7 +34,7 @@ function DocumentXmlReader(options) {
return readXmlElements(rawNote.body).map(function(body) {
return new documents.Note({
noteType: rawNote.noteType,
- id: rawNote.id,
+ noteId: rawNote.id,
body: body
});
});
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index 63b445d..17fdb27 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -290,10 +290,10 @@ describe('DocumentConverter', function() {
noteId: "4"
});
var converter = new DocumentConverter({
- generateUniquifier: function() { return 42; }
+ idPrefix: "doc-42"
});
return converter.convertToHtml(footnoteReference).then(function(result) {
- assert.equal(result.value, '<sup><a href="#footnote-42-4" id="footnote-ref-42-4">[1]</a></sup>');
+ assert.equal(result.value, '<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup>');
});
});
@@ -311,7 +311,7 @@ describe('DocumentConverter', function() {
notes: new documents.Notes({
4: new documents.Note({
noteType: "footnote",
- id: "4",
+ noteId: "4",
body: [paragraphOfText("Who's there?")]
})
})
@@ -319,11 +319,11 @@ describe('DocumentConverter', function() {
);
var converter = new DocumentConverter({
- generateUniquifier: function() { return 42; }
+ idPrefix: "doc-42"
});
return converter.convertToHtml(document).then(function(result) {
- var expectedOutput = '<p>Knock knock<sup><a href="#footnote-42-4" id="footnote-ref-42-4">[1]</a></sup></p>' +
- '<ol><li id="footnote-42-4"><p>Who\'s there? <a href="#footnote-ref-42-4">↑</a></p></li></ol>';
+ var expectedOutput = '<p>Knock knock<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup></p>' +
+ '<ol><li id="doc-42-footnote-4"><p>Who\'s there? <a href="#doc-42-footnote-ref-4">↑</a></p></li></ol>';
assert.equal(result.value, expectedOutput);
});
});
diff --git a/tests/docx/document-xml-reader.tests.js b/tests/docx/document-xml-reader.tests.js
index 86c0fcb..3350917 100644
--- a/tests/docx/document-xml-reader.tests.js
+++ b/tests/docx/document-xml-reader.tests.js
@@ -362,7 +362,7 @@ describe("convertXmlToDocument: ", function() {
var document = convertXmlToDocumentValue({root: documentXml}, {footnotes: footnotes});
var footnote = document.notes.resolve({noteType: "footnote", noteId: "4"});
- assert.deepEqual(footnote.id, "4");
+ assert.deepEqual(footnote.noteId, "4");
assert.deepEqual(footnote.body[0].type, "paragraph");
});
});
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index 48ce680..985dd99 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -150,14 +150,14 @@ describe('mammoth', function() {
// TODO: don't duplicate footnotes with multiple references
var docxPath = path.join(__dirname, "test-data/footnotes.docx");
var options = {
- generateUniquifier: function() { return 42; }
+ idPrefix: "doc-42"
};
return mammoth.convertToHtml({path: docxPath}, options).then(function(result) {
var expectedOutput = '<p>Ouch' +
- '<sup><a href="#footnote-42-1" id="footnote-ref-42-1">[1]</a></sup>.' +
- '<sup><a href="#footnote-42-2" id="footnote-ref-42-2">[2]</a></sup></p>' +
- '<ol><li id="footnote-42-1"><p> A tachyon walks into a bar. <a href="#footnote-ref-42-1">↑</a></p></li>' +
- '<li id="footnote-42-2"><p> Fin. <a href="#footnote-ref-42-2">↑</a></p></li></ol>';
+ '<sup><a href="#doc-42-footnote-1" id="doc-42-footnote-ref-1">[1]</a></sup>.' +
+ '<sup><a href="#doc-42-footnote-2" id="doc-42-footnote-ref-2">[2]</a></sup></p>' +
+ '<ol><li id="doc-42-footnote-1"><p> A tachyon walks into a bar. <a href="#doc-42-footnote-ref-1">↑</a></p></li>' +
+ '<li id="doc-42-footnote-2"><p> Fin. <a href="#doc-42-footnote-ref-2">↑</a></p></li></ol>';
assert.equal(result.value, expectedOutput);
assert.deepEqual(result.messages, []);
});
@@ -166,14 +166,14 @@ describe('mammoth', function() {
test('endnotes are appended to text', function() {
var docxPath = path.join(__dirname, "test-data/endnotes.docx");
var options = {
- generateUniquifier: function() { return 42; }
+ idPrefix: "doc-42"
};
return mammoth.convertToHtml({path: docxPath}, options).then(function(result) {
var expectedOutput = '<p>Ouch' +
- '<sup><a href="#endnote-42-2" id="endnote-ref-42-2">[1]</a></sup>.' +
- '<sup><a href="#endnote-42-3" id="endnote-ref-42-3">[2]</a></sup></p>' +
- '<ol><li id="endnote-42-2"><p> A tachyon walks into a bar. <a href="#endnote-ref-42-2">↑</a></p></li>' +
- '<li id="endnote-42-3"><p> Fin. <a href="#endnote-ref-42-3">↑</a></p></li></ol>';
+ '<sup><a href="#doc-42-endnote-2" id="doc-42-endnote-ref-2">[1]</a></sup>.' +
+ '<sup><a href="#doc-42-endnote-3" id="doc-42-endnote-ref-3">[2]</a></sup></p>' +
+ '<ol><li id="doc-42-endnote-2"><p> A tachyon walks into a bar. <a href="#doc-42-endnote-ref-2">↑</a></p></li>' +
+ '<li id="doc-42-endnote-3"><p> Fin. <a href="#doc-42-endnote-ref-3">↑</a></p></li></ol>';
assert.equal(result.value, expectedOutput);
assert.deepEqual(result.messages, []);
});