(
state: { lang: string | null; stack: unknown },
line: string,
theme: Theme,
)
| 502 | let loggedEmitterShapeError = false |
| 503 | |
| 504 | function highlightLine( |
| 505 | state: { lang: string | null; stack: unknown }, |
| 506 | line: string, |
| 507 | theme: Theme, |
| 508 | ): Block[] { |
| 509 | // syntect-parity: feed a trailing \n so line comments terminate, then strip |
| 510 | const code = line + '\n' |
| 511 | if (!state.lang) { |
| 512 | return [[defaultStyle(theme), code]] |
| 513 | } |
| 514 | let result |
| 515 | try { |
| 516 | result = hljs().highlight(code, { |
| 517 | language: state.lang, |
| 518 | ignoreIllegals: true, |
| 519 | }) |
| 520 | } catch { |
| 521 | // hljs throws on unknown language despite ignoreIllegals |
| 522 | return [[defaultStyle(theme), code]] |
| 523 | } |
| 524 | // highlight.js ≥11 renamed `emitter` to `_emitter` (private). |
| 525 | const emitter = (result as any).emitter ?? (result as any)._emitter |
| 526 | if (!hasRootNode(emitter)) { |
| 527 | if (!loggedEmitterShapeError) { |
| 528 | loggedEmitterShapeError = true |
| 529 | logError( |
| 530 | new Error( |
| 531 | `color-diff: hljs emitter shape mismatch (keys: ${Object.keys(emitter ?? {}).join(',')}). Syntax highlighting disabled.`, |
| 532 | ), |
| 533 | ) |
| 534 | } |
| 535 | return [[defaultStyle(theme), code]] |
| 536 | } |
| 537 | const blocks: Block[] = [] |
| 538 | flattenHljs(emitter.rootNode, theme, undefined, blocks) |
| 539 | return blocks |
| 540 | } |
| 541 | |
| 542 | // --------------------------------------------------------------------------- |
| 543 | // Word diff |
no test coverage detected