diff options
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | lib/document-to-html.js | 10 | ||||
| -rw-r--r-- | lib/html-generator.js | 1 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 16 | ||||
| -rw-r--r-- | tests/test-data/empty.docx | bin | 0 -> 3306 bytes |
5 files changed, 28 insertions, 2 deletions
@@ -301,6 +301,9 @@ Converts the source document to HTML. * `convertUnderline`: by default, the underlining of any text is ignored. Set this option to [`mammoth.underline.element(name)`](#underline) to override the default behaviour. + + * `ignoreEmptyParagraphs`: by default, empty paragraphs are ignored. + Set this option to `false` to preserve empty paragraphs in the output. * Returns a promise containing a result. This result has the following properties: diff --git a/lib/document-to-html.js b/lib/document-to-html.js index e9d35b9..220ae2b 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -1,6 +1,7 @@ -var promises = require("./promises"); var async = require("async"); +var _ = require("underscore"); +var promises = require("./promises"); var documents = require("./documents"); var htmlPaths = require("./html-paths"); var HtmlGenerator = require("./html-generator").HtmlGenerator; @@ -32,7 +33,7 @@ function DocumentConversion(options) { var noteReferences = []; - options = options || {}; + options = _.extend({ignoreEmptyParagraphs: true}, options); var idPrefix = options.idPrefix === undefined ? Math.floor(Math.random() * 1000000000000000) : options.idPrefix; @@ -76,6 +77,11 @@ function DocumentConversion(options) { function convertParagraph(element, html, messages, callback) { html.satisfyPath(styleForParagraph(element, messages)); + + if (!options.ignoreEmptyParagraphs) { + html.writeAll(); + } + setImmediate(function() { convertElements(element.children, html, messages, callback); }); diff --git a/lib/html-generator.js b/lib/html-generator.js index be1ebd2..a071a5e 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -90,6 +90,7 @@ function HtmlGenerator(options) { open: open, close: close, closeAll: closeAll, + writeAll: writeAll, asString: asString, append: append, selfClosing: selfClosing diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index 247ef17..6395afd 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -40,6 +40,22 @@ describe('mammoth', function() { }); }); + test('empty paragraphs are ignored by default', function() { + var docxPath = path.join(__dirname, "test-data/empty.docx"); + return mammoth.convertToHtml({path: docxPath}).then(function(result) { + assert.equal(result.value, ""); + assert.deepEqual(result.messages, []); + }); + }); + + test('empty paragraphs are preserved if ignoreEmptyParagraphs is false', function() { + var docxPath = path.join(__dirname, "test-data/empty.docx"); + return mammoth.convertToHtml({path: docxPath}, {ignoreEmptyParagraphs: false}).then(function(result) { + assert.equal(result.value, "<p></p>"); + assert.deepEqual(result.messages, []); + }); + }); + test('style map can be expressed as string', function() { var docxFile = createFakeDocxFile({ "word/document.xml": testData("simple/word/document.xml") diff --git a/tests/test-data/empty.docx b/tests/test-data/empty.docx Binary files differnew file mode 100644 index 0000000..e10b869 --- /dev/null +++ b/tests/test-data/empty.docx |
