diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-06-10 15:40:05 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-06-10 15:40:05 +0100 |
| commit | 296ac78496e4d94dafd1a4f6f872a1283b796318 (patch) | |
| tree | 1d30666e971e63a5bf0e4008bf1a0abacde3d0be | |
| parent | 88b21ba0d0989a5c4f142f5971d33b069a7dfb26 (diff) | |
Pass all footnotes into document object
| -rw-r--r-- | lib/document-to-html.js | 10 | ||||
| -rw-r--r-- | lib/documents.js | 11 | ||||
| -rw-r--r-- | lib/docx/footnotes-reader.js | 10 | ||||
| -rw-r--r-- | tests/document-to-html.tests.js | 18 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 1 |
5 files changed, 30 insertions, 20 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index b984089..5b8db9a 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -28,6 +28,8 @@ function DocumentConverter(options) { function DocumentConversion(options) { var footnoteNumber = 1; + var footnoteIds = []; + options = options || {}; var defaultParagraphStyle = htmlPaths.topLevelElement("p"); @@ -73,7 +75,7 @@ function DocumentConversion(options) { return style.to; } else { if (element.styleId) { - messages.push(unrecognisedStyleWarning("paragraph", element));; + messages.push(unrecognisedStyleWarning("paragraph", element)); } return defaultParagraphStyle; } @@ -134,7 +136,10 @@ function DocumentConversion(options) { } else { html.closeAll(); html.open(htmlPaths.element("ol")); - convertElements(document.footnotes, html, messages, function(error) { + var footnotes = footnoteIds.map(function(id) { + return document.footnotes.findFootnoteById(id); + }); + convertElements(footnotes, html, messages, function(error) { if (error) { callback(error); } else { @@ -169,6 +174,7 @@ function DocumentConversion(options) { "footnoteReference": function(element, html, messages, callback) { html.open(htmlPaths.element("sup")); html.open(htmlPaths.element("a", {href: "#footnote-" + element.footnoteId})); + footnoteIds.push(element.footnoteId); html.text((footnoteNumber++).toString()); html.close(); html.close(); diff --git a/lib/documents.js b/lib/documents.js index 135e229..86e0709 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -15,7 +15,7 @@ function Document(children, options) { return { type: types.document, children: children, - footnotes: options.footnotes || [] + footnotes: options.footnotes || new Footnotes({}) }; } @@ -71,6 +71,14 @@ function FootnoteReference(options) { }; } +function Footnotes(footnotes) { + this._footnotes = footnotes; +} + +Footnotes.prototype.findFootnoteById = function(id) { + return this._footnotes[id] || null; +} + function Footnote(options) { return { type: types.footnote, @@ -105,5 +113,6 @@ exports.Text = Text; exports.Tab = Tab; exports.Hyperlink = Hyperlink; exports.FootnoteReference = FootnoteReference; +exports.Footnotes = Footnotes; exports.Footnote = Footnote; exports.Image = Image; diff --git a/lib/docx/footnotes-reader.js b/lib/docx/footnotes-reader.js index a55b261..c7ec98c 100644 --- a/lib/docx/footnotes-reader.js +++ b/lib/docx/footnotes-reader.js @@ -1,14 +1,6 @@ exports.readFootnotesXml = readFootnotesXml; -function Footnotes(footnotes) { - function findFootnoteById(id) { - return footnotes[id] || null; - } - - return { - findFootnoteById: findFootnoteById - }; -} +var Footnotes = require("../documents").Footnotes; function readFootnotesXml(xml) { var footnotes = {}; diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js index f030a72..da9879e 100644 --- a/tests/document-to-html.tests.js +++ b/tests/document-to-html.tests.js @@ -199,7 +199,7 @@ describe('DocumentConverter', function() { test('footnote reference is converted to superscript intra-page link', function() { var footnoteReference = new documents.FootnoteReference({ - footnoteId: 4, + footnoteId: "4", body: paragraphOfText("Who's there?") }); var converter = new DocumentConverter(); @@ -210,7 +210,7 @@ describe('DocumentConverter', function() { test('footnotes are included after the main body', function() { var footnoteReference = new documents.FootnoteReference({ - footnoteId: 1 + footnoteId: "4" }); var document = new documents.Document( [new documents.Paragraph([ @@ -218,17 +218,19 @@ describe('DocumentConverter', function() { new documents.Run([footnoteReference]) ])], { - footnotes: [documents.Footnote({ - body: [paragraphOfText("Who's there?")], - id: 1 - })] + footnotes: new documents.Footnotes({ + 4: new documents.Footnote({ + id: "4", + body: [paragraphOfText("Who's there?")] + }) + }) } ); var converter = new DocumentConverter(); return converter.convertToHtml(document).then(function(result) { - var expectedOutput = '<p>Knock knock<sup><a href="#footnote-1">1</a></sup></p>' + - '<ol><li id="footnote-1"><p>Who\'s there?</p></li></ol>'; + 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>'; assert.equal(result.value, expectedOutput); }); }); diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index 093173e..dd0f5b7 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -91,6 +91,7 @@ describe('mammoth', function() { test('footnotes are appended to text', function() { // TODO: generate unique IDs for footnotes // TODO: generate links back to main body + // TODO: don't duplicate footnotes with multiple references // TODO: not yet implemented return; var docxPath = path.join(__dirname, "test-data/footnotes.docx"); |
