diff --git a/src/turndown.js b/src/turndown.js index d110b155..514d372e 100644 --- a/src/turndown.js +++ b/src/turndown.js @@ -6,6 +6,21 @@ import Node from './node' var reduce = Array.prototype.reduce var leadingNewLinesRegExp = /^\n*/ var trailingNewLinesRegExp = /\n*$/ +var escapes = [ + [/\\/g, '\\\\'], + [/\*/g, '\\*'], + [/-/g, '\\-'], + [/^\+ /g, '\\+ '], + [/^(=+)/g, '\\$1'], + [/^(#{1,6}) /g, '\\$1 '], + [/`/g, '\\`'], + [/~~~/g, '\\~~~'], + [/\[/g, '\\['], + [/\]/g, '\\]'], + [/^>/g, '\\>'], + [/_/g, '\\_'], + [/^(\d+)\. /g, '$1\\. '] +] export default function TurndownService (options) { if (!(this instanceof TurndownService)) return new TurndownService(options) @@ -126,48 +141,9 @@ TurndownService.prototype = { */ escape: function (string) { - return ( - string - // Escape backslash escapes! - .replace(/\\(\S)/g, '\\\\$1') - - // Escape headings - .replace(/^(#{1,6} )/gm, '\\$1') - - // Escape hr - .replace(/^([-*_] *){3,}$/gm, function (match, character) { - return match.split(character).join('\\' + character) - }) - - // Escape ol bullet points - .replace(/^(\W* {0,3})(\d+)\. /gm, '$1$2\\. ') - - // Escape ul bullet points - .replace(/^([^\\\w]*)[*+-] /gm, function (match) { - return match.replace(/([*+-])/g, '\\$1') - }) - - // Escape blockquote indents - .replace(/^(\W* {0,3})> /gm, '$1\\> ') - - // Escape em/strong * - .replace(/\*+(?![*\s\W]).+?\*+/g, function (match) { - return match.replace(/\*/g, '\\*') - }) - - // Escape em/strong _ - .replace(/_+(?![_\s\W]).+?_+/g, function (match) { - return match.replace(/_/g, '\\_') - }) - - // Escape code _ - .replace(/`+(?![`\s\W]).+?`+/g, function (match) { - return match.replace(/`/g, '\\`') - }) - - // Escape link brackets - .replace(/[\[\]]/g, '\\$&') // eslint-disable-line no-useless-escape - ) + return escapes.reduce(function (accumulator, escape) { + return accumulator.replace(escape[0], escape[1]) + }, string) } } diff --git a/test/index.html b/test/index.html index 3174427c..40b30150 100644 --- a/test/index.html +++ b/test/index.html @@ -88,6 +88,16 @@ ================= +
\===+
A sentence containing =+
# Level One Heading with ATX@@ -281,6 +291,22 @@ ~~~
~~~ +def aFencedCodeBlock; end +~~~+
\~~~ +def aFencedCodeBlock; end +\~~~+
A sentence containing ~+
def a_fenced_code block; end
@@ -665,8 +691,8 @@ *\\*+
backslash \\
\### This is not a heading
#This is not # a heading+
To add emphasis, surround text with *. For example: \*this is emphasis\*+
To add emphasis, surround text with \*. For example: \*this is emphasis\*
To add emphasis, surround text with _. For example: \_this is emphasis\_+
To add emphasis, surround text with \_. For example: \_this is emphasis\_
def this_is_a_method; end;
def this_is_a_method; end;
To add strong emphasis, surround text with **. For example: \*\*this is strong\*\*+
To add strong emphasis, surround text with \*\*. For example: \*\*this is strong\*\*
To add strong emphasis, surround text with __. For example: \_\_this is strong\_\_+
To add strong emphasis, surround text with \_\_. For example: \_\_this is strong\_\_
1984\. by George Orwell
George Orwell wrote 1984.+
1984.George Orwell wrote 1984.
\+ An unordered list item
You can use * for multiplication: 1.5 * 3 = 4.5+
+1 and another ++
You can use \* for multiplication
45.5 - 3.5 = 42+
**\*\*test**
+1+
_test\_italics_
45 \- 3 is 42+
\> Blockquote in markdown
\>Blockquote in markdown+
42 > 1
fasdf *883 asdf wer qweasd fsd asdf asdfaqwe rqwefrsdf+
fasdf \*883 asdf wer qweasd fsd asdf asdfaqwe rqwefrsdf
* * ** It aims to be*
\* \* \*\* It aims to be*+
\* \* \*\* It aims to be\*
_Really_? Is that what it _is_? A **2000** year-old computer?
\_Really\_? Is that what it \_is\_? A \*\*2000\*\* year-old computer?+
\_Really\_? Is that what it \_is\_? A \*\*2000\*\* year\-old computer?