Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
Changes after code review. Escape certain characters only when used i…
Browse files Browse the repository at this point in the history
…n valid markdown.
  • Loading branch information
Paul committed Jul 10, 2018
1 parent 0140cd5 commit 3c1faec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
21 changes: 9 additions & 12 deletions src/turndown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ import Node from './node'
var reduce = Array.prototype.reduce
var leadingNewLinesRegExp = /^\n*/
var trailingNewLinesRegExp = /\n*$/
const markdownReplacements = [
var escapes = [
[/\\/g, '\\\\'],
[/\*/g, '\\*'],
[/-/g, '\\-'],
[/\+/g, '\\+'],
[/=/g, '\\='],
[/#/g, '\\#'],
[/^\+ /g, '\\+ '],
[/^(=+)/g, '\\$1'],
[/^(#{1,6}) /g, '\\$1 '],
[/`/g, '\\`'],
[/~/g, '\\~'],
[/\|/g, '\\|'],
[/\(/g, '\\('],
[/\)/g, '\\)'],
[/\[/g, '\\['],
[/\]/g, '\\]'],
[/>/g, '\\>'],
[/^>/g, '\\>'],
[/_/g, '_'],
[/(\d+)\./g, '$1\\.']
[/^(\d+)\. /g, '$1\\. ']
]

export default function TurndownService (options) {
Expand Down Expand Up @@ -144,10 +142,9 @@ TurndownService.prototype = {
*/

escape: function (string) {
return markdownReplacements.reduce(
(search, replacement) => search.replace(replacement[0], replacement[1]),
string
)
return escapes.reduce(function (accumulator, escape) {
return accumulator.replace(escape[0], escape[1])
}, string)
}
}

Expand Down
52 changes: 31 additions & 21 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@
=================</pre>
</div>

<div class="case" data-name="escape = when used as heading">
<div class="input">===</div>
<pre class="expected">\===</pre>
</div>

<div class="case" data-name="do not escape = outside of a heading">
<div class="input">A sentence containing =</div>
<pre class="expected">A sentence containing =</pre>
</div>

<div class="case" data-name="h1 as atx" data-options='{"headingStyle":"atx"}'>
<div class="input"><h1>Level One Heading with ATX</h1></div>
<pre class="expected"># Level One Heading with ATX</pre>
Expand Down Expand Up @@ -671,7 +681,12 @@ <h2>This is a header.</h2>

<div class="case" data-name="escaping headings with #">
<div class="input">### This is not a heading</div>
<pre class="expected">\#\#\# This is not a heading</pre>
<pre class="expected">\### This is not a heading</pre>
</div>

<div class="case" data-name="do not escape # outside of a heading">
<div class="input">#This is not # a heading</div>
<pre class="expected">#This is not # a heading</pre>
</div>

<div class="case" data-name="escaping em markdown with *">
Expand Down Expand Up @@ -729,9 +744,9 @@ <h2>This is a header.</h2>
<pre class="expected">1984\. by George Orwell</pre>
</div>

<div class="case" data-name="escaping numbers followed by a full stop in a sentence">
<div class="input">George Orwell wrote 1984.</div>
<pre class="expected">George Orwell wrote 1984\.</pre>
<div class="case" data-name="do not escape . outside of an ol">
<div class="input">1984.George Orwell wrote 1984.</div>
<pre class="expected">1984.George Orwell wrote 1984.</pre>
</div>

<div class="case" data-name="escaping ul markdown *">
Expand All @@ -749,9 +764,9 @@ <h2>This is a header.</h2>
<pre class="expected">\+ An unordered list item</pre>
</div>

<div class="case" data-name="escaping =">
<div class="input">A sentence containing =</div>
<pre class="expected">A sentence containing \=</pre>
<div class="case" data-name="do not escape + outside of a ul">
<div class="input">+1 and another +</div>
<pre class="expected">+1 and another +</pre>
</div>

<div class="case" data-name="escaping ~">
Expand All @@ -764,11 +779,6 @@ <h2>This is a header.</h2>
<pre class="expected">A sentence containing \|</pre>
</div>

<div class="case" data-name="escaping parentheses">
<div class="input">(A sentence containing)</div>
<pre class="expected">\(A sentence containing\)</pre>
</div>

<div class="case" data-name="escaping *">
<div class="input">You can use * for multiplication</div>
<pre class="expected">You can use \* for multiplication</pre>
Expand All @@ -785,23 +795,23 @@ <h2>This is a header.</h2>
</div>

<div class="case" data-name="escaping -">
<div class="input">45.5 - 3.5 = 42</div>
<pre class="expected">45\.5 \- 3\.5 \= 42</pre>
</div>

<div class="case" data-name="escaping +">
<div class="input">+1</div>
<pre class="expected">\+1</pre>
<div class="input">45 - 3 is 42</div>
<pre class="expected">45 \- 3 is 42</pre>
</div>

<div class="case" data-name="escaping > as blockquote">
<div class="input">> Blockquote in markdown</div>
<pre class="expected">\> Blockquote in markdown</pre>
</div>

<div class="case" data-name="escaping > within text">
<div class="case" data-name="escaping > as blockquote without space">
<div class="input">>Blockquote in markdown</div>
<pre class="expected">\>Blockquote in markdown</pre>
</div>

<div class="case" data-name="do not escape > outside of a blockquote">
<div class="input">42 > 1</div>
<pre class="expected">42 \> 1</pre>
<pre class="expected">42 > 1</pre>
</div>

<div class="case" data-name="escaping code">
Expand Down

0 comments on commit 3c1faec

Please sign in to comment.