(
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 | if (!hasRootNode(result.emitter)) { |
| 525 | if (!loggedEmitterShapeError) { |
| 526 | loggedEmitterShapeError = true |
| 527 | logError( |
| 528 | new Error( |
| 529 | `color-diff: hljs emitter shape mismatch (keys: ${Object.keys(result.emitter).join(',')}). Syntax highlighting disabled.`, |
| 530 | ), |
| 531 | ) |
| 532 | } |
| 533 | return [[defaultStyle(theme), code]] |
| 534 | } |
| 535 | const blocks: Block[] = [] |
| 536 | flattenHljs(result.emitter.rootNode, theme, undefined, blocks) |
| 537 | return blocks |
| 538 | } |
| 539 | |
| 540 | // --------------------------------------------------------------------------- |
| 541 | // Word diff |
no test coverage detected