diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-07-06 17:52:24 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-07-06 17:52:24 +0100 |
| commit | 2550dfe6a8cd777a6c21ce8ae6c0a99b7f26aced (patch) | |
| tree | c24ce9e95b38de9505d246f37cdb4e6f5fc386f2 | |
| parent | def56988014ad3654185261ebb1bf4208cc3b6d8 (diff) | |
Add extractRawText function
| -rw-r--r-- | lib/index.js | 25 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 7 |
2 files changed, 32 insertions, 0 deletions
diff --git a/lib/index.js b/lib/index.js index 35bcbaf..879327f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ var readStyle = require("./style-reader").readStyle; var readOptions = require("./options-reader").readOptions; exports.convertToHtml = convertToHtml; +exports.extractRawText = extractRawText; function convertToHtml(input, options) { var fullOptions = readOptions(options); @@ -26,6 +27,30 @@ function convertDocumentToHtml(documentResult, options) { }); } + +function extractRawText(input) { + return docxReader.read(input) + .then(function(documentResult) { + return documentResult.map(convertElementToRawText); + }); +} + +function convertElementToRawText(element) { + if (element.type === "text") { + return element.value; + } else { + var tail = element.type === "paragraph" ? "\n\n" : ""; + return (element.children || []).map(convertElementToRawText).join("") + tail; + } +} + +function convertElements(elements, html, messages, callback) { + async.eachSeries(elements, function(element, callback) { + return elementToHtml(element, html, messages, callback); + }, callback); +} + + exports.styleMapping = function() { throw new Error('Use a raw string instead of mammoth.styleMapping e.g. "p[style-naming=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-naming=\'Title\'] => h1")'); }; diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index cba59ea..11f4fc6 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -138,4 +138,11 @@ describe('mammoth', function() { ); } }); + + test('extractRawText only retains raw text', function() { + var docxPath = path.join(__dirname, "test-data/simple-list.docx"); + return mammoth.extractRawText({path: docxPath}).then(function(result) { + assert.equal(result.value, 'Apple\n\nBanana\n\n'); + }); + }); }) |
