Skip to content

Commit 3b5527b

Browse files
authored
WEBDEV-8147: Improve story syntax highlighting (#28)
* Improve syntax highlighting * Use auto code syntax highlighting when possible * simplify syntax highlighter * Lazy load hljs
1 parent bd89dc0 commit 3b5527b

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

demo/story-template.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,25 @@ export class StoryTemplate extends LitElement {
8585
<slot name="demo"></slot>
8686
</div>
8787
<h3>Import</h3>
88-
<syntax-highlighter .code=${this.importCode}></syntax-highlighter>
88+
<syntax-highlighter
89+
language="typescript"
90+
.code=${this.importCode}
91+
></syntax-highlighter>
8992
<h3>Usage</h3>
9093
<syntax-highlighter
91-
.code=${this.exampleUsage + this.cssCode}
94+
language="auto"
95+
.code=${this.exampleUsage}
9296
></syntax-highlighter>
97+
${when(
98+
this.cssCode,
99+
() => html`
100+
<h3>Styling</h3>
101+
<syntax-highlighter
102+
language="css"
103+
.code=${this.cssCode}
104+
></syntax-highlighter>
105+
`,
106+
)}
93107
${this.styleSettingsTemplate}
94108
${this.shouldShowPropertySettings ? html` <h3>Settings</h3>` : nothing}
95109
<div

demo/syntax-highlighter.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import hljs from 'highlight.js';
2-
import typescript from 'highlight.js/lib/languages/typescript';
31
import {
42
type CSSResultGroup,
53
LitElement,
@@ -17,32 +15,41 @@ import { syntaxStyles } from './syntax-style-light';
1715
export class SyntaxHighlighter extends LitElement {
1816
@property({ type: String }) code = '';
1917

20-
@state() private highlightedCode = '';
18+
/**
19+
* The language to use for syntax highlighting, or 'auto' to automatically detect
20+
*
21+
* See https://highlightjs.readthedocs.io/en/latest/supported-languages.html for supported languages.
22+
*/
23+
@property({ type: String }) language = 'auto';
2124

22-
connectedCallback(): void {
23-
super.connectedCallback();
24-
hljs.registerLanguage('typescript', typescript);
25-
}
25+
@state() private highlightedCode: string = '';
2626

2727
protected willUpdate(_changedProperties: PropertyValues): void {
28-
if (_changedProperties.has('code')) {
28+
if (_changedProperties.has('code') || _changedProperties.has('language')) {
2929
this.highlightCode();
3030
}
3131
}
3232

3333
render() {
3434
return html`
35-
<pre><code class="hljs language-typescript">${unsafeHTML(
36-
this.highlightedCode,
37-
)}</code></pre>
35+
<pre><code class="hljs">${unsafeHTML(this.highlightedCode)}</code></pre>
3836
`;
3937
}
4038

41-
private highlightCode() {
39+
private async highlightCode() {
40+
const hljsModule = await import('highlight.js');
41+
const hljs = hljsModule.default;
4242
const code = this.code.trim();
43-
const highlighted = hljs.highlight(code, {
44-
language: 'typescript',
45-
}).value;
43+
44+
let highlighted: string;
45+
if (this.language === 'auto') {
46+
highlighted = hljs.highlightAuto(code).value;
47+
} else {
48+
highlighted = hljs.highlight(code, {
49+
language: this.language,
50+
}).value;
51+
}
52+
4653
this.highlightedCode = highlighted;
4754
}
4855

0 commit comments

Comments
 (0)