Skip to content

Commit

Permalink
Merge pull request #1335 from richdouglasevans/issue/1334-no-typo-sub…
Browse files Browse the repository at this point in the history
…s-in-certain-blocks

Fix typographic substitution in (pre|code|kbd|script) blocks when smartypants=true
  • Loading branch information
styfle committed Sep 16, 2018
2 parents 7f2a917 + d604680 commit 7eab26a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ InlineLexer.prototype.output = function(src) {
} else if (this.inLink && /^<\/a>/i.test(cap[0])) {
this.inLink = false;
}
if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
this.inRawBlock = true;
} else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
this.inRawBlock = false;
}

src = src.substring(cap[0].length);
out += this.options.sanitize
? this.options.sanitizer
Expand Down Expand Up @@ -820,7 +826,11 @@ InlineLexer.prototype.output = function(src) {
// text
if (cap = this.rules.text.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.text(escape(this.smartypants(cap[0])));
if (this.inRawBlock) {
out += this.renderer.text(cap[0]);
} else {
out += this.renderer.text(escape(this.smartypants(cap[0])));
}
continue;
}

Expand Down
11 changes: 11 additions & 0 deletions test/new/smartypants_code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<pre>&amp;</pre>
<p><code>--foo</code>
<kbd>---foo</kbd></p>
<script>--foo</script>

<p>Ensure that text such as custom tags that happen to
begin with the same letters as the above tags don’t
match and thus benefit from Smartypants-ing.</p>

<p><script-custom>–foo</script-custom>
<code>--foo</code> &lt;codebar –foo codebar&gt;</p>
15 changes: 15 additions & 0 deletions test/new/smartypants_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
smartypants: true
description: SmartyPants does not modify characters within <pre>, <code>, <kbd>, or <script> tag blocks.
spec: https://daringfireball.net/projects/smartypants/
---
<pre>&amp;</pre>
<code>--foo</code>
<kbd>---foo</kbd>
<script>--foo</script>

Ensure that text such as custom tags that happen to
begin with the same letters as the above tags don't
match and thus benefit from Smartypants-ing.
<script-custom>--foo</script-custom>
`--foo` <codebar --foo codebar>

0 comments on commit 7eab26a

Please sign in to comment.