diff --git a/packages/app-cli/tests/html_to_md/anchor_with_underscores.html b/packages/app-cli/tests/html_to_md/anchor_with_underscores.html new file mode 100644 index 0000000000..accd74468c --- /dev/null +++ b/packages/app-cli/tests/html_to_md/anchor_with_underscores.html @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/packages/app-cli/tests/html_to_md/anchor_with_underscores.md b/packages/app-cli/tests/html_to_md/anchor_with_underscores.md new file mode 100644 index 0000000000..2e2955873a --- /dev/null +++ b/packages/app-cli/tests/html_to_md/anchor_with_underscores.md @@ -0,0 +1,4 @@ +- http://example.com/a_test +- http://example.com/a_test/___ +- Another *test*: http://example.com/a_test/* +- And another **test**: [Test](http://example.com/_test_) \ No newline at end of file diff --git a/packages/turndown/src/commonmark-rules.js b/packages/turndown/src/commonmark-rules.js index 7f4fcdedaa..190948d813 100644 --- a/packages/turndown/src/commonmark-rules.js +++ b/packages/turndown/src/commonmark-rules.js @@ -313,6 +313,12 @@ rules.inlineLink = { ) }, + escapeContent: function (node, _options) { + // Disable escaping content (including '_'s) when the link has the same URL and href. + // This prevents links from being broken by added escapes. + return node.getAttribute('href') !== node.textContent; + }, + replacement: function (content, node, options) { var href = filterLinkHref(node.getAttribute('href')) diff --git a/packages/turndown/src/turndown.js b/packages/turndown/src/turndown.js index 973dbdcba6..77416b2d47 100644 --- a/packages/turndown/src/turndown.js +++ b/packages/turndown/src/turndown.js @@ -216,7 +216,7 @@ function postProcess (output) { function replacementForNode (node) { var rule = this.rules.forNode(node) - var content = process.call(this, node, rule.escapeContent ? rule.escapeContent() : 'auto') + var content = process.call(this, node, rule.escapeContent ? rule.escapeContent(node) : 'auto') var whitespace = node.flankingWhitespace if (whitespace.leading || whitespace.trailing) content = content.trim()