Files
deepseek-harness/packages/client/ui-primitives/src/code-highlighting.ts
T
mektpoy 6945da14cb feat(web): unify the code-highlight language table and extend it
The document Code preview and the Host read card each owned an
extension-to-language table that had drifted: the preview table had 13
extensions the read card lacked, and the two returned different language
ids for the same suffix. Both now resolve through the new zero-dependency
@deepseek-ai/dsh-util-code-language, which also adds common Windows,
config, markup, and language extensions. Every added grammar stays in the
lazy set; the boot grammars are unchanged.

Fixes #4822
2026-09-21 17:42:11 +08:00

26 lines
1.3 KiB
TypeScript

/** Shared filename-to-grammar selection and lazy line highlighter for source views. */
import { useCallback, useSyncExternalStore } from 'react'
import {
grammarLoadCount, highlightLines, subscribeGrammarLoaded, type HighlightSpan,
} from './markdown/highlight.ts'
// The single extension table is owned by `@deepseek-ai/dsh-util-code-language`,
// shared with the Host read card; re-export it so Client callers keep importing
// the language selector and the preview registry's suffix list from this package.
export { CODE_HIGHLIGHT_EXTENSIONS, languageForPath } from '@deepseek-ai/dsh-util-code-language'
/** Highlight one source fragment into one token list per line. */
export type CodeHighlighter = (code: string) => HighlightSpan[][] | undefined
/**
* Bind the shared lazy highlighter to one language and refresh after its grammar loads.
* @param language - grammar hint selected from the source filename.
* @returns a stable fragment highlighter; unknown and loading grammars return `undefined` for plain-text fallback.
*/
export function useCodeHighlighter(language: string | undefined): CodeHighlighter {
const loaded = useSyncExternalStore(subscribeGrammarLoaded, grammarLoadCount, grammarLoadCount)
return useCallback(code => highlightLines(code, language), [language, loaded])
}
export type { HighlightSpan }