Skip to content

Commit 7eab26a

Browse files
authored
Merge pull request #1335 from richdouglasevans/issue/1334-no-typo-subs-in-certain-blocks
Fix typographic substitution in (pre|code|kbd|script) blocks when smartypants=true
2 parents 7f2a917 + d604680 commit 7eab26a

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lib/marked.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,12 @@ InlineLexer.prototype.output = function(src) {
730730
} else if (this.inLink && /^<\/a>/i.test(cap[0])) {
731731
this.inLink = false;
732732
}
733+
if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
734+
this.inRawBlock = true;
735+
} else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
736+
this.inRawBlock = false;
737+
}
738+
733739
src = src.substring(cap[0].length);
734740
out += this.options.sanitize
735741
? this.options.sanitizer
@@ -820,7 +826,11 @@ InlineLexer.prototype.output = function(src) {
820826
// text
821827
if (cap = this.rules.text.exec(src)) {
822828
src = src.substring(cap[0].length);
823-
out += this.renderer.text(escape(this.smartypants(cap[0])));
829+
if (this.inRawBlock) {
830+
out += this.renderer.text(cap[0]);
831+
} else {
832+
out += this.renderer.text(escape(this.smartypants(cap[0])));
833+
}
824834
continue;
825835
}
826836

test/new/smartypants_code.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<pre>&amp;</pre>
2+
<p><code>--foo</code>
3+
<kbd>---foo</kbd></p>
4+
<script>--foo</script>
5+
6+
<p>Ensure that text such as custom tags that happen to
7+
begin with the same letters as the above tags don’t
8+
match and thus benefit from Smartypants-ing.</p>
9+
10+
<p><script-custom>–foo</script-custom>
11+
<code>--foo</code> &lt;codebar –foo codebar&gt;</p>

test/new/smartypants_code.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
smartypants: true
3+
description: SmartyPants does not modify characters within <pre>, <code>, <kbd>, or <script> tag blocks.
4+
spec: https://daringfireball.net/projects/smartypants/
5+
---
6+
<pre>&amp;</pre>
7+
<code>--foo</code>
8+
<kbd>---foo</kbd>
9+
<script>--foo</script>
10+
11+
Ensure that text such as custom tags that happen to
12+
begin with the same letters as the above tags don't
13+
match and thus benefit from Smartypants-ing.
14+
<script-custom>--foo</script-custom>
15+
`--foo` <codebar --foo codebar>

0 commit comments

Comments
 (0)