From 47efcec3be6f5bf84987c73e16fdeee991fd6bba Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 8 Jan 2014 17:21:46 +0000 Subject: Document transformDocument --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 71c51e8..39a2fc6 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,32 @@ var options = { }; ``` +### Document transforms + +Mammoth allows a document to be transformed before it is converted. +For instance, +suppose that document has not been semantically marked up, +but you know that any centre-aligned paragraph should be a heading. +You can use the `transformDocument` argument to modify the document appropriately: + +```javascript +function transformElement(element) { + if (element.children) { + element.children.forEach(transformElement); + } + if (element.type === "paragraph") { + if (element.alignment === "center" && !element.styleName) { + element.styleName = "Heading2"; + } + } + return element; +} + +var options = { + transformDocument: transformElement +}; +``` + ### API #### `mammoth.convertToHtml(input, options)` @@ -115,6 +141,9 @@ Converts the source document to HTML. the style map passed in `styleMap` is combined with the default style map. To stop using the default style map altogether, set `options.includeDefaultStyleMap` to `false`. + + * `transformDocument`: if set, + this function is applied to the document read from the docx file before the conversion to HTML. * Returns a promise containing a result. This result has the following properties: -- cgit v1.2.3