mirror of
https://github.com/deepseek-ai/deepseek-harness.git
synced 2026-11-10 04:27:42 +08:00
The shared code-language table must not claim a suffix a dedicated viewer declares, and the Host read card must keep the `lang` values it already persisted. - Drop `csv` from the shared table: Code registers before Spreadsheet, so the earlier body would take `.csv` and the browser snapshot gate fails. - Add `readLangHintForPath`, the read card's legacy projection over the shared table: the 43 pre-unification suffixes keep their old short ids byte for byte, new suffixes use the canonical id. - Add an ownership regression test over the full preview registry that turns red when `csv` returns, plus byte-level and alias-normalized tests for the read projection.
161 lines
6.6 KiB
TypeScript
161 lines
6.6 KiB
TypeScript
/**
|
|
* The single file-extension to syntax-highlighting language table shared by every
|
|
* code surface: the Client's document Code preview and diff review, and the Host
|
|
* read tool's persisted `lang` hint. Language ids are the grammar ids the Client
|
|
* highlighter's alias table resolves, so a language returned here reaches
|
|
* `highlightLines`/`highlightToHtml` unchanged; a filename outside the table, or
|
|
* one naming a language the highlighter does not register, renders as plain text.
|
|
* The read card predates the shared table, so {@link readLangHintForPath} projects
|
|
* its historical short ids over this table for the suffixes the old read table
|
|
* already recognized, keeping those persisted values byte-identical.
|
|
* @module @deepseek-ai/dsh-util-code-language
|
|
*/
|
|
|
|
/**
|
|
* Recognized extensions per canonical language id. Values are lowercase
|
|
* extensions without the dot; the id names the grammar rather than a display
|
|
* label. The set covers common source, config, script, data, and markup
|
|
* extensions a line-numbered code view or preview benefits from highlighting; it
|
|
* is deliberately not an exhaustive linguist registry. Extensions whose Shiki
|
|
* grammar does not exist map to the nearest available grammar
|
|
* (`properties` to `ini`, whose registration carries the `properties` alias) or
|
|
* stay unlisted. Certificate and lock extensions (`pem`, `crt`, `key`, `cer`,
|
|
* `lock`) stay unlisted, and `csv` is deliberately absent: the Spreadsheet
|
|
* preview declares it and must keep it, so listing it here would let the earlier
|
|
* Code registration take the suffix.
|
|
*/
|
|
const LANGUAGE_EXTENSIONS: Readonly<Record<string, readonly string[]>> = {
|
|
typescript: ['ts', 'tsx', 'mts', 'cts'],
|
|
javascript: ['js', 'jsx', 'mjs', 'cjs'],
|
|
shellscript: ['sh', 'bash', 'zsh'],
|
|
fish: ['fish'],
|
|
json: ['json', 'jsonc', 'jsonl', 'ndjson', 'ipynb'],
|
|
python: ['py', 'pyw', 'pyi'],
|
|
ruby: ['rb', 'rake', 'gemspec'],
|
|
go: ['go'],
|
|
rust: ['rs'],
|
|
java: ['java'],
|
|
c: ['c', 'h'],
|
|
cpp: ['cc', 'cpp', 'cxx', 'hh', 'hpp', 'hxx'],
|
|
csharp: ['cs'],
|
|
kotlin: ['kt', 'kts'],
|
|
swift: ['swift'],
|
|
php: ['php'],
|
|
yaml: ['yaml', 'yml'],
|
|
toml: ['toml'],
|
|
ini: ['ini', 'conf', 'cfg', 'properties'],
|
|
dotenv: ['env'],
|
|
log: ['log'],
|
|
diff: ['diff', 'patch'],
|
|
http: ['http'],
|
|
markdown: ['md', 'markdown'],
|
|
mdx: ['mdx'],
|
|
rst: ['rst'],
|
|
latex: ['tex', 'sty', 'cls'],
|
|
bibtex: ['bib'],
|
|
asciidoc: ['adoc'],
|
|
html: ['html', 'htm', 'xhtml'],
|
|
css: ['css'],
|
|
scss: ['scss'],
|
|
less: ['less'],
|
|
sql: ['sql'],
|
|
xml: ['xml', 'xsd', 'xsl', 'xslt', 'plist', 'svg'],
|
|
lua: ['lua'],
|
|
bat: ['bat', 'cmd'],
|
|
powershell: ['ps1', 'psm1', 'psd1'],
|
|
r: ['r'],
|
|
julia: ['jl'],
|
|
dart: ['dart'],
|
|
scala: ['scala'],
|
|
clojure: ['clj', 'cljs', 'edn'],
|
|
erlang: ['erl', 'hrl'],
|
|
elixir: ['ex', 'exs'],
|
|
haskell: ['hs'],
|
|
fsharp: ['fs', 'fsi', 'fsx'],
|
|
vb: ['vb'],
|
|
perl: ['pl', 'pm'],
|
|
verilog: ['v'],
|
|
'system-verilog': ['sv', 'svh'],
|
|
graphql: ['graphql', 'gql'],
|
|
proto: ['proto'],
|
|
hcl: ['tf', 'tfvars', 'hcl'],
|
|
nix: ['nix'],
|
|
vue: ['vue'],
|
|
svelte: ['svelte'],
|
|
make: ['makefile', 'mk'],
|
|
cmake: ['cmake'],
|
|
groovy: ['gradle'],
|
|
}
|
|
|
|
/**
|
|
* Lowercase extension to canonical language id. A Map, not an object: a filename
|
|
* whose extension is an `Object.prototype` key (`foo.constructor`,
|
|
* `foo.__proto__`) must miss instead of resolving the inherited member, which
|
|
* would otherwise reach callers as a non-string language value.
|
|
*/
|
|
const LANGUAGES = new Map(Object.entries(LANGUAGE_EXTENSIONS)
|
|
.flatMap(([language, extensions]) => extensions.map(extension => [extension, language] as const)))
|
|
|
|
/** Every recognized filename suffix, one entry each; a document-preview registry uses it to claim Code-rendered bodies. */
|
|
export const CODE_HIGHLIGHT_EXTENSIONS: readonly string[] = [...LANGUAGES.keys()]
|
|
|
|
/**
|
|
* The read card's historical short language hints for the suffixes its
|
|
* pre-unification table recognized. Keys are shared-table extensions, so the
|
|
* recognized set still derives from {@link LANGUAGE_EXTENSIONS}; values preserve
|
|
* the old persisted `lang` strings byte for byte. An extension absent here but
|
|
* present in the shared table is new and uses its canonical id.
|
|
*/
|
|
const READ_LANG_BY_EXTENSION = new Map<string, string>(Object.entries({
|
|
ts: 'ts', tsx: 'tsx', mts: 'ts', cts: 'ts',
|
|
js: 'js', jsx: 'jsx', mjs: 'js', cjs: 'js',
|
|
json: 'json', jsonc: 'json',
|
|
py: 'py', rb: 'rb', go: 'go', rs: 'rs', java: 'java',
|
|
c: 'c', h: 'c', cc: 'cpp', cpp: 'cpp', hpp: 'cpp', cxx: 'cpp',
|
|
cs: 'cs', kt: 'kotlin', swift: 'swift', php: 'php',
|
|
sh: 'sh', bash: 'sh', zsh: 'sh',
|
|
yaml: 'yaml', yml: 'yaml', toml: 'toml', ini: 'ini',
|
|
md: 'md', markdown: 'md', mdx: 'mdx',
|
|
html: 'html', htm: 'html', css: 'css', scss: 'scss', less: 'less',
|
|
sql: 'sql', xml: 'xml', lua: 'lua',
|
|
}))
|
|
|
|
function extensionForPath(path: string): string | undefined {
|
|
const slash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
|
|
const base = path.slice(slash + 1)
|
|
const dot = base.lastIndexOf('.')
|
|
return dot < 0 ? undefined : base.slice(dot + 1).toLowerCase()
|
|
}
|
|
|
|
/**
|
|
* Derive the syntax-highlighting language from a filename or path, case
|
|
* insensitively. The extension is the text after the last dot of the final path
|
|
* segment; a leading dot is still the separator, so `.env` resolves to `dotenv`
|
|
* while an unlisted dotfile (`.gitignore`) and an extensionless name return
|
|
* `undefined`. Both path separators are recognized, so
|
|
* `C:\\dir\\main.PS1` resolves like `dir/main.ps1`.
|
|
* @param path - decoded filename or path.
|
|
* @returns the canonical language id, or `undefined` for an unrecognized or absent suffix.
|
|
*/
|
|
export function languageForPath(path: string): string | undefined {
|
|
const extension = extensionForPath(path)
|
|
return extension === undefined ? undefined : LANGUAGES.get(extension)
|
|
}
|
|
|
|
/**
|
|
* Derive the Host read card's persisted `lang` hint from a read path's
|
|
* extension. This is the legacy projection the read card writes: a suffix the
|
|
* pre-unification read table recognized keeps its old short id (`ts`, `md`,
|
|
* `cpp`, …), while a suffix only the shared table knows (`.ps1`, `.env`,
|
|
* `.tf`, …) yields its canonical id. An unrecognized suffix stays
|
|
* `undefined`, so the card renders as plain text.
|
|
* @param path - the model-facing path the read reported.
|
|
* @returns the persisted language hint, or `undefined` when the extension maps to none.
|
|
*/
|
|
export function readLangHintForPath(path: string): string | undefined {
|
|
const extension = extensionForPath(path)
|
|
if (extension === undefined) return undefined
|
|
const canonical = LANGUAGES.get(extension)
|
|
return canonical === undefined ? undefined : READ_LANG_BY_EXTENSION.get(extension) ?? canonical
|
|
}
|