@unieai/rabi-util-code-language 0.2.1-rc.3 → 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.
Files changed (2) hide show
  1. package/package.json +5 -2
  2. package/src/index.ts +233 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unieai/rabi-util-code-language",
3
3
  "description": "Single file-extension to syntax-highlighting language table shared by Client code surfaces and the Host read card",
4
- "version": "0.2.1-rc.3",
4
+ "version": "0.2.2-rc.5",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -19,10 +19,13 @@
19
19
  "files": [
20
20
  "lib/index.js",
21
21
  "lib/types/**/*.d.ts",
22
+ "lib/types/**/*.d.ts.map",
23
+ "lib/**/*.js.map",
24
+ "src",
22
25
  "README.md"
23
26
  ],
24
27
  "license": "MIT",
25
28
  "peerDependencies": {
26
- "@unieai/cordis": "4.0.5"
29
+ "@unieai/cordis": "4.1.0"
27
30
  }
28
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
+ }