@unieai/rabi-util-code-language 0.0.0-stage → 0.2.2-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +269 -0
- package/lib/types/index.d.ts +40 -0
- package/package.json +29 -4
- package/src/index.ts +233 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-util-code-language
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Single file-extension to syntax-highlighting language table shared by Client code surfaces and the Host read card
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
//#region lib/types/index.js
|
|
2
|
+
/**
|
|
3
|
+
* Lowercase extension to canonical language id. A Map, not an object: a filename
|
|
4
|
+
* whose extension is an `Object.prototype` key (`foo.constructor`,
|
|
5
|
+
* `foo.__proto__`) must miss instead of resolving the inherited member, which
|
|
6
|
+
* would otherwise reach callers as a non-string language value.
|
|
7
|
+
*/
|
|
8
|
+
const LANGUAGES = new Map(Object.entries({
|
|
9
|
+
typescript: [
|
|
10
|
+
"ts",
|
|
11
|
+
"tsx",
|
|
12
|
+
"mts",
|
|
13
|
+
"cts"
|
|
14
|
+
],
|
|
15
|
+
javascript: [
|
|
16
|
+
"js",
|
|
17
|
+
"jsx",
|
|
18
|
+
"mjs",
|
|
19
|
+
"cjs"
|
|
20
|
+
],
|
|
21
|
+
shellscript: [
|
|
22
|
+
"sh",
|
|
23
|
+
"bash",
|
|
24
|
+
"zsh"
|
|
25
|
+
],
|
|
26
|
+
fish: ["fish"],
|
|
27
|
+
json: [
|
|
28
|
+
"json",
|
|
29
|
+
"jsonc",
|
|
30
|
+
"jsonl",
|
|
31
|
+
"ndjson",
|
|
32
|
+
"ipynb"
|
|
33
|
+
],
|
|
34
|
+
csv: ["csv"],
|
|
35
|
+
python: [
|
|
36
|
+
"py",
|
|
37
|
+
"pyw",
|
|
38
|
+
"pyi"
|
|
39
|
+
],
|
|
40
|
+
ruby: [
|
|
41
|
+
"rb",
|
|
42
|
+
"rake",
|
|
43
|
+
"gemspec"
|
|
44
|
+
],
|
|
45
|
+
go: ["go"],
|
|
46
|
+
rust: ["rs"],
|
|
47
|
+
java: ["java"],
|
|
48
|
+
c: ["c", "h"],
|
|
49
|
+
cpp: [
|
|
50
|
+
"cc",
|
|
51
|
+
"cpp",
|
|
52
|
+
"cxx",
|
|
53
|
+
"hh",
|
|
54
|
+
"hpp",
|
|
55
|
+
"hxx"
|
|
56
|
+
],
|
|
57
|
+
csharp: ["cs"],
|
|
58
|
+
kotlin: ["kt", "kts"],
|
|
59
|
+
swift: ["swift"],
|
|
60
|
+
php: ["php"],
|
|
61
|
+
yaml: ["yaml", "yml"],
|
|
62
|
+
toml: ["toml"],
|
|
63
|
+
ini: [
|
|
64
|
+
"ini",
|
|
65
|
+
"conf",
|
|
66
|
+
"cfg",
|
|
67
|
+
"properties"
|
|
68
|
+
],
|
|
69
|
+
dotenv: ["env"],
|
|
70
|
+
log: ["log"],
|
|
71
|
+
diff: ["diff", "patch"],
|
|
72
|
+
http: ["http"],
|
|
73
|
+
markdown: ["md", "markdown"],
|
|
74
|
+
mdx: ["mdx"],
|
|
75
|
+
rst: ["rst"],
|
|
76
|
+
latex: [
|
|
77
|
+
"tex",
|
|
78
|
+
"sty",
|
|
79
|
+
"cls"
|
|
80
|
+
],
|
|
81
|
+
bibtex: ["bib"],
|
|
82
|
+
asciidoc: ["adoc"],
|
|
83
|
+
html: [
|
|
84
|
+
"html",
|
|
85
|
+
"htm",
|
|
86
|
+
"xhtml"
|
|
87
|
+
],
|
|
88
|
+
css: ["css"],
|
|
89
|
+
scss: ["scss"],
|
|
90
|
+
less: ["less"],
|
|
91
|
+
sql: ["sql"],
|
|
92
|
+
xml: [
|
|
93
|
+
"xml",
|
|
94
|
+
"xsd",
|
|
95
|
+
"xsl",
|
|
96
|
+
"xslt",
|
|
97
|
+
"plist",
|
|
98
|
+
"svg"
|
|
99
|
+
],
|
|
100
|
+
lua: ["lua"],
|
|
101
|
+
bat: ["bat", "cmd"],
|
|
102
|
+
powershell: [
|
|
103
|
+
"ps1",
|
|
104
|
+
"psm1",
|
|
105
|
+
"psd1"
|
|
106
|
+
],
|
|
107
|
+
r: ["r"],
|
|
108
|
+
julia: ["jl"],
|
|
109
|
+
dart: ["dart"],
|
|
110
|
+
scala: ["scala"],
|
|
111
|
+
clojure: [
|
|
112
|
+
"clj",
|
|
113
|
+
"cljs",
|
|
114
|
+
"edn"
|
|
115
|
+
],
|
|
116
|
+
erlang: ["erl", "hrl"],
|
|
117
|
+
elixir: ["ex", "exs"],
|
|
118
|
+
haskell: ["hs"],
|
|
119
|
+
fsharp: [
|
|
120
|
+
"fs",
|
|
121
|
+
"fsi",
|
|
122
|
+
"fsx"
|
|
123
|
+
],
|
|
124
|
+
vb: ["vb"],
|
|
125
|
+
perl: ["pl", "pm"],
|
|
126
|
+
verilog: ["v"],
|
|
127
|
+
"system-verilog": ["sv", "svh"],
|
|
128
|
+
graphql: ["graphql", "gql"],
|
|
129
|
+
proto: ["proto"],
|
|
130
|
+
hcl: [
|
|
131
|
+
"tf",
|
|
132
|
+
"tfvars",
|
|
133
|
+
"hcl"
|
|
134
|
+
],
|
|
135
|
+
nix: ["nix"],
|
|
136
|
+
vue: ["vue"],
|
|
137
|
+
svelte: ["svelte"],
|
|
138
|
+
make: ["makefile", "mk"],
|
|
139
|
+
cmake: ["cmake"],
|
|
140
|
+
groovy: ["gradle", "groovy"]
|
|
141
|
+
}).flatMap(([language, extensions]) => extensions.map((extension) => [extension, language])));
|
|
142
|
+
/** Every recognized filename suffix, one entry each; a document-preview registry uses it to claim Code-rendered bodies. */
|
|
143
|
+
const CODE_HIGHLIGHT_EXTENSIONS = [...LANGUAGES.keys()];
|
|
144
|
+
/**
|
|
145
|
+
* Persisted `lang` values a language-level short id cannot express, keyed by the
|
|
146
|
+
* suffix that produced them. Everything else falls back to
|
|
147
|
+
* {@link SHORT_BY_LANGUAGE}, so only a suffix whose own name is the better label
|
|
148
|
+
* appears here. Each value is the exact string recorded sessions already hold
|
|
149
|
+
* and must not change; the byte-level expectation for every suffix lives in the
|
|
150
|
+
* read consumer's test.
|
|
151
|
+
*/
|
|
152
|
+
const READ_LANG_BY_EXTENSION = new Map([
|
|
153
|
+
["tsx", "tsx"],
|
|
154
|
+
["jsx", "jsx"],
|
|
155
|
+
["tf", "tf"],
|
|
156
|
+
["tfvars", "tfvars"],
|
|
157
|
+
["gradle", "gradle"]
|
|
158
|
+
]);
|
|
159
|
+
/**
|
|
160
|
+
* The short `lang` id the read card persists, keyed by canonical language id.
|
|
161
|
+
* Keys are exactly the languages in {@link LANGUAGE_EXTENSIONS}: where
|
|
162
|
+
* {@link READ_LANG_BY_EXTENSION} does not already fix a suffix's value, this
|
|
163
|
+
* table supplies the language's short name (`powershell` to `ps1`, `hcl` to
|
|
164
|
+
* `tf`, `system-verilog` to `sv`) rather than the grammar id. Keys are canonical
|
|
165
|
+
* ids, not untrusted extensions, so an object lookup cannot reach an
|
|
166
|
+
* `Object.prototype` member the way the extension tables can.
|
|
167
|
+
*/
|
|
168
|
+
const SHORT_BY_LANGUAGE = {
|
|
169
|
+
typescript: "ts",
|
|
170
|
+
javascript: "js",
|
|
171
|
+
shellscript: "sh",
|
|
172
|
+
fish: "fish",
|
|
173
|
+
json: "json",
|
|
174
|
+
csv: "csv",
|
|
175
|
+
python: "py",
|
|
176
|
+
ruby: "rb",
|
|
177
|
+
go: "go",
|
|
178
|
+
rust: "rs",
|
|
179
|
+
java: "java",
|
|
180
|
+
c: "c",
|
|
181
|
+
cpp: "cpp",
|
|
182
|
+
csharp: "cs",
|
|
183
|
+
kotlin: "kotlin",
|
|
184
|
+
swift: "swift",
|
|
185
|
+
php: "php",
|
|
186
|
+
yaml: "yaml",
|
|
187
|
+
toml: "toml",
|
|
188
|
+
ini: "ini",
|
|
189
|
+
dotenv: "env",
|
|
190
|
+
log: "log",
|
|
191
|
+
diff: "diff",
|
|
192
|
+
http: "http",
|
|
193
|
+
markdown: "md",
|
|
194
|
+
mdx: "mdx",
|
|
195
|
+
rst: "rst",
|
|
196
|
+
latex: "tex",
|
|
197
|
+
bibtex: "bib",
|
|
198
|
+
asciidoc: "adoc",
|
|
199
|
+
html: "html",
|
|
200
|
+
css: "css",
|
|
201
|
+
scss: "scss",
|
|
202
|
+
less: "less",
|
|
203
|
+
sql: "sql",
|
|
204
|
+
xml: "xml",
|
|
205
|
+
lua: "lua",
|
|
206
|
+
bat: "bat",
|
|
207
|
+
powershell: "ps1",
|
|
208
|
+
r: "r",
|
|
209
|
+
julia: "jl",
|
|
210
|
+
dart: "dart",
|
|
211
|
+
scala: "scala",
|
|
212
|
+
clojure: "clj",
|
|
213
|
+
erlang: "erl",
|
|
214
|
+
elixir: "ex",
|
|
215
|
+
haskell: "hs",
|
|
216
|
+
fsharp: "fs",
|
|
217
|
+
vb: "vb",
|
|
218
|
+
perl: "pl",
|
|
219
|
+
verilog: "v",
|
|
220
|
+
"system-verilog": "sv",
|
|
221
|
+
graphql: "graphql",
|
|
222
|
+
proto: "proto",
|
|
223
|
+
hcl: "hcl",
|
|
224
|
+
nix: "nix",
|
|
225
|
+
vue: "vue",
|
|
226
|
+
svelte: "svelte",
|
|
227
|
+
make: "make",
|
|
228
|
+
cmake: "cmake",
|
|
229
|
+
groovy: "groovy"
|
|
230
|
+
};
|
|
231
|
+
function extensionForPath(path) {
|
|
232
|
+
const slash = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
233
|
+
const base = path.slice(slash + 1);
|
|
234
|
+
const dot = base.lastIndexOf(".");
|
|
235
|
+
return dot < 0 ? void 0 : base.slice(dot + 1).toLowerCase();
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Derive the syntax-highlighting language from a filename or path, case
|
|
239
|
+
* insensitively. The extension is the text after the last dot of the final path
|
|
240
|
+
* segment; a leading dot is still the separator, so `.env` resolves to `dotenv`
|
|
241
|
+
* while an unlisted dotfile (`.gitignore`) and an extensionless name return
|
|
242
|
+
* `undefined`. Both path separators are recognized, so
|
|
243
|
+
* `C:\\dir\\main.PS1` resolves like `dir/main.ps1`.
|
|
244
|
+
* @param path - decoded filename or path.
|
|
245
|
+
* @returns the canonical language id, or `undefined` for an unrecognized or absent suffix.
|
|
246
|
+
*/
|
|
247
|
+
function languageForPath(path) {
|
|
248
|
+
const extension = extensionForPath(path);
|
|
249
|
+
return extension === void 0 ? void 0 : LANGUAGES.get(extension);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Derive the Host read card's persisted `lang` hint from a read path's
|
|
253
|
+
* extension. The value is the language's short name; for some languages that name
|
|
254
|
+
* is also the grammar id (`kotlin`, `swift`, `java`, `yaml`, `json`), and
|
|
255
|
+
* {@link READ_LANG_BY_EXTENSION} overrides it where the suffix names itself
|
|
256
|
+
* better (`tsx`, `tf`, `gradle`). A suffix a recorded session already holds keeps
|
|
257
|
+
* its persisted value. An unrecognized suffix stays `undefined`, so the card
|
|
258
|
+
* renders as plain text.
|
|
259
|
+
* @param path - the model-facing path the read reported.
|
|
260
|
+
* @returns the persisted language hint, or `undefined` when the extension maps to none.
|
|
261
|
+
*/
|
|
262
|
+
function readLangHintForPath(path) {
|
|
263
|
+
const extension = extensionForPath(path);
|
|
264
|
+
if (extension === void 0) return void 0;
|
|
265
|
+
const canonical = LANGUAGES.get(extension);
|
|
266
|
+
return canonical === void 0 ? void 0 : READ_LANG_BY_EXTENSION.get(extension) ?? SHORT_BY_LANGUAGE[canonical];
|
|
267
|
+
}
|
|
268
|
+
//#endregion
|
|
269
|
+
export { CODE_HIGHLIGHT_EXTENSIONS, languageForPath, readLangHintForPath };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single file-extension to syntax-highlighting language table shared by every
|
|
3
|
+
* code surface: the Client's document Code preview and diff review, and the Host
|
|
4
|
+
* read tool's persisted `lang` hint. Language ids are the grammar ids the Client
|
|
5
|
+
* highlighter's alias table resolves, so a language returned here reaches
|
|
6
|
+
* `highlightLines`/`highlightToHtml` unchanged; a filename outside the table, or
|
|
7
|
+
* one naming a language the highlighter does not register, renders as plain text.
|
|
8
|
+
* The read tool persists a short `lang` id, so {@link readLangHintForPath}
|
|
9
|
+
* projects short ids over this table: a suffix keeps the value a recorded session
|
|
10
|
+
* already holds, and every other suffix persists the language's short name —
|
|
11
|
+
* which for `kotlin`, `swift`, `yaml`, `json`, and similar equals that
|
|
12
|
+
* language's grammar id.
|
|
13
|
+
* @module @unieai/rabi-util-code-language
|
|
14
|
+
*/
|
|
15
|
+
/** Every recognized filename suffix, one entry each; a document-preview registry uses it to claim Code-rendered bodies. */
|
|
16
|
+
export declare const CODE_HIGHLIGHT_EXTENSIONS: readonly string[];
|
|
17
|
+
/**
|
|
18
|
+
* Derive the syntax-highlighting language from a filename or path, case
|
|
19
|
+
* insensitively. The extension is the text after the last dot of the final path
|
|
20
|
+
* segment; a leading dot is still the separator, so `.env` resolves to `dotenv`
|
|
21
|
+
* while an unlisted dotfile (`.gitignore`) and an extensionless name return
|
|
22
|
+
* `undefined`. Both path separators are recognized, so
|
|
23
|
+
* `C:\\dir\\main.PS1` resolves like `dir/main.ps1`.
|
|
24
|
+
* @param path - decoded filename or path.
|
|
25
|
+
* @returns the canonical language id, or `undefined` for an unrecognized or absent suffix.
|
|
26
|
+
*/
|
|
27
|
+
export declare function languageForPath(path: string): string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Derive the Host read card's persisted `lang` hint from a read path's
|
|
30
|
+
* extension. The value is the language's short name; for some languages that name
|
|
31
|
+
* is also the grammar id (`kotlin`, `swift`, `java`, `yaml`, `json`), and
|
|
32
|
+
* {@link READ_LANG_BY_EXTENSION} overrides it where the suffix names itself
|
|
33
|
+
* better (`tsx`, `tf`, `gradle`). A suffix a recorded session already holds keeps
|
|
34
|
+
* its persisted value. An unrecognized suffix stays `undefined`, so the card
|
|
35
|
+
* renders as plain text.
|
|
36
|
+
* @param path - the model-facing path the read reported.
|
|
37
|
+
* @returns the persisted language hint, or `undefined` when the extension maps to none.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readLangHintForPath(path: string): string | undefined;
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-util-code-language",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Single file-extension to syntax-highlighting language table shared by Client code surfaces and the Host read card",
|
|
4
|
+
"version": "0.2.2-rc.5",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./src/*": "./src/*",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib/index.js",
|
|
21
|
+
"lib/types/**/*.d.ts",
|
|
22
|
+
"lib/types/**/*.d.ts.map",
|
|
23
|
+
"lib/**/*.js.map",
|
|
24
|
+
"src",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@unieai/cordis": "4.1.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single file-extension to syntax-highlighting language table shared by every
|
|
3
|
+
* code surface: the Client's document Code preview and diff review, and the Host
|
|
4
|
+
* read tool's persisted `lang` hint. Language ids are the grammar ids the Client
|
|
5
|
+
* highlighter's alias table resolves, so a language returned here reaches
|
|
6
|
+
* `highlightLines`/`highlightToHtml` unchanged; a filename outside the table, or
|
|
7
|
+
* one naming a language the highlighter does not register, renders as plain text.
|
|
8
|
+
* The read tool persists a short `lang` id, so {@link readLangHintForPath}
|
|
9
|
+
* projects short ids over this table: a suffix keeps the value a recorded session
|
|
10
|
+
* already holds, and every other suffix persists the language's short name —
|
|
11
|
+
* which for `kotlin`, `swift`, `yaml`, `json`, and similar equals that
|
|
12
|
+
* language's grammar id.
|
|
13
|
+
* @module @unieai/rabi-util-code-language
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Recognized extensions per canonical language id. Values are lowercase
|
|
18
|
+
* extensions without the dot; the id names the grammar rather than a display
|
|
19
|
+
* label. The set covers common source, config, script, data, and markup
|
|
20
|
+
* extensions a line-numbered code view or preview benefits from highlighting; it
|
|
21
|
+
* is deliberately not an exhaustive linguist registry. Extensions whose Shiki
|
|
22
|
+
* grammar does not exist map to the nearest available grammar
|
|
23
|
+
* (`properties` to `ini`, whose registration carries the `properties` alias) or
|
|
24
|
+
* stay unlisted. Certificate and lock extensions (`pem`, `crt`, `key`, `cer`,
|
|
25
|
+
* `lock`) stay unlisted. Preview registries order specialized viewers before
|
|
26
|
+
* Code so CSV can default to Spreadsheet while retaining syntax highlighting.
|
|
27
|
+
* `makefile` covers only the suffix
|
|
28
|
+
* (`foo.makefile`); the extensionless `Makefile` name stays unlisted.
|
|
29
|
+
*/
|
|
30
|
+
const LANGUAGE_EXTENSIONS: Readonly<Record<string, readonly string[]>> = {
|
|
31
|
+
typescript: ['ts', 'tsx', 'mts', 'cts'],
|
|
32
|
+
javascript: ['js', 'jsx', 'mjs', 'cjs'],
|
|
33
|
+
shellscript: ['sh', 'bash', 'zsh'],
|
|
34
|
+
fish: ['fish'],
|
|
35
|
+
json: ['json', 'jsonc', 'jsonl', 'ndjson', 'ipynb'],
|
|
36
|
+
csv: ['csv'],
|
|
37
|
+
python: ['py', 'pyw', 'pyi'],
|
|
38
|
+
ruby: ['rb', 'rake', 'gemspec'],
|
|
39
|
+
go: ['go'],
|
|
40
|
+
rust: ['rs'],
|
|
41
|
+
java: ['java'],
|
|
42
|
+
c: ['c', 'h'],
|
|
43
|
+
cpp: ['cc', 'cpp', 'cxx', 'hh', 'hpp', 'hxx'],
|
|
44
|
+
csharp: ['cs'],
|
|
45
|
+
kotlin: ['kt', 'kts'],
|
|
46
|
+
swift: ['swift'],
|
|
47
|
+
php: ['php'],
|
|
48
|
+
yaml: ['yaml', 'yml'],
|
|
49
|
+
toml: ['toml'],
|
|
50
|
+
ini: ['ini', 'conf', 'cfg', 'properties'],
|
|
51
|
+
dotenv: ['env'],
|
|
52
|
+
log: ['log'],
|
|
53
|
+
diff: ['diff', 'patch'],
|
|
54
|
+
http: ['http'],
|
|
55
|
+
markdown: ['md', 'markdown'],
|
|
56
|
+
mdx: ['mdx'],
|
|
57
|
+
rst: ['rst'],
|
|
58
|
+
latex: ['tex', 'sty', 'cls'],
|
|
59
|
+
bibtex: ['bib'],
|
|
60
|
+
asciidoc: ['adoc'],
|
|
61
|
+
html: ['html', 'htm', 'xhtml'],
|
|
62
|
+
css: ['css'],
|
|
63
|
+
scss: ['scss'],
|
|
64
|
+
less: ['less'],
|
|
65
|
+
sql: ['sql'],
|
|
66
|
+
xml: ['xml', 'xsd', 'xsl', 'xslt', 'plist', 'svg'],
|
|
67
|
+
lua: ['lua'],
|
|
68
|
+
bat: ['bat', 'cmd'],
|
|
69
|
+
powershell: ['ps1', 'psm1', 'psd1'],
|
|
70
|
+
r: ['r'],
|
|
71
|
+
julia: ['jl'],
|
|
72
|
+
dart: ['dart'],
|
|
73
|
+
scala: ['scala'],
|
|
74
|
+
clojure: ['clj', 'cljs', 'edn'],
|
|
75
|
+
erlang: ['erl', 'hrl'],
|
|
76
|
+
elixir: ['ex', 'exs'],
|
|
77
|
+
haskell: ['hs'],
|
|
78
|
+
fsharp: ['fs', 'fsi', 'fsx'],
|
|
79
|
+
vb: ['vb'],
|
|
80
|
+
perl: ['pl', 'pm'],
|
|
81
|
+
verilog: ['v'],
|
|
82
|
+
'system-verilog': ['sv', 'svh'],
|
|
83
|
+
graphql: ['graphql', 'gql'],
|
|
84
|
+
proto: ['proto'],
|
|
85
|
+
hcl: ['tf', 'tfvars', 'hcl'],
|
|
86
|
+
nix: ['nix'],
|
|
87
|
+
vue: ['vue'],
|
|
88
|
+
svelte: ['svelte'],
|
|
89
|
+
make: ['makefile', 'mk'],
|
|
90
|
+
cmake: ['cmake'],
|
|
91
|
+
groovy: ['gradle', 'groovy'],
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Lowercase extension to canonical language id. A Map, not an object: a filename
|
|
96
|
+
* whose extension is an `Object.prototype` key (`foo.constructor`,
|
|
97
|
+
* `foo.__proto__`) must miss instead of resolving the inherited member, which
|
|
98
|
+
* would otherwise reach callers as a non-string language value.
|
|
99
|
+
*/
|
|
100
|
+
const LANGUAGES = new Map(Object.entries(LANGUAGE_EXTENSIONS)
|
|
101
|
+
.flatMap(([language, extensions]) => extensions.map(extension => [extension, language] as const)))
|
|
102
|
+
|
|
103
|
+
/** Every recognized filename suffix, one entry each; a document-preview registry uses it to claim Code-rendered bodies. */
|
|
104
|
+
export const CODE_HIGHLIGHT_EXTENSIONS: readonly string[] = [...LANGUAGES.keys()]
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Persisted `lang` values a language-level short id cannot express, keyed by the
|
|
108
|
+
* suffix that produced them. Everything else falls back to
|
|
109
|
+
* {@link SHORT_BY_LANGUAGE}, so only a suffix whose own name is the better label
|
|
110
|
+
* appears here. Each value is the exact string recorded sessions already hold
|
|
111
|
+
* and must not change; the byte-level expectation for every suffix lives in the
|
|
112
|
+
* read consumer's test.
|
|
113
|
+
*/
|
|
114
|
+
const READ_LANG_BY_EXTENSION = new Map<string, string>([
|
|
115
|
+
// The two JSX flavors keep their own suffix; every other persisted value is
|
|
116
|
+
// the language's short name, or an extension naming itself better than its
|
|
117
|
+
// language does (`tf` rather than `hcl`, `gradle` rather than `groovy`).
|
|
118
|
+
['tsx', 'tsx'], ['jsx', 'jsx'],
|
|
119
|
+
['tf', 'tf'], ['tfvars', 'tfvars'], ['gradle', 'gradle'],
|
|
120
|
+
])
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The short `lang` id the read card persists, keyed by canonical language id.
|
|
124
|
+
* Keys are exactly the languages in {@link LANGUAGE_EXTENSIONS}: where
|
|
125
|
+
* {@link READ_LANG_BY_EXTENSION} does not already fix a suffix's value, this
|
|
126
|
+
* table supplies the language's short name (`powershell` to `ps1`, `hcl` to
|
|
127
|
+
* `tf`, `system-verilog` to `sv`) rather than the grammar id. Keys are canonical
|
|
128
|
+
* ids, not untrusted extensions, so an object lookup cannot reach an
|
|
129
|
+
* `Object.prototype` member the way the extension tables can.
|
|
130
|
+
*/
|
|
131
|
+
const SHORT_BY_LANGUAGE: Readonly<Record<string, string>> = {
|
|
132
|
+
typescript: 'ts',
|
|
133
|
+
javascript: 'js',
|
|
134
|
+
shellscript: 'sh',
|
|
135
|
+
fish: 'fish',
|
|
136
|
+
json: 'json',
|
|
137
|
+
csv: 'csv',
|
|
138
|
+
python: 'py',
|
|
139
|
+
ruby: 'rb',
|
|
140
|
+
go: 'go',
|
|
141
|
+
rust: 'rs',
|
|
142
|
+
java: 'java',
|
|
143
|
+
c: 'c',
|
|
144
|
+
cpp: 'cpp',
|
|
145
|
+
csharp: 'cs',
|
|
146
|
+
kotlin: 'kotlin',
|
|
147
|
+
swift: 'swift',
|
|
148
|
+
php: 'php',
|
|
149
|
+
yaml: 'yaml',
|
|
150
|
+
toml: 'toml',
|
|
151
|
+
ini: 'ini',
|
|
152
|
+
dotenv: 'env',
|
|
153
|
+
log: 'log',
|
|
154
|
+
diff: 'diff',
|
|
155
|
+
http: 'http',
|
|
156
|
+
markdown: 'md',
|
|
157
|
+
mdx: 'mdx',
|
|
158
|
+
rst: 'rst',
|
|
159
|
+
latex: 'tex',
|
|
160
|
+
bibtex: 'bib',
|
|
161
|
+
asciidoc: 'adoc',
|
|
162
|
+
html: 'html',
|
|
163
|
+
css: 'css',
|
|
164
|
+
scss: 'scss',
|
|
165
|
+
less: 'less',
|
|
166
|
+
sql: 'sql',
|
|
167
|
+
xml: 'xml',
|
|
168
|
+
lua: 'lua',
|
|
169
|
+
bat: 'bat',
|
|
170
|
+
powershell: 'ps1',
|
|
171
|
+
r: 'r',
|
|
172
|
+
julia: 'jl',
|
|
173
|
+
dart: 'dart',
|
|
174
|
+
scala: 'scala',
|
|
175
|
+
clojure: 'clj',
|
|
176
|
+
erlang: 'erl',
|
|
177
|
+
elixir: 'ex',
|
|
178
|
+
haskell: 'hs',
|
|
179
|
+
fsharp: 'fs',
|
|
180
|
+
vb: 'vb',
|
|
181
|
+
perl: 'pl',
|
|
182
|
+
verilog: 'v',
|
|
183
|
+
'system-verilog': 'sv',
|
|
184
|
+
graphql: 'graphql',
|
|
185
|
+
proto: 'proto',
|
|
186
|
+
hcl: 'hcl',
|
|
187
|
+
nix: 'nix',
|
|
188
|
+
vue: 'vue',
|
|
189
|
+
svelte: 'svelte',
|
|
190
|
+
make: 'make',
|
|
191
|
+
cmake: 'cmake',
|
|
192
|
+
groovy: 'groovy',
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function extensionForPath(path: string): string | undefined {
|
|
196
|
+
const slash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
|
|
197
|
+
const base = path.slice(slash + 1)
|
|
198
|
+
const dot = base.lastIndexOf('.')
|
|
199
|
+
return dot < 0 ? undefined : base.slice(dot + 1).toLowerCase()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Derive the syntax-highlighting language from a filename or path, case
|
|
204
|
+
* insensitively. The extension is the text after the last dot of the final path
|
|
205
|
+
* segment; a leading dot is still the separator, so `.env` resolves to `dotenv`
|
|
206
|
+
* while an unlisted dotfile (`.gitignore`) and an extensionless name return
|
|
207
|
+
* `undefined`. Both path separators are recognized, so
|
|
208
|
+
* `C:\\dir\\main.PS1` resolves like `dir/main.ps1`.
|
|
209
|
+
* @param path - decoded filename or path.
|
|
210
|
+
* @returns the canonical language id, or `undefined` for an unrecognized or absent suffix.
|
|
211
|
+
*/
|
|
212
|
+
export function languageForPath(path: string): string | undefined {
|
|
213
|
+
const extension = extensionForPath(path)
|
|
214
|
+
return extension === undefined ? undefined : LANGUAGES.get(extension)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Derive the Host read card's persisted `lang` hint from a read path's
|
|
219
|
+
* extension. The value is the language's short name; for some languages that name
|
|
220
|
+
* is also the grammar id (`kotlin`, `swift`, `java`, `yaml`, `json`), and
|
|
221
|
+
* {@link READ_LANG_BY_EXTENSION} overrides it where the suffix names itself
|
|
222
|
+
* better (`tsx`, `tf`, `gradle`). A suffix a recorded session already holds keeps
|
|
223
|
+
* its persisted value. An unrecognized suffix stays `undefined`, so the card
|
|
224
|
+
* renders as plain text.
|
|
225
|
+
* @param path - the model-facing path the read reported.
|
|
226
|
+
* @returns the persisted language hint, or `undefined` when the extension maps to none.
|
|
227
|
+
*/
|
|
228
|
+
export function readLangHintForPath(path: string): string | undefined {
|
|
229
|
+
const extension = extensionForPath(path)
|
|
230
|
+
if (extension === undefined) return undefined
|
|
231
|
+
const canonical = LANGUAGES.get(extension)
|
|
232
|
+
return canonical === undefined ? undefined : READ_LANG_BY_EXTENSION.get(extension) ?? SHORT_BY_LANGUAGE[canonical]
|
|
233
|
+
}
|