From 589c2ae52d4ffda75731b0c63fcd9ba3349ab4c2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Apr 2017 18:53:38 +0100 Subject: Add monospace paragraph to code example --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 0a6add2..5257760 100644 --- a/README.md +++ b/README.md @@ -538,6 +538,33 @@ var options = { }; ``` +Or if you want paragraphs that have been explicitly set to use monospace fonts to represent code: + +```javascript +function transformParagraph(paragraph) { + var runs = mammoth.transforms.getDescendantsOfType(paragraph, "run"); + var isMatch = runs.length > 0 && runs.every(function(run) { + return run.font && fonts.indexOf(run.font.toLowerCase()) !== -1; + }); + if (isMatch) { + return { + ...paragraph, + styleId: "code", + styleName: "Code" + }; + } else { + return paragraph; + } +} + +var options = { + transformDocument: mammoth.transforms.paragraph(transformParagraph), + styleMap: [ + "p[style-name='Code'] => pre:separator('\n')" + ] +}; +``` + #### `mammoth.transforms.paragraph(transformParagraph)` Returns a function that can be used as the `transformDocument` option. -- cgit v1.2.3