Skip to content

Commit 9d59d25

Browse files
committed
Support attributes at a inline code level
1 parent 8f9d0b7 commit 9d59d25

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/marked.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ var inline = {
458458
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
459459
em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
460460
code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
461+
attr: /^({{)(.*)}}/,
461462
br: /^ {2,}\n(?!\s*$)/,
462463
del: noop,
463464
text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
@@ -661,7 +662,15 @@ InlineLexer.prototype.output = function(src) {
661662
// code
662663
if (cap = this.rules.code.exec(src)) {
663664
src = src.substring(cap[0].length);
664-
out += this.renderer.codespan(escape(cap[2], true));
665+
666+
var attr;
667+
if (attr_cap = this.rules.attr.exec(src)) {
668+
src = src.substring(attr_cap[0].length);
669+
attr = attr_cap[2];
670+
}
671+
672+
out += this.renderer.codespan(escape(cap[2], true), attr);
673+
665674
continue;
666675
}
667676

@@ -854,7 +863,10 @@ Renderer.prototype.em = function(text) {
854863
return '<em>' + text + '</em>';
855864
};
856865

857-
Renderer.prototype.codespan = function(text) {
866+
Renderer.prototype.codespan = function(text, attr) {
867+
if(attr) {
868+
return '<code class="' + attr + '">' + text + '</code>';
869+
}
858870
return '<code>' + text + '</code>';
859871
};
860872

test/tests/code_spans.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<p><code>&lt;test a=&quot;</code> content of attribute <code>&quot;&gt;</code></p>
22

3+
<p><code class="lang">code with an attribute</code></p>
4+
35
<p>Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span></p>
46

57
<p>Here&#39;s how you put <code>`backticks`</code> in a code span.</p>

test/tests/code_spans.text

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
`<test a="` content of attribute `">`
22

3+
`code with an attribute`{{lang}}
4+
35
Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span>
46

57
Here's how you put `` `backticks` `` in a code span.

0 commit comments

Comments
 (0)