diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-03-29 22:44:29 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-03-29 22:44:29 +0000 |
| commit | 785deeefe5e64b0a12786d3d9647e403c320b99e (patch) | |
| tree | 02dad1eecc0a369f92d888234ce75fe58121e4db | |
| parent | d978f126b9847f45f17f4bd8419236a99bfe99a9 (diff) | |
Pass options from Converter through to DocumentConverter
| -rw-r--r-- | lib/documents.js | 3 | ||||
| -rw-r--r-- | lib/index.js | 4 | ||||
| -rw-r--r-- | tests/documents.tests.js | 10 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 20 |
4 files changed, 30 insertions, 7 deletions
diff --git a/lib/documents.js b/lib/documents.js index b973e4a..83a8106 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -12,6 +12,7 @@ exports.Text = Text; function DocumentConverter(options) { options = options || {}; options.paragraphStyleMap = options.paragraphStyleMap || {}; + var defaultParagraphStyle = options.defaultParagraphStyle || styles.topLevelElement("p"); var paragraphStyleMap = options.paragraphStyleMap; @@ -43,7 +44,7 @@ function DocumentConverter(options) { } function styleForParagraph(element) { - return paragraphStyleMap[element.properties.styleName] || styles.topLevelElement("p"); + return paragraphStyleMap[element.properties.styleName] || defaultParagraphStyle; } var elementConverters = { diff --git a/lib/index.js b/lib/index.js index 9fd3b16..7f124f5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,8 +5,8 @@ var documents = require("./documents"); exports.Converter = Converter; -function Converter() { - this._documentConverter = new documents.DocumentConverter(); +function Converter(options) { + this._documentConverter = new documents.DocumentConverter(options); } Converter.prototype.convertToHtml = function(docxFile) { diff --git a/tests/documents.tests.js b/tests/documents.tests.js index 629e0da..d5db03a 100644 --- a/tests/documents.tests.js +++ b/tests/documents.tests.js @@ -17,6 +17,16 @@ describe('DocumentConverter', function() { }); }); + test('can use non-default HTML element for unstyled paragraphs', function() { + var document = new documents.Document([ + paragraphOfText("Hello.") + ]); + var converter = new DocumentConverter({defaultParagraphStyle: styles.topLevelElement("h1")}); + return converter.convertToHtml(document).then(function(result) { + assert.equal("<h1>Hello.</h1>", result.html); + }); + }); + test('text is HTML-escaped', function() { var document = new documents.Document([ paragraphOfText("1 < 2") diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index 05ef218..ba6b925 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -4,6 +4,7 @@ var fs = require("fs"); var mammoth = require("../") var q = require("q"); var test = require("./testing").test; +var styles = require("../lib/styles"); describe('mammoth', function() { @@ -11,10 +12,23 @@ describe('mammoth', function() { var docxFile = createFakeDocxFile({ "word/document.xml": testData("simple/word/document.xml") }); - return convertToHtml(docxFile).then(function(result) { + var converter = new mammoth.Converter(); + return converter.convertToHtml(docxFile).then(function(result) { assert.equal("<p>Hello.</p>", result.html); }); - }) + }); + + test('options are passed to document converter', function() { + var docxFile = createFakeDocxFile({ + "word/document.xml": testData("simple/word/document.xml") + }); + var converter = new mammoth.Converter({ + defaultParagraphStyle: styles.topLevelElement("h1") + }); + return converter.convertToHtml(docxFile).then(function(result) { + assert.equal("<h1>Hello.</h1>", result.html); + }); + }); }) function createFakeDocxFile(files) { @@ -33,6 +47,4 @@ function testData(testDataPath) { } function convertToHtml(docxFile) { - var converter = new mammoth.Converter(); - return converter.convertToHtml(docxFile); } |
