aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-06-17 15:40:21 +0100
committerMichael Williamson <mike@zwobble.org>2015-06-17 15:40:21 +0100
commita52d99fd85061a9f20b481535407a225d5927434 (patch)
tree6cea0c2d11a263a959df251eb7be1af2455b6440
parentdc53892204989de1788081624c65062dc283480a (diff)
Allow strikethrough to be configured in style mapping
-rw-r--r--lib/document-matchers.js1
-rw-r--r--lib/document-to-html.js11
-rw-r--r--lib/style-reader.js4
-rw-r--r--tests/document-to-html.tests.js15
-rw-r--r--tests/mammoth.tests.js7
-rw-r--r--tests/style-reader.tests.js7
6 files changed, 43 insertions, 2 deletions
diff --git a/lib/document-matchers.js b/lib/document-matchers.js
index a4d59be..a03e335 100644
--- a/lib/document-matchers.js
+++ b/lib/document-matchers.js
@@ -1,6 +1,7 @@
exports.paragraph = paragraph;
exports.run = run;
exports.underline = new Matcher("underline");
+exports.strikethrough = new Matcher("strikethrough");
function paragraph(options) {
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index f424acf..907d62c 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -126,7 +126,7 @@ function DocumentConversion(options) {
convertUnderline(runHtml);
}
if (run.isStrikethrough) {
- runHtml.open(htmlPaths.element("s"));
+ convertStrikethrough(runHtml);
}
convertElements(run.children, runHtml, messages, function(err) {
if (err) {
@@ -143,6 +143,15 @@ function DocumentConversion(options) {
(options.convertUnderline || defaultConvertUnderline)(runHtml);
}
+ function convertStrikethrough(runHtml) {
+ var style = findStyle({type: "strikethrough"});
+ if (style) {
+ runHtml.appendPath(style.to);
+ } else {
+ runHtml.open(htmlPaths.element("s"));
+ }
+ }
+
function defaultConvertUnderline(runHtml) {
var style = findStyle({type: "underline"});
if (style) {
diff --git a/lib/style-reader.js b/lib/style-reader.js
index 811e06e..2560971 100644
--- a/lib/style-reader.js
+++ b/lib/style-reader.js
@@ -121,10 +121,12 @@ function documentMatcherRule() {
});
var underline = identifierToConstant("u", documentMatchers.underline);
+ var strikethrough = identifierToConstant("strike", documentMatchers.strikethrough);
return lop.rules.firstOf("element type",
paragraphOrRun,
- underline
+ underline,
+ strikethrough
);
}
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index 02079cd..2088f12 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -210,6 +210,21 @@ describe('DocumentConverter', function() {
});
});
+ test('strikethrough runs can be configured with style mapping', function() {
+ var run = runOfText("Hello.", {isStrikethrough: true});
+ var converter = new DocumentConverter({
+ styleMap: [
+ {
+ from: documentMatchers.strikethrough,
+ to: htmlPaths.elements([htmlPaths.element("del")])
+ }
+ ]
+ });
+ return converter.convertToHtml(run).then(function(result) {
+ assert.equal(result.value, "<del>Hello.</del>");
+ });
+ });
+
test('style mapping for underline runs does not close parent elements', function() {
var run = runOfText("Hello.", {isUnderline: true, isBold: true});
var converter = new DocumentConverter({
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index cdc14e5..d4d7ef4 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -231,6 +231,13 @@ describe('mammoth', function() {
});
});
+ test('strikethrough conversion can be configured with style mappings', function() {
+ var docxPath = path.join(__dirname, "test-data/strikethrough.docx");
+ return mammoth.convertToHtml({path: docxPath}, {styleMap: "strike => del"}).then(function(result) {
+ assert.equal(result.value, "<p><del>Today's Special: Salmon</del> Sold out</p>");
+ });
+ });
+
test('indentation is used if prettyPrint is true', function() {
var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
return mammoth.convertToHtml({path: docxPath}, {prettyPrint: true}).then(function(result) {
diff --git a/tests/style-reader.tests.js b/tests/style-reader.tests.js
index 59d7c51..bfe61dc 100644
--- a/tests/style-reader.tests.js
+++ b/tests/style-reader.tests.js
@@ -100,6 +100,13 @@ describe("styleReader.readDocumentMatcher", function() {
documentMatchers.underline
);
});
+
+ test("reads strikethrough", function() {
+ assert.deepEqual(
+ readDocumentMatcher("strike"),
+ documentMatchers.strikethrough
+ );
+ });
});
describe("styleReader.read", function() {