diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-07-08 10:53:00 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-07-08 10:53:00 +0100 |
| commit | 1d09fd322b96e8340126f69a3c048d3f0c183724 (patch) | |
| tree | 6c0bac2e900f315108e9210dc0607c4b221cf08f | |
| parent | 5ea0765a0699c2d007a9e53da60b3121e53ff50e (diff) | |
Document custom image conversion
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | README.md | 50 |
2 files changed, 54 insertions, 0 deletions
@@ -1,3 +1,7 @@ +# 0.3.5 + +* Add `mammoth.images.inline`, and document custom image conversion. + # 0.3.4 * Add the function `mammoth.extractRawText`. @@ -172,6 +172,25 @@ var options = { }; ``` +#### Custom image handlers + +By default, images are converted to `<img>` elements with the source included inline in the `src` attribute. +This behaviour can be changed by setting the `convertImage` option to an [image converter](#image-converters) . + +For instance, the following would replicate the default behaviour: + +```javascript +var options = { + convertImage: mammoth.images.inline(function(element) { + return element.read("base64").then(function(imageBuffer) { + return { + src: "data:" + element.contentType + ";base64," + imageBuffer + }; + }); + }) +}; +``` + #### Document transforms Mammoth allows a document to be transformed before it is converted. @@ -230,6 +249,9 @@ Converts the source document to HTML. * `transformDocument`: if set, this function is applied to the document read from the docx file before the conversion to HTML. + + * `convertImage`: by default, images are converted to `<img>` elements with the source included inline in the `src` attribute. + Set this option to an [image converter](#image-converters) to override the default behaviour. * Returns a promise containing a result. This result has the following properties: @@ -265,6 +287,34 @@ Each message has the following properties: * `message`: a string containing the actual message +#### Image converters + +An inline image converter can be created by calling `mammoth.images.inline(func)`. +This creates an inline `<img>` element for each image in the original docx. +`func` should be a function that has one argument called `element`. +This argument is the image element being converted, +and has the following properties: + +* `read([encoding])`: read the image file with the specified encoding. + If no encoding is specified, a `Buffer` is returned. + +* `contentType`: the content type of the image, such as `image/png`. + +`func` should return an object (or a promise containing an object) with a `src` property, +which will be used as the `src` attribute on the `<img>` element. + +For instance, the following replicates the default image conversion: + +```javascript +mammoth.images.inline(function(element) { + return element.read("base64").then(function(imageBuffer) { + return { + src: "data:" + element.contentType + ";base64," + imageBuffer + }; + }); +}) +``` + ## Writing style maps A style map is made up of a number of style mappings separated by new lines. |
