diff options
| -rw-r--r-- | lib/document-to-html.js | 36 | ||||
| -rw-r--r-- | lib/documents.js | 21 | ||||
| -rw-r--r-- | tests/document-to-html.tests.js | 34 |
3 files changed, 80 insertions, 11 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 9741781..5e50e89 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -117,7 +117,22 @@ function DocumentConverter(options) { var elementConverters = { "document": function(document, html, messages, callback) { - convertElements(document.children, html, messages, callback); + convertElements(document.children, html, messages, function(error) { + if (error) { + callback(error); + } else { + html.closeAll(); + html.open(htmlPaths.element("ol")); + convertElements(document.footnotes, html, messages, function(error) { + if (error) { + callback(error); + } else { + html.close(); + callback(); + } + }); + } + }); }, "paragraph": convertParagraph, "run": convertRun, @@ -141,13 +156,28 @@ function DocumentConverter(options) { }); }, "footnoteReference": function(element, html, messages, callback) { + var number = element.footnoteNumber.toString(); html.open(htmlPaths.element("sup")); - html.open(htmlPaths.element("a", {href: "#footnote-" + element.id})); - html.text("1"); + html.open(htmlPaths.element("a", {href: "#footnote-" + number})); + html.text(element.footnoteNumber.toString()); html.close(); html.close(); callback(); }, + "footnote": function(element, html, messages, callback) { + html.open(htmlPaths.element("li", {id: "footnote-" + element.footnoteNumber})); + var footnoteHtml = new HtmlGenerator(); + convertElements(element.body, footnoteHtml, messages, function(err) { + if (err) { + callback(err); + } else { + footnoteHtml.closeAll(); + html.append(footnoteHtml); + html.close(); + callback(); + } + }); + }, "image": options.convertImage || defaultConvertImage }; return { diff --git a/lib/documents.js b/lib/documents.js index 12ebc73..f293ec3 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -6,13 +6,16 @@ var types = { tab: "tab", hyperlink: "hyperlink", footnoteReference: "footnoteReference", - image: "image" + image: "image", + footnote: "footnote" }; -function Document(children) { +function Document(children, options) { + options = options || {}; return { type: types.document, - children: children + children: children, + footnotes: options.footnotes || [] }; } @@ -64,11 +67,18 @@ function Hyperlink(children, options) { function FootnoteReference(options) { return { type: types.footnoteReference, - id: options.id, - body: options.body + footnoteNumber: options.footnoteNumber }; } +function Footnote(options) { + return { + type: types.footnote, + body: options.body, + footnoteNumber: options.footnoteNumber + }; +}; + function ElementWithChildren(type) { return function(children, properties) { return { @@ -95,4 +105,5 @@ exports.Text = Text; exports.Tab = Tab; exports.Hyperlink = Hyperlink; exports.FootnoteReference = FootnoteReference; +exports.Footnote = Footnote; exports.Image = Image; diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js index aae8499..9a40c88 100644 --- a/tests/document-to-html.tests.js +++ b/tests/document-to-html.tests.js @@ -198,10 +198,38 @@ describe('DocumentConverter', function() { }); test('footnote reference is converted to superscript intra-page link', function() { - var tab = new documents.FootnoteReference({id: 4, body: paragraphOfText("Hello.")}); + var footnoteReference = new documents.FootnoteReference({ + footnoteNumber: 1, + body: paragraphOfText("Who's there?") + }); var converter = new DocumentConverter(); - return converter.convertToHtml(tab).then(function(result) { - assert.equal(result.value, '<sup><a href="#footnote-4">1</a></sup>'); + return converter.convertToHtml(footnoteReference).then(function(result) { + assert.equal(result.value, '<sup><a href="#footnote-1">1</a></sup>'); + }); + }); + + test('footnotes are included after the main body', function() { + var footnoteReference = new documents.FootnoteReference({ + footnoteNumber: 1 + }); + var document = new documents.Document( + [new documents.Paragraph([ + runOfText("Knock knock"), + new documents.Run([footnoteReference]) + ])], + { + footnotes: [documents.Footnote({ + body: [paragraphOfText("Who's there?")], + footnoteNumber: 1 + })] + } + ); + + 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>'; + assert.equal(result.value, expectedOutput); }); }); |
