Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added collapseMultipleWhitespaces option #362

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion src/collapse-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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] === ' ') {
Expand Down
3 changes: 2 additions & 1 deletion src/root-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/turndown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' : ''
},
Expand Down
10 changes: 10 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,16 @@ <h2>This is a header.</h2>
Content in another div</pre>
</div>

<div class="case" data-name="preserve multiple whitespaces" data-options='{"collapseMultipleWhitespaces": false}'>
<div class="input"><a href="http://www.example.com">link with spaces</a></div>
<pre class="expected">[link with spaces](http://www.example.com)</pre>
</div>

<div class="case" data-name="trim string preserving multiple whitespaces" data-options='{"collapseMultipleWhitespaces": false}'>
<div class="input"><span>this <em> is an example </em> with space</span></div>
<pre class="expected">this _is an example_ with space</pre>
</div>

<div class="case" data-name="escaping backslashes">
<div class="input">backslash \</div>
<pre class="expected">backslash \\</pre>
Expand Down