From 6e7c5fbb8c917d3ccc2f21a65cfb8c48680e4424 Mon Sep 17 00:00:00 2001 From: Elia Date: Fri, 22 Jan 2021 16:58:01 +1100 Subject: [PATCH] Added collapseMultipleWhitespaces option --- README.md | 1 + src/collapse-whitespace.js | 11 ++++++++++- src/root-node.js | 3 ++- src/turndown.js | 1 + test/index.html | 10 ++++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4c238fd2..d007483d 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ var turndownService = new TurndownService({ option: 'value' }) | `linkStyle` | `inlined` or `referenced` | `inlined` | | `linkReferenceStyle` | `full`, `collapsed`, or `shortcut` | `full` | | `preformattedCode` | `false` or [`true`](https://github.com/lucthev/collapse-whitespace/issues/16) | `false` | +| `collapseMultipleWhitespaces` | `true` or `false` | `true` | ### Advanced Options diff --git a/src/collapse-whitespace.js b/src/collapse-whitespace.js index 653d08c1..33553e8e 100644 --- a/src/collapse-whitespace.js +++ b/src/collapse-whitespace.js @@ -37,6 +37,7 @@ function collapseWhitespace (options) { var isPre = options.isPre || function (node) { return node.nodeName === 'PRE' } + var collapseWhitespaces = options.collapseWhitespaces if (!element.firstChild || isPre(element)) return @@ -48,7 +49,15 @@ function collapseWhitespace (options) { while (node !== element) { if (node.nodeType === 3 || node.nodeType === 4) { // Node.TEXT_NODE or Node.CDATA_SECTION_NODE - var text = node.data.replace(/[ \r\n\t]+/g, ' ') + var text + + // Replace series of multiple whitespaces with a single one + if (collapseWhitespaces) { + text = node.data.replace(/[ \r\n\t]+/g, ' ') + } else { + // Replace only for leading and trailing + text = node.data.replace(/^[ \r\n\t]+|[ \r\n\t]+$/g, ' ') + } if ((!prevText || / $/.test(prevText.data)) && !keepLeadingWs && text[0] === ' ') { diff --git a/src/root-node.js b/src/root-node.js index 7deaa8a8..28bca656 100644 --- a/src/root-node.js +++ b/src/root-node.js @@ -20,7 +20,8 @@ export default function RootNode (input, options) { element: root, isBlock: isBlock, isVoid: isVoid, - isPre: options.preformattedCode ? isPreOrCode : null + isPre: options.preformattedCode ? isPreOrCode : null, + collapseWhitespaces: options.collapseMultipleWhitespaces }) return root diff --git a/src/turndown.js b/src/turndown.js index d65fc348..60bb785f 100644 --- a/src/turndown.js +++ b/src/turndown.js @@ -38,6 +38,7 @@ export default function TurndownService (options) { linkReferenceStyle: 'full', br: ' ', preformattedCode: false, + collapseMultipleWhitespaces: true, blankReplacement: function (content, node) { return node.isBlock ? '\n\n' : '' }, diff --git a/test/index.html b/test/index.html index 26fa586c..80ed476c 100644 --- a/test/index.html +++ b/test/index.html @@ -717,6 +717,16 @@

This is a header.

Content in another div +
+ +
[link with     spaces](http://www.example.com)
+
+ +
+
this is an example with space
+
this _is     an example_ with space
+
+
backslash \
backslash \\