Skip to content

Commit 04f72b1

Browse files
committed
ES6: Template strings + interpolation
1 parent 1453aac commit 04f72b1

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

components/prism-javascript.js

+19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ Prism.languages.insertBefore('javascript', 'keyword', {
1111
}
1212
});
1313

14+
Prism.languages.insertBefore('javascript', 'string', {
15+
'template-string': {
16+
pattern: /`(?:\\`|\\?[^`])*`/,
17+
inside: {
18+
'interpolation': {
19+
pattern: /\$\{[^}]+\}/,
20+
inside: {
21+
'interpolation-punctuation': {
22+
pattern: /^\$\{|\}$/,
23+
alias: 'punctuation'
24+
},
25+
rest: Prism.languages.javascript
26+
}
27+
},
28+
'string': /[\s\S]+/
29+
}
30+
}
31+
});
32+
1433
if (Prism.languages.markup) {
1534
Prism.languages.insertBefore('markup', 'tag', {
1635
'script': {

components/prism-javascript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-javascript.html

+26-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ <h2>A division operator on the same line as a regex</h2>
5050
<pre><code>var foo = 1/2, bar = /a/g;
5151
var foo = /a/, bar = 3/4;</code></pre>
5252

53+
<h2>ES6 features</h2>
54+
<pre><code>// Regex "y" and "u" flags
55+
/[a-zA-Z]+/gimyu
56+
57+
// for..of loops
58+
for(let x of y) { }
59+
60+
// Modules: import
61+
import { foo as bar } from "file.js"
62+
63+
// Template strings
64+
`Only on ${y} one line`
65+
`This template string ${x} is on
66+
67+
multiple lines.`
68+
`40 + 2 = ${ 40 + 2 }`
69+
`The squares of the first 3 natural integers are ${[for (x of [1,2,3]) x*x].join(', ')}`</code></pre>
70+
5371
<h2>Known failures</h2>
5472
<p>There are certain edge cases where Prism will fail.
5573
There are always such cases in every regex-based syntax highlighter.
@@ -61,4 +79,11 @@ <h3>Comment-like substrings</h3>
6179
<pre><code>"foo /* bar */ baz"; "foo // bar";</code></pre>
6280

6381
<h3>Two quotes of the same type (i.e. both single or both double) inside a regex</h3>
64-
<pre><code>foo = /"foo"/;</code></pre>
82+
<pre><code>foo = /"foo"/;</code></pre>
83+
84+
<h3>String interpolation containing a closing brace</h3>
85+
<pre><code>`${ {foo:'bar'}.foo }`
86+
`${ '}' }`</code></pre>
87+
88+
<h3>String interpolation containing an unescaped back-tick</h3>
89+
<pre><code>`${ '`' }`</code></pre>

prism.js

+19
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,25 @@ Prism.languages.insertBefore('javascript', 'keyword', {
588588
}
589589
});
590590

591+
Prism.languages.insertBefore('javascript', 'string', {
592+
'template-string': {
593+
pattern: /`(?:\\`|\\?[^`])*`/,
594+
inside: {
595+
'interpolation': {
596+
pattern: /\$\{[^}]+\}/,
597+
inside: {
598+
'interpolation-punctuation': {
599+
pattern: /^\$\{|\}$/,
600+
alias: 'punctuation'
601+
},
602+
rest: Prism.languages.javascript
603+
}
604+
},
605+
'string': /[\s\S]+/
606+
}
607+
}
608+
});
609+
591610
if (Prism.languages.markup) {
592611
Prism.languages.insertBefore('markup', 'tag', {
593612
'script': {

0 commit comments

Comments
 (0)