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