From bf37d9d321dad98f3fa2ca99a5235d07cb66892e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 21:37:21 +0100 Subject: Wire up comment conversion code --- lib/docx/body-reader.js | 2 +- lib/docx/document-xml-reader.js | 5 ++++- lib/docx/docx-reader.js | 19 +++++++++++++++---- lib/options-reader.js | 2 ++ test/mammoth.tests.js | 19 +++++++++++++++++++ test/test-data/comments.docx | Bin 0 -> 15526 bytes 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100755 test/test-data/comments.docx diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index b514363..7b3120d 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -379,13 +379,13 @@ var ignoreElements = { "office-word:wrap": true, "v:shadow": true, "v:shapetype": true, + "w:annotationRef": true, "w:bookmarkEnd": true, "w:sectPr": true, "w:proofErr": true, "w:lastRenderedPageBreak": true, "w:commentRangeStart": true, "w:commentRangeEnd": true, - "w:commentReference": true, "w:del": true, "w:footnoteRef": true, "w:endnoteRef": true, diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index 12ca19e..bb912e4 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -12,7 +12,10 @@ function DocumentXmlReader(options) { var result = bodyReader.readXmlElements(body.children) .map(function(children) { - return new documents.Document(children, {notes: options.notes}); + return new documents.Document(children, { + notes: options.notes, + comments: options.comments + }); }); return new Result(result.value, result.messages); } diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 6ad63ab..57830d7 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -14,6 +14,7 @@ var contentTypesReader = require("./content-types-reader"); var numberingXml = require("./numbering-xml"); var stylesReader = require("./styles-reader"); var notesReader = require("./notes-reader"); +var commentsReader = require("./comments-reader"); var Files = require("./files").Files; @@ -40,6 +41,13 @@ function read(docxFile, input) { } else { return new Result([]); } + }), + comments: readXmlFileWithBody("comments", result, function(bodyReader, xml) { + if (xml) { + return commentsReader.createCommentsReader(bodyReader)(xml); + } else { + return new Result([]); + } }) }; }).also(function(result) { @@ -53,11 +61,14 @@ function read(docxFile, input) { }).then(function(result) { return readXmlFileWithBody("document", result, function(bodyReader, xml) { return result.notes.flatMap(function(notes) { - var reader = new DocumentXmlReader({ - bodyReader: bodyReader, - notes: notes + return result.comments.flatMap(function(comments) { + var reader = new DocumentXmlReader({ + bodyReader: bodyReader, + notes: notes, + comments: comments + }); + return reader.convertXmlToDocument(xml); }); - return reader.convertXmlToDocument(xml); }); }); }); diff --git a/lib/options-reader.js b/lib/options-reader.js index 19934d8..3765fd3 100644 --- a/lib/options-reader.js +++ b/lib/options-reader.js @@ -26,6 +26,8 @@ var standardOptions = exports._standardOptions = { "r[style-name='footnote reference'] =>", "p[style-name='endnote text'] => p", "r[style-name='endnote reference'] =>", + "p[style-name='annotation text'] => p", + "r[style-name='annotation reference'] =>", // LibreOffice "p[style-name='Footnote'] => p", diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index 12bee96..0515b2a 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -269,6 +269,25 @@ describe('mammoth', function() { }); }); + test('when style mapping is defined for comment references then comments are included', function() { + var docxPath = path.join(__dirname, "test-data/comments.docx"); + var options = { + idPrefix: "doc-42-", + styleMap: "comment-reference => sup" + }; + return mammoth.convertToHtml({path: docxPath}, options).then(function(result) { + var expectedOutput = ( + '

Ouch' + + '[MW1].' + + '[MW2]

' + + '
Comment [MW1]

A tachyon walks into a bar. ↑

' + + '
Comment [MW2]

Fin. ↑

' + ); + assert.equal(result.value, expectedOutput); + assert.deepEqual(result.messages, []); + }); + }); + test('textboxes are read', function() { var docxPath = path.join(__dirname, "test-data/text-box.docx"); return mammoth.convertToHtml({path: docxPath}).then(function(result) { diff --git a/test/test-data/comments.docx b/test/test-data/comments.docx new file mode 100755 index 0000000..32cdae1 Binary files /dev/null and b/test/test-data/comments.docx differ -- cgit v1.2.3