|
5 | 5 | type Language, |
6 | 6 | type Theme, |
7 | 7 | } from './syntax-highlight-component.types'; |
8 | | -import { setupTokenHighlights } from './cssHighlight'; |
9 | 8 | import { configDefaults } from './config'; |
| 9 | +import { setupTokenHighlights } from './cssHighlight'; |
10 | 10 |
|
11 | 11 | /** |
12 | 12 | * A web component for syntax highlighting code blocks using the CSS Custom Highlight API. |
@@ -80,9 +80,7 @@ export default class SyntaxHighlightComponent extends LitElement { |
80 | 80 | super(); |
81 | 81 | this._internals = this.attachInternals(); |
82 | 82 | this._internals.role = 'code'; |
83 | | - setupTokenHighlights(configDefaults.tokenTypes, { |
84 | | - languageTokens: configDefaults.languageTokens, |
85 | | - }); |
| 83 | + setupTokenHighlights(configDefaults.tokenTypes); |
86 | 84 | } |
87 | 85 |
|
88 | 86 | get contentElement(): HTMLElement { |
@@ -223,36 +221,30 @@ export default class SyntaxHighlightComponent extends LitElement { |
223 | 221 | return html`<slot @slotchange=${this.paintTokenHighlights}></slot>`; |
224 | 222 | } |
225 | 223 |
|
226 | | - async paintTokenHighlights() { |
| 224 | + paintTokenHighlights() { |
227 | 225 | this.clearTokenHighlights(); |
228 | 226 | if (!CSS.highlights) return; |
229 | 227 |
|
230 | | - const text = this.contentElement.innerText; |
231 | | - const tokens = await SyntaxHighlightComponent._config.tokenize( |
232 | | - text, |
233 | | - this.language, |
234 | | - ); |
235 | | - const languageTokenTypes = |
236 | | - SyntaxHighlightComponent._config.languageTokens?.[this.language] || []; |
237 | | - |
238 | | - let pos = 0; |
239 | | - for (const token of tokens) { |
240 | | - if (token.type) { |
241 | | - const tokenType = languageTokenTypes.includes(token.type) |
242 | | - ? `${this.language}-${token.type}` |
243 | | - : token.type; |
244 | | - |
245 | | - const range = new Range(); |
246 | | - if (this.contentElement.firstChild) { |
247 | | - range.setStart(this.contentElement.firstChild, pos); |
248 | | - range.setEnd(this.contentElement.firstChild, pos + token.length); |
| 228 | + this.contentElement.childNodes.forEach(async (node) => { |
| 229 | + let pos = 0; |
| 230 | + const tokens = await SyntaxHighlightComponent._config.tokenize( |
| 231 | + node.textContent || '', |
| 232 | + this.language, |
| 233 | + ); |
| 234 | + for (const token of tokens) { |
| 235 | + if (token.type) { |
| 236 | + const range = new Range(); |
| 237 | + if (node && node.textContent?.length) { |
| 238 | + range.setStart(node, pos); |
| 239 | + range.setEnd(node, pos + token.length); |
249 | 240 |
|
250 | | - CSS.highlights.get(tokenType)?.add(range); |
251 | | - this._highlights.add({ tokenType, range }); |
| 241 | + CSS.highlights.get(token.type)?.add(range); |
| 242 | + this._highlights.add({ tokenType: token.type, range }); |
| 243 | + } |
252 | 244 | } |
| 245 | + pos += token.length; |
253 | 246 | } |
254 | | - pos += token.length; |
255 | | - } |
| 247 | + }); |
256 | 248 | } |
257 | 249 |
|
258 | 250 | protected clearTokenHighlights() { |
|
0 commit comments