Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support highlighting inline HTML and CSS tagged template strings…
… in JS and TS (#2105) * Support nested template strings in TypeScript by copying how JavaScript does it. * Add support for highlighting inline HTML and CSS in TypeScript. With libraries like lit-html, htm, hyperHTML, and others, it's becoming common to write inline HTML and CSS as tagged template literals in source code. This change adds support for highlighting such code as HTML and CSS using highlight.js's inline language support. * Also add support for inline HTML and CSS to JavaScript highlighter.
- Loading branch information
Showing
8 changed files
with
188 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<span class="hljs-keyword">let</span> foo = <span class="hljs-literal">true</span>; | ||
<span class="hljs-string">`hello <span class="hljs-subst">${foo ? <span class="hljs-string">`Mr <span class="hljs-subst">${name}</span>`</span> : <span class="hljs-string">'there'</span>}</span>`</span>; | ||
foo = <span class="hljs-literal">false</span>; | ||
|
||
html`<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"foo"</span>></span>Hello world<span class="hljs-tag"></<span class="hljs-name">div</span>></span>`</span>; | ||
|
||
html`<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"foo"</span>></span>Hello times </span><span class="hljs-subst">${<span class="hljs-number">10</span>}</span><span class="xml"> <span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bar"</span>></span>world<span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>`</span>; | ||
|
||
html`<span class="xml"> | ||
<span class="hljs-tag"><<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"list"</span>></span> | ||
</span><span class="hljs-subst">${repeat([<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>], (v) => { | ||
<span class="hljs-keyword">return</span> html`<span class="xml"><span class="hljs-tag"><<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item"</span>></span></span><span class="hljs-subst">${v}</span><span class="xml"><span class="hljs-tag"></<span class="hljs-name">li</span>></span>`</span>; | ||
}</span><span class="xml">} | ||
<span class="hljs-tag"></<span class="hljs-name">ul</span>></span> | ||
`</span>; | ||
|
||
css`<span class="css"> | ||
<span class="hljs-selector-tag">body</span> { | ||
<span class="hljs-attribute">color</span>: red; | ||
} | ||
`</span>; | ||
|
||
<span class="hljs-comment">// Ensure that we're back in JavaScript mode.</span> | ||
<span class="hljs-keyword">var</span> foo = <span class="hljs-number">10</span>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
let foo = true; | ||
`hello ${foo ? `Mr ${name}` : 'there'}`; | ||
foo = false; | ||
|
||
html`<div id="foo">Hello world</div>`; | ||
|
||
html`<div id="foo">Hello times ${10} <span id="bar">world</span></div>`; | ||
|
||
html` | ||
<ul id="list"> | ||
${repeat(['a', 'b', 'c'], (v) => { | ||
return html`<li class="item">${v}</li>`; | ||
}} | ||
</ul> | ||
`; | ||
|
||
css` | ||
body { | ||
color: red; | ||
} | ||
`; | ||
|
||
// Ensure that we're back in JavaScript mode. | ||
var foo = 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<span class="hljs-keyword">let</span> foo = <span class="hljs-literal">true</span>; | ||
<span class="hljs-string">`hello <span class="hljs-subst">${foo ? <span class="hljs-string">`Mr <span class="hljs-subst">${name}</span>`</span> : <span class="hljs-string">'there'</span>}</span>`</span>; | ||
foo = <span class="hljs-literal">false</span>; | ||
|
||
html`<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"foo"</span>></span>Hello world<span class="hljs-tag"></<span class="hljs-name">div</span>></span>`</span>; | ||
|
||
html`<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"foo"</span>></span>Hello times </span><span class="hljs-subst">${<span class="hljs-number">10</span>}</span><span class="xml"> <span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bar"</span>></span>world<span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>`</span>; | ||
|
||
html`<span class="xml"> | ||
<span class="hljs-tag"><<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"list"</span>></span> | ||
</span><span class="hljs-subst">${repeat([<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>], (v) => { | ||
<span class="hljs-keyword">return</span> html`<span class="xml"><span class="hljs-tag"><<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item"</span>></span></span><span class="hljs-subst">${v}</span><span class="xml"><span class="hljs-tag"></<span class="hljs-name">li</span>></span>`</span>; | ||
}</span><span class="xml">} | ||
<span class="hljs-tag"></<span class="hljs-name">ul</span>></span> | ||
`</span>; | ||
|
||
css`<span class="css"> | ||
<span class="hljs-selector-tag">body</span> { | ||
<span class="hljs-attribute">color</span>: red; | ||
} | ||
`</span>; | ||
|
||
<span class="hljs-comment">// Ensure that we're back in TypeScript mode.</span> | ||
<span class="hljs-keyword">var</span> foo = <span class="hljs-number">10</span>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
let foo = true; | ||
`hello ${foo ? `Mr ${name}` : 'there'}`; | ||
foo = false; | ||
|
||
html`<div id="foo">Hello world</div>`; | ||
|
||
html`<div id="foo">Hello times ${10} <span id="bar">world</span></div>`; | ||
|
||
html` | ||
<ul id="list"> | ||
${repeat(['a', 'b', 'c'], (v) => { | ||
return html`<li class="item">${v}</li>`; | ||
}} | ||
</ul> | ||
`; | ||
|
||
css` | ||
body { | ||
color: red; | ||
} | ||
`; | ||
|
||
// Ensure that we're back in TypeScript mode. | ||
var foo = 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<span class="hljs-keyword">let</span> foo = <span class="hljs-literal">true</span>; | ||
<span class="hljs-string">`hello <span class="hljs-subst">${foo ? <span class="hljs-string">`Mr <span class="hljs-subst">${name}</span>`</span> : <span class="hljs-string">'there'</span>}</span>`</span>; | ||
foo = <span class="hljs-literal">false</span>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let foo = true; | ||
`hello ${foo ? `Mr ${name}` : 'there'}`; | ||
foo = false; |