@scalar/highlight 0.2.0

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 (166) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +324 -0
  3. package/dist/all.d.ts +28 -0
  4. package/dist/all.d.ts.map +1 -0
  5. package/dist/all.js +105 -0
  6. package/dist/compat/hljs.d.ts +24 -0
  7. package/dist/compat/hljs.d.ts.map +1 -0
  8. package/dist/compat/hljs.js +95 -0
  9. package/dist/compat/index.d.ts +24 -0
  10. package/dist/compat/index.d.ts.map +1 -0
  11. package/dist/compat/index.js +21 -0
  12. package/dist/compat/languages.d.ts +46 -0
  13. package/dist/compat/languages.d.ts.map +1 -0
  14. package/dist/compat/languages.js +236 -0
  15. package/dist/compat/syntax-highlight.d.ts +37 -0
  16. package/dist/compat/syntax-highlight.d.ts.map +1 -0
  17. package/dist/compat/syntax-highlight.js +133 -0
  18. package/dist/core/compile.d.ts +3 -0
  19. package/dist/core/compile.d.ts.map +1 -0
  20. package/dist/core/compile.js +142 -0
  21. package/dist/core/index.d.ts +9 -0
  22. package/dist/core/index.d.ts.map +1 -0
  23. package/dist/core/index.js +5 -0
  24. package/dist/core/registry.d.ts +14 -0
  25. package/dist/core/registry.d.ts.map +1 -0
  26. package/dist/core/registry.js +77 -0
  27. package/dist/core/render.d.ts +67 -0
  28. package/dist/core/render.d.ts.map +1 -0
  29. package/dist/core/render.js +216 -0
  30. package/dist/core/scopes.d.ts +94 -0
  31. package/dist/core/scopes.d.ts.map +1 -0
  32. package/dist/core/scopes.js +113 -0
  33. package/dist/core/tokenize.d.ts +18 -0
  34. package/dist/core/tokenize.d.ts.map +1 -0
  35. package/dist/core/tokenize.js +131 -0
  36. package/dist/core/types.d.ts +99 -0
  37. package/dist/core/types.d.ts.map +1 -0
  38. package/dist/core/types.js +10 -0
  39. package/dist/index.d.ts +19 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +20 -0
  42. package/dist/langs/bash.d.ts +4 -0
  43. package/dist/langs/bash.d.ts.map +1 -0
  44. package/dist/langs/bash.js +123 -0
  45. package/dist/langs/c.d.ts +4 -0
  46. package/dist/langs/c.d.ts.map +1 -0
  47. package/dist/langs/c.js +196 -0
  48. package/dist/langs/clojure.d.ts +4 -0
  49. package/dist/langs/clojure.d.ts.map +1 -0
  50. package/dist/langs/clojure.js +213 -0
  51. package/dist/langs/cpp.d.ts +4 -0
  52. package/dist/langs/cpp.d.ts.map +1 -0
  53. package/dist/langs/cpp.js +391 -0
  54. package/dist/langs/csharp.d.ts +4 -0
  55. package/dist/langs/csharp.d.ts.map +1 -0
  56. package/dist/langs/csharp.js +307 -0
  57. package/dist/langs/css.d.ts +4 -0
  58. package/dist/langs/css.d.ts.map +1 -0
  59. package/dist/langs/css.js +94 -0
  60. package/dist/langs/dart.d.ts +4 -0
  61. package/dist/langs/dart.d.ts.map +1 -0
  62. package/dist/langs/dart.js +262 -0
  63. package/dist/langs/diff.d.ts +10 -0
  64. package/dist/langs/diff.d.ts.map +1 -0
  65. package/dist/langs/diff.js +26 -0
  66. package/dist/langs/dockerfile.d.ts +4 -0
  67. package/dist/langs/dockerfile.d.ts.map +1 -0
  68. package/dist/langs/dockerfile.js +308 -0
  69. package/dist/langs/elixir.d.ts +4 -0
  70. package/dist/langs/elixir.d.ts.map +1 -0
  71. package/dist/langs/elixir.js +343 -0
  72. package/dist/langs/fsharp.d.ts +4 -0
  73. package/dist/langs/fsharp.d.ts.map +1 -0
  74. package/dist/langs/fsharp.js +330 -0
  75. package/dist/langs/go.d.ts +5 -0
  76. package/dist/langs/go.d.ts.map +1 -0
  77. package/dist/langs/go.js +90 -0
  78. package/dist/langs/graphql.d.ts +4 -0
  79. package/dist/langs/graphql.d.ts.map +1 -0
  80. package/dist/langs/graphql.js +185 -0
  81. package/dist/langs/haskell.d.ts +4 -0
  82. package/dist/langs/haskell.d.ts.map +1 -0
  83. package/dist/langs/haskell.js +375 -0
  84. package/dist/langs/html.d.ts +14 -0
  85. package/dist/langs/html.d.ts.map +1 -0
  86. package/dist/langs/html.js +71 -0
  87. package/dist/langs/http.d.ts +4 -0
  88. package/dist/langs/http.d.ts.map +1 -0
  89. package/dist/langs/http.js +185 -0
  90. package/dist/langs/ini.d.ts +4 -0
  91. package/dist/langs/ini.d.ts.map +1 -0
  92. package/dist/langs/ini.js +227 -0
  93. package/dist/langs/java.d.ts +4 -0
  94. package/dist/langs/java.d.ts.map +1 -0
  95. package/dist/langs/java.js +178 -0
  96. package/dist/langs/javascript.d.ts +4 -0
  97. package/dist/langs/javascript.d.ts.map +1 -0
  98. package/dist/langs/javascript.js +226 -0
  99. package/dist/langs/json.d.ts +10 -0
  100. package/dist/langs/json.d.ts.map +1 -0
  101. package/dist/langs/json.js +46 -0
  102. package/dist/langs/kotlin.d.ts +4 -0
  103. package/dist/langs/kotlin.d.ts.map +1 -0
  104. package/dist/langs/kotlin.js +220 -0
  105. package/dist/langs/lua.d.ts +4 -0
  106. package/dist/langs/lua.d.ts.map +1 -0
  107. package/dist/langs/lua.js +330 -0
  108. package/dist/langs/makefile.d.ts +4 -0
  109. package/dist/langs/makefile.d.ts.map +1 -0
  110. package/dist/langs/makefile.js +266 -0
  111. package/dist/langs/markdown.d.ts +11 -0
  112. package/dist/langs/markdown.d.ts.map +1 -0
  113. package/dist/langs/markdown.js +71 -0
  114. package/dist/langs/matlab.d.ts +4 -0
  115. package/dist/langs/matlab.d.ts.map +1 -0
  116. package/dist/langs/matlab.js +352 -0
  117. package/dist/langs/mojo.d.ts +4 -0
  118. package/dist/langs/mojo.d.ts.map +1 -0
  119. package/dist/langs/mojo.js +337 -0
  120. package/dist/langs/nginx.d.ts +4 -0
  121. package/dist/langs/nginx.d.ts.map +1 -0
  122. package/dist/langs/nginx.js +168 -0
  123. package/dist/langs/objectivec.d.ts +4 -0
  124. package/dist/langs/objectivec.d.ts.map +1 -0
  125. package/dist/langs/objectivec.js +287 -0
  126. package/dist/langs/ocaml.d.ts +4 -0
  127. package/dist/langs/ocaml.d.ts.map +1 -0
  128. package/dist/langs/ocaml.js +309 -0
  129. package/dist/langs/perl.d.ts +4 -0
  130. package/dist/langs/perl.d.ts.map +1 -0
  131. package/dist/langs/perl.js +435 -0
  132. package/dist/langs/php.d.ts +4 -0
  133. package/dist/langs/php.d.ts.map +1 -0
  134. package/dist/langs/php.js +325 -0
  135. package/dist/langs/powershell.d.ts +4 -0
  136. package/dist/langs/powershell.d.ts.map +1 -0
  137. package/dist/langs/powershell.js +308 -0
  138. package/dist/langs/python.d.ts +4 -0
  139. package/dist/langs/python.d.ts.map +1 -0
  140. package/dist/langs/python.js +350 -0
  141. package/dist/langs/r.d.ts +4 -0
  142. package/dist/langs/r.d.ts.map +1 -0
  143. package/dist/langs/r.js +265 -0
  144. package/dist/langs/ruby.d.ts +4 -0
  145. package/dist/langs/ruby.d.ts.map +1 -0
  146. package/dist/langs/ruby.js +365 -0
  147. package/dist/langs/rust.d.ts +10 -0
  148. package/dist/langs/rust.d.ts.map +1 -0
  149. package/dist/langs/rust.js +116 -0
  150. package/dist/langs/scala.d.ts +4 -0
  151. package/dist/langs/scala.d.ts.map +1 -0
  152. package/dist/langs/scala.js +308 -0
  153. package/dist/langs/sql.d.ts +5 -0
  154. package/dist/langs/sql.d.ts.map +1 -0
  155. package/dist/langs/sql.js +57 -0
  156. package/dist/langs/swift.d.ts +33 -0
  157. package/dist/langs/swift.d.ts.map +1 -0
  158. package/dist/langs/swift.js +256 -0
  159. package/dist/langs/yaml.d.ts +11 -0
  160. package/dist/langs/yaml.d.ts.map +1 -0
  161. package/dist/langs/yaml.js +123 -0
  162. package/dist/lazy.d.ts +69 -0
  163. package/dist/lazy.d.ts.map +1 -0
  164. package/dist/lazy.js +200 -0
  165. package/dist/style.css +308 -0
  166. package/package.json +86 -0
@@ -0,0 +1,46 @@
1
+ import type { CompiledGrammar } from '../core/types.js';
2
+ /**
3
+ * `lowlightLanguageMappings`, copied verbatim from `@scalar/code-highlight`.
4
+ *
5
+ * This runs before anything else and decides the `language-*` class on the
6
+ * `<code>` element, so it has to stay byte-identical: `code.css` targets
7
+ * `.hljs.language-html`, `.hljs.language-curl` and friends by name.
8
+ */
9
+ export declare const lowlightLanguageMappings: Record<string, string>;
10
+ /**
11
+ * Every key of `standardLanguages` in `@scalar/code-highlight`.
12
+ *
13
+ * Kept so `keyof typeof standardLanguages` — which is how `ScalarCodeBlock`
14
+ * derives its `StandardLanguageKey` prop type — resolves to the same union
15
+ * after a swap. The names we have no grammar for stay in the list: they
16
+ * render as plain escaped text, which is what an unregistered language does
17
+ * in the lowlight pipeline today.
18
+ */
19
+ export declare const STANDARD_LANGUAGE_NAMES: readonly ["bash", "c", "clojure", "cpp", "csharp", "css", "curl", "dart", "diff", "docker", "dockerfile", "elixir", "fsharp", "go", "graphql", "haskell", "html", "http", "ini", "java", "javascript", "json", "julia", "kotlin", "less", "lua", "makefile", "markdown", "matlab", "mojo", "nginx", "objectivec", "ocaml", "perl", "php", "plaintext", "powershell", "properties", "python", "r", "ruby", "rust", "scala", "scss", "shell", "sql", "swift", "toml", "typescript", "xml", "yaml"];
20
+ export type StandardLanguageKey = (typeof STANDARD_LANGUAGE_NAMES)[number];
21
+ /**
22
+ * Stand-in for `standardLanguages`.
23
+ *
24
+ * The lowlight version maps each name to a highlight.js `LanguageFn`; this one
25
+ * maps to our grammar name, or `null` where we have no grammar yet. Callers
26
+ * pass it straight back into `syntaxHighlight`, which ignores it — the
27
+ * registry already knows what is loaded — so the value only has to carry the
28
+ * key set and the coverage answer.
29
+ */
30
+ export declare const standardLanguages: Record<StandardLanguageKey, string | null>;
31
+ /** Language names in `standardLanguages` that no bundled grammar covers yet. */
32
+ export declare const unsupportedLanguages: string[];
33
+ /**
34
+ * Applies the lowlight alias table. The result is what lands in the
35
+ * `language-*` class, whether or not we can highlight it.
36
+ */
37
+ export declare const canonicalName: (lang: string) => string;
38
+ /**
39
+ * Finds a grammar for a highlight.js language name, or `undefined`.
40
+ *
41
+ * Never throws. An unknown language has to degrade to unhighlighted code the
42
+ * way `rehype-highlight` does — a docs page with one exotic fence should not
43
+ * lose the whole code block.
44
+ */
45
+ export declare const grammarFor: (lang: string) => CompiledGrammar | undefined;
46
+ //# sourceMappingURL=languages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"languages.d.ts","sourceRoot":"","sources":["../../src/compat/languages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAEpD;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAQ3D,CAAA;AA2HD;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB,keAwD1B,CAAA;AAEV,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,CAAC,CAAA;AAE1E;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,EAEzB,MAAM,CAAC,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC,CAAA;AAE/C,gFAAgF;AAChF,eAAO,MAAM,oBAAoB,EAAE,MAAM,EAAiE,CAAA;AAE1G;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,KAAG,MAK5C,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SAG3D,CAAA"}
@@ -0,0 +1,236 @@
1
+ import { getLanguage } from '../core/registry.js';
2
+ /**
3
+ * `lowlightLanguageMappings`, copied verbatim from `@scalar/code-highlight`.
4
+ *
5
+ * This runs before anything else and decides the `language-*` class on the
6
+ * `<code>` element, so it has to stay byte-identical: `code.css` targets
7
+ * `.hljs.language-html`, `.hljs.language-curl` and friends by name.
8
+ */
9
+ export const lowlightLanguageMappings = {
10
+ ts: 'typescript',
11
+ js: 'javascript',
12
+ py: 'python',
13
+ py3: 'python',
14
+ 'c#': 'csharp',
15
+ 'c++': 'cpp',
16
+ node: 'javascript',
17
+ };
18
+ /**
19
+ * highlight.js language names -> the grammar in this package that covers them.
20
+ *
21
+ * One grammar answers to several highlight.js names, which is most of why
22
+ * forty grammars reach as far as they do: `xml`, `svg` and `vue` are all the
23
+ * HTML grammar, `ts`/`tsx`/`jsx` are all the JavaScript grammar.
24
+ */
25
+ const GRAMMAR_FOR = {
26
+ bash: 'bash',
27
+ c: 'c',
28
+ 'c#': 'csharp',
29
+ 'c++': 'cpp',
30
+ cc: 'cpp',
31
+ cjs: 'javascript',
32
+ clj: 'clojure',
33
+ clojure: 'clojure',
34
+ console: 'bash',
35
+ cpp: 'cpp',
36
+ cs: 'csharp',
37
+ csharp: 'csharp',
38
+ css: 'css',
39
+ // A curl invocation is a shell command line, so the bash grammar is the
40
+ // honest answer. `code.css` still targets `.hljs.language-curl` by name —
41
+ // the class comes from what the caller asked for, not from the grammar.
42
+ curl: 'bash',
43
+ cxx: 'cpp',
44
+ dart: 'dart',
45
+ diff: 'diff',
46
+ docker: 'dockerfile',
47
+ dockerfile: 'dockerfile',
48
+ elixir: 'elixir',
49
+ ex: 'elixir',
50
+ exs: 'elixir',
51
+ 'f#': 'fsharp',
52
+ fs: 'fsharp',
53
+ fsharp: 'fsharp',
54
+ go: 'go',
55
+ golang: 'go',
56
+ gql: 'graphql',
57
+ graphql: 'graphql',
58
+ h: 'c',
59
+ 'h++': 'cpp',
60
+ haskell: 'haskell',
61
+ hpp: 'cpp',
62
+ hs: 'haskell',
63
+ html: 'html',
64
+ http: 'http',
65
+ https: 'http',
66
+ hxx: 'cpp',
67
+ ini: 'ini',
68
+ java: 'java',
69
+ javascript: 'javascript',
70
+ js: 'javascript',
71
+ json: 'json',
72
+ json5: 'json',
73
+ jsonc: 'json',
74
+ jsx: 'javascript',
75
+ kotlin: 'kotlin',
76
+ kt: 'kotlin',
77
+ less: 'css',
78
+ lua: 'lua',
79
+ make: 'makefile',
80
+ makefile: 'makefile',
81
+ markdown: 'markdown',
82
+ matlab: 'matlab',
83
+ md: 'markdown',
84
+ mdx: 'markdown',
85
+ mjs: 'javascript',
86
+ mk: 'makefile',
87
+ ml: 'ocaml',
88
+ mojo: 'mojo',
89
+ mysql: 'sql',
90
+ nginx: 'nginx',
91
+ nginxconf: 'nginx',
92
+ 'obj-c': 'objectivec',
93
+ objc: 'objectivec',
94
+ objectivec: 'objectivec',
95
+ ocaml: 'ocaml',
96
+ octave: 'matlab',
97
+ patch: 'diff',
98
+ perl: 'perl',
99
+ php: 'php',
100
+ pl: 'perl',
101
+ pm: 'perl',
102
+ posh: 'powershell',
103
+ postgres: 'sql',
104
+ postgresql: 'sql',
105
+ powershell: 'powershell',
106
+ properties: 'ini',
107
+ ps: 'powershell',
108
+ ps1: 'powershell',
109
+ pwsh: 'powershell',
110
+ py: 'python',
111
+ py3: 'python',
112
+ python: 'python',
113
+ python3: 'python',
114
+ r: 'r',
115
+ rb: 'ruby',
116
+ rs: 'rust',
117
+ ruby: 'ruby',
118
+ rust: 'rust',
119
+ sbt: 'scala',
120
+ scala: 'scala',
121
+ scss: 'css',
122
+ sh: 'bash',
123
+ shell: 'bash',
124
+ sql: 'sql',
125
+ sqlite: 'sql',
126
+ svg: 'html',
127
+ swift: 'swift',
128
+ toml: 'ini',
129
+ ts: 'javascript',
130
+ tsx: 'javascript',
131
+ typescript: 'javascript',
132
+ vue: 'html',
133
+ xml: 'html',
134
+ yaml: 'yaml',
135
+ yml: 'yaml',
136
+ zsh: 'bash',
137
+ };
138
+ /**
139
+ * Every key of `standardLanguages` in `@scalar/code-highlight`.
140
+ *
141
+ * Kept so `keyof typeof standardLanguages` — which is how `ScalarCodeBlock`
142
+ * derives its `StandardLanguageKey` prop type — resolves to the same union
143
+ * after a swap. The names we have no grammar for stay in the list: they
144
+ * render as plain escaped text, which is what an unregistered language does
145
+ * in the lowlight pipeline today.
146
+ */
147
+ export const STANDARD_LANGUAGE_NAMES = [
148
+ 'bash',
149
+ 'c',
150
+ 'clojure',
151
+ 'cpp',
152
+ 'csharp',
153
+ 'css',
154
+ 'curl',
155
+ 'dart',
156
+ 'diff',
157
+ 'docker',
158
+ 'dockerfile',
159
+ 'elixir',
160
+ 'fsharp',
161
+ 'go',
162
+ 'graphql',
163
+ 'haskell',
164
+ 'html',
165
+ 'http',
166
+ 'ini',
167
+ 'java',
168
+ 'javascript',
169
+ 'json',
170
+ // Kept in `StandardLanguageKey` for parity with `@scalar/code-highlight`,
171
+ // which highlights Julia via highlight.js. This zero-dependency package has
172
+ // no hand-written Julia grammar yet, so it stays in `unsupportedLanguages`
173
+ // and renders as plain escaped text until one is added.
174
+ 'julia',
175
+ 'kotlin',
176
+ 'less',
177
+ 'lua',
178
+ 'makefile',
179
+ 'markdown',
180
+ 'matlab',
181
+ 'mojo',
182
+ 'nginx',
183
+ 'objectivec',
184
+ 'ocaml',
185
+ 'perl',
186
+ 'php',
187
+ 'plaintext',
188
+ 'powershell',
189
+ 'properties',
190
+ 'python',
191
+ 'r',
192
+ 'ruby',
193
+ 'rust',
194
+ 'scala',
195
+ 'scss',
196
+ 'shell',
197
+ 'sql',
198
+ 'swift',
199
+ 'toml',
200
+ 'typescript',
201
+ 'xml',
202
+ 'yaml',
203
+ ];
204
+ /**
205
+ * Stand-in for `standardLanguages`.
206
+ *
207
+ * The lowlight version maps each name to a highlight.js `LanguageFn`; this one
208
+ * maps to our grammar name, or `null` where we have no grammar yet. Callers
209
+ * pass it straight back into `syntaxHighlight`, which ignores it — the
210
+ * registry already knows what is loaded — so the value only has to carry the
211
+ * key set and the coverage answer.
212
+ */
213
+ export const standardLanguages = Object.fromEntries(STANDARD_LANGUAGE_NAMES.map((name) => [name, GRAMMAR_FOR[name] ?? null]));
214
+ /** Language names in `standardLanguages` that no bundled grammar covers yet. */
215
+ export const unsupportedLanguages = STANDARD_LANGUAGE_NAMES.filter((name) => !GRAMMAR_FOR[name]);
216
+ /**
217
+ * Applies the lowlight alias table. The result is what lands in the
218
+ * `language-*` class, whether or not we can highlight it.
219
+ */
220
+ export const canonicalName = (lang) => {
221
+ // `lang` reaches here straight from a markdown fence, so `constructor` and
222
+ // `__proto__` are names a document can ask for. A bare index would return
223
+ // something off Object.prototype rather than undefined.
224
+ return Object.hasOwn(lowlightLanguageMappings, lang) ? (lowlightLanguageMappings[lang] ?? lang) : lang;
225
+ };
226
+ /**
227
+ * Finds a grammar for a highlight.js language name, or `undefined`.
228
+ *
229
+ * Never throws. An unknown language has to degrade to unhighlighted code the
230
+ * way `rehype-highlight` does — a docs page with one exotic fence should not
231
+ * lose the whole code block.
232
+ */
233
+ export const grammarFor = (lang) => {
234
+ const key = lang.toLowerCase();
235
+ return getLanguage(Object.hasOwn(GRAMMAR_FOR, key) ? (GRAMMAR_FOR[key] ?? key) : key);
236
+ };
@@ -0,0 +1,37 @@
1
+ export type SyntaxHighlightOptions = {
2
+ /** Language name, in highlight.js vocabulary. */
3
+ lang: string;
4
+ /**
5
+ * Accepted and ignored.
6
+ *
7
+ * The lowlight version needs a `Record<string, LanguageFn>` because it
8
+ * builds a fresh lowlight instance per call. Here the registry already knows
9
+ * what is loaded, so the argument only exists to keep call sites compiling.
10
+ */
11
+ languages?: unknown;
12
+ /** Wrap each line in `<span class="line">` and set the gutter variables. */
13
+ lineNumbers?: boolean;
14
+ /** Substrings to blank out behind a `.credential` span. */
15
+ maskCredentials?: string | string[];
16
+ };
17
+ /**
18
+ * Drop-in replacement for `syntaxHighlight` from `@scalar/code-highlight`.
19
+ *
20
+ * Same signature, same `<pre><code class="hljs language-x">` envelope, same
21
+ * `hljs-*` token classes, so `code.css` and every consumer of the returned
22
+ * string keep working. What changes is underneath: our state machine instead
23
+ * of a lowlight instance built per call.
24
+ *
25
+ * An unknown language degrades to escaped, unhighlighted code — still carrying
26
+ * the `hljs` class, because that is what the rehype plugin leaves behind when
27
+ * `lowlight.highlight` throws `Unknown language`.
28
+ *
29
+ * `lang` reaches the `class` attribute, and it is not always trusted — in a
30
+ * docs pipeline it is the info string of a fenced block in someone else's
31
+ * document. It is escaped rather than passed through: every real language name
32
+ * is unchanged by that, so the envelope stays byte-identical to the lowlight
33
+ * one, but a name carrying a quote can no longer close the attribute and open
34
+ * an element of its own.
35
+ */
36
+ export declare const syntaxHighlight: (codeString: string, options: SyntaxHighlightOptions) => string;
37
+ //# sourceMappingURL=syntax-highlight.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"syntax-highlight.d.ts","sourceRoot":"","sources":["../../src/compat/syntax-highlight.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,sBAAsB,GAAG;IACnC,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAA;IACZ;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,4EAA4E;IAC5E,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,2DAA2D;IAC3D,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,CAAA;AA+HD;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,eAAe,GAAI,YAAY,MAAM,EAAE,SAAS,sBAAsB,KAAG,MAYrF,CAAA"}
@@ -0,0 +1,133 @@
1
+ import { escapeAttribute, escapeHtml } from '../core/render.js';
2
+ import { tokenize } from '../core/tokenize.js';
3
+ import { hljsClass } from './hljs.js';
4
+ import { canonicalName, grammarFor } from './languages.js';
5
+ /** Opening tags, built once per class attribute rather than once per span. */
6
+ const tags = new Map();
7
+ const openTag = (cls) => {
8
+ let tag = tags.get(cls);
9
+ if (tag === undefined) {
10
+ tag = `<span class="${cls}">`;
11
+ tags.set(cls, tag);
12
+ }
13
+ return tag;
14
+ };
15
+ /**
16
+ * Renders `code` to highlight.js-shaped markup, optionally split into lines.
17
+ *
18
+ * One tokenizer pass over the whole string, with line breaks handled as they
19
+ * are reached. Tokenizing line by line would be simpler and wrong: a block
20
+ * comment or a template literal carries state across a newline, and a
21
+ * per-line tokenizer would restart in the root state on line two.
22
+ *
23
+ * Adjacent ranges that resolve to the same class are merged. Our scope
24
+ * vocabulary is finer than highlight.js's, so `keyword.declaration` followed
25
+ * by `keyword` has to collapse into one `hljs-keyword` span rather than the
26
+ * two neighbours lowlight would never have emitted.
27
+ */
28
+ const render = (code, grammar, lineMode) => {
29
+ const lines = [];
30
+ let line = '';
31
+ // Pending run of same-classed ranges, flushed when the class changes.
32
+ let cls = null;
33
+ let start = 0;
34
+ let end = 0;
35
+ const flush = () => {
36
+ if (end > start) {
37
+ const text = escapeHtml(code.slice(start, end));
38
+ line += cls === null ? text : `${openTag(cls)}${text}</span>`;
39
+ }
40
+ start = end;
41
+ };
42
+ const endLine = () => {
43
+ flush();
44
+ lines.push(line);
45
+ line = '';
46
+ };
47
+ const emit = (scope, s, e) => {
48
+ const next = scope === null ? null : hljsClass(scope);
49
+ let at = s;
50
+ while (at < e) {
51
+ const nl = lineMode ? code.indexOf('\n', at) : -1;
52
+ const stop = nl === -1 || nl >= e ? e : nl;
53
+ if (stop > at) {
54
+ if (next !== cls || at !== end) {
55
+ flush();
56
+ cls = next;
57
+ start = at;
58
+ }
59
+ end = stop;
60
+ }
61
+ if (stop === e)
62
+ break;
63
+ // Landed on a newline: close the line and restart after it. The newline
64
+ // itself is written by the wrapper, not carried as token text.
65
+ endLine();
66
+ at = stop + 1;
67
+ cls = null;
68
+ start = at;
69
+ end = at;
70
+ }
71
+ };
72
+ if (grammar) {
73
+ tokenize(code, grammar, emit);
74
+ }
75
+ else {
76
+ // No grammar for this language — the whole block is one unscoped range,
77
+ // which still needs splitting into lines.
78
+ emit(null, 0, code.length);
79
+ }
80
+ endLine();
81
+ if (!lineMode)
82
+ return { html: lines.join(''), count: lines.length };
83
+ let html = '';
84
+ for (const content of lines)
85
+ html += `<span class="line">${content}\n</span>`;
86
+ return { html, count: lines.length };
87
+ };
88
+ /**
89
+ * Credentials shorter than this are not masked — too likely to appear inside
90
+ * unrelated tokens, and a two-character secret is not a secret.
91
+ */
92
+ const MIN_CREDENTIAL_LENGTH = 3;
93
+ /**
94
+ * Replaces credential substrings in the finished HTML.
95
+ *
96
+ * Post-processing the serialized string, rather than the token stream, is what
97
+ * `@scalar/code-highlight` does. It is kept here deliberately: a credential
98
+ * that straddles a token boundary masks or fails to mask exactly as it does
99
+ * today, so a swap cannot change what is visible on screen.
100
+ */
101
+ const applyCredentialMask = (html, credentials) => {
102
+ const list = (typeof credentials === 'string' ? [credentials] : (credentials ?? [])).filter((credential) => credential.length >= MIN_CREDENTIAL_LENGTH);
103
+ if (!list.length)
104
+ return html;
105
+ return list.reduce((acc, credential) => acc.split(credential).join(`<span class="credential"><span class="credential-value">${credential}</span></span>`), html);
106
+ };
107
+ /**
108
+ * Drop-in replacement for `syntaxHighlight` from `@scalar/code-highlight`.
109
+ *
110
+ * Same signature, same `<pre><code class="hljs language-x">` envelope, same
111
+ * `hljs-*` token classes, so `code.css` and every consumer of the returned
112
+ * string keep working. What changes is underneath: our state machine instead
113
+ * of a lowlight instance built per call.
114
+ *
115
+ * An unknown language degrades to escaped, unhighlighted code — still carrying
116
+ * the `hljs` class, because that is what the rehype plugin leaves behind when
117
+ * `lowlight.highlight` throws `Unknown language`.
118
+ *
119
+ * `lang` reaches the `class` attribute, and it is not always trusted — in a
120
+ * docs pipeline it is the info string of a fenced block in someone else's
121
+ * document. It is escaped rather than passed through: every real language name
122
+ * is unchanged by that, so the envelope stays byte-identical to the lowlight
123
+ * one, but a name carrying a quote can no longer close the attribute and open
124
+ * an element of its own.
125
+ */
126
+ export const syntaxHighlight = (codeString, options) => {
127
+ const name = canonicalName(options.lang);
128
+ const grammar = grammarFor(name);
129
+ const lineMode = options.lineNumbers === true;
130
+ const { html, count } = render(codeString, grammar, lineMode);
131
+ const style = lineMode ? ` style="--line-count: ${count}; --line-digits: ${count.toString().length};"` : '';
132
+ return applyCredentialMask(`<pre><code class="hljs language-${escapeAttribute(name)}"${style}>${html}</code></pre>`, options.maskCredentials);
133
+ };
@@ -0,0 +1,3 @@
1
+ import type { CompiledGrammar, Grammar } from './types.js';
2
+ export declare const compile: (grammar: Grammar) => CompiledGrammar;
3
+ //# sourceMappingURL=compile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/core/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA+B,OAAO,EAAmB,MAAM,SAAS,CAAA;AAoIrG,eAAO,MAAM,OAAO,GAAI,SAAS,OAAO,KAAG,eA8B1C,CAAA"}
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Compiles a grammar into one merged regex per state.
3
+ *
4
+ * The merge is the whole performance story. Instead of trying N patterns at
5
+ * each position from JS, we hand the engine a single ordered alternation and
6
+ * let it find the next interesting position itself — plain text between tokens
7
+ * is skipped inside the regex engine rather than by a JS loop.
8
+ *
9
+ * Each rule is wrapped in its own group so that, after a match, we can tell
10
+ * which alternative won by finding the first defined wrapper group.
11
+ */
12
+ const BACKREF = /\\(?:[1-9]|k<)/;
13
+ /** Counts capture groups in a pattern, ignoring escapes and character classes. */
14
+ const countGroups = (src) => {
15
+ let count = 0;
16
+ let inClass = false;
17
+ for (let i = 0; i < src.length; i++) {
18
+ const c = src.charCodeAt(i);
19
+ if (c === 92 /* \ */) {
20
+ i++;
21
+ continue;
22
+ }
23
+ if (inClass) {
24
+ if (c === 93 /* ] */)
25
+ inClass = false;
26
+ continue;
27
+ }
28
+ if (c === 91 /* [ */) {
29
+ inClass = true;
30
+ continue;
31
+ }
32
+ if (c !== 40 /* ( */)
33
+ continue;
34
+ if (src.charCodeAt(i + 1) !== 63 /* ? */) {
35
+ count++;
36
+ }
37
+ else if (src.charCodeAt(i + 2) === 60 /* < */) {
38
+ // (?<name> captures; (?<= and (?<! are lookbehind.
39
+ const after = src.charCodeAt(i + 3);
40
+ if (after !== 61 /* = */ && after !== 33 /* ! */)
41
+ count++;
42
+ }
43
+ }
44
+ return count;
45
+ };
46
+ const patternSource = (match) => {
47
+ return typeof match === 'string' ? match : match.source;
48
+ };
49
+ /** Resolves `include` entries into a flat, priority-ordered rule list. */
50
+ const flatten = (grammarName, states, name, seen) => {
51
+ if (seen.has(name)) {
52
+ throw new Error(`[${grammarName}] circular include of state "${name}"`);
53
+ }
54
+ seen.add(name);
55
+ // `hasOwn` because a grammar is data a caller supplies: an `include` of
56
+ // `constructor` would otherwise resolve off Object.prototype and sail past
57
+ // this check, failing later with something far less legible.
58
+ const state = Object.hasOwn(states, name) ? states[name] : undefined;
59
+ if (!state)
60
+ throw new Error(`[${grammarName}] include of unknown state "${name}"`);
61
+ const out = [];
62
+ for (const entry of state.rules) {
63
+ if ('include' in entry) {
64
+ out.push(...flatten(grammarName, states, entry.include, seen));
65
+ }
66
+ else {
67
+ out.push(entry);
68
+ }
69
+ }
70
+ seen.delete(name);
71
+ return out;
72
+ };
73
+ const compileState = (grammar, name) => {
74
+ const state = grammar.states[name];
75
+ const rules = flatten(grammar.name, grammar.states, name, new Set());
76
+ const compiled = [];
77
+ const sources = [];
78
+ // Group 0 is the whole match, so rule wrapper groups start at 1.
79
+ let group = 1;
80
+ const caseFlag = state.ignoreCase ? 'i' : '';
81
+ for (const rule of rules) {
82
+ const src = patternSource(rule.match);
83
+ if (BACKREF.test(src)) {
84
+ throw new Error(`[${grammar.name}:${name}] backreferences are not supported (group numbers ` +
85
+ `shift when rules are merged): ${src}`);
86
+ }
87
+ const captures = Array.isArray(rule.scope) ? rule.scope : null;
88
+ const inner = countGroups(src);
89
+ if (captures) {
90
+ if (captures.length > inner) {
91
+ throw new Error(`[${grammar.name}:${name}] rule scopes ${captures.length} groups but the ` + `pattern has ${inner}: ${src}`);
92
+ }
93
+ }
94
+ compiled.push({
95
+ scope: typeof rule.scope === 'string' ? rule.scope : null,
96
+ captures,
97
+ // Sticky, so it can be re-run at the position the merged regex chose.
98
+ own: captures ? new RegExp(src, `ymd${caseFlag}`) : null,
99
+ rest: rule.rest ?? null,
100
+ group,
101
+ push: rule.push ?? null,
102
+ set: rule.set ?? null,
103
+ pop: rule.pop === true ? 1 : (rule.pop ?? 0),
104
+ });
105
+ sources.push(`(${src})`);
106
+ group += 1 + inner;
107
+ }
108
+ // `m` so grammars can anchor to lines. Deliberately no `d`: offsets are
109
+ // resolved per rule instead, which keeps the scan free of that cost.
110
+ const flags = `gm${caseFlag}`;
111
+ // An empty state matches nothing: everything falls through to `default`.
112
+ const re = new RegExp(sources.length ? sources.join('|') : '(?!)', flags);
113
+ return { name, re, rules: compiled, default: state.default ?? null };
114
+ };
115
+ export const compile = (grammar) => {
116
+ if (!Object.hasOwn(grammar.states, 'root')) {
117
+ throw new Error(`[${grammar.name}] grammar has no "root" state`);
118
+ }
119
+ // Null-prototype: state names come from grammar data, and the unknown-state
120
+ // check below has to see `constructor` and `__proto__` as missing rather
121
+ // than as inherited Object members. Tokenizing looks states up here too.
122
+ const states = Object.create(null);
123
+ for (const name of Object.keys(grammar.states)) {
124
+ states[name] = compileState(grammar, name);
125
+ }
126
+ // Fail loudly at compile time rather than mid-tokenize on a rare branch.
127
+ for (const state of Object.values(states)) {
128
+ for (const rule of state.rules) {
129
+ for (const target of [rule.push, rule.set]) {
130
+ if (target && !states[target]) {
131
+ throw new Error(`[${grammar.name}:${state.name}] transition to unknown state "${target}"`);
132
+ }
133
+ }
134
+ }
135
+ }
136
+ return {
137
+ name: grammar.name,
138
+ aliases: grammar.aliases ?? [],
139
+ states,
140
+ root: states['root'],
141
+ };
142
+ };
@@ -0,0 +1,9 @@
1
+ export { compile } from './compile.js';
2
+ export { getLanguage, isRegistered, listLanguages, registerLanguage, resolveGrammar, } from './registry.js';
3
+ export type { BlockOptions, RenderOptions } from './render.js';
4
+ export { DEFAULT_PREFIX, escapeAttribute, escapeHtml, highlight as highlightWith, highlightBlock as highlightBlockWith, } from './render.js';
5
+ export type { Scope } from './scopes.js';
6
+ export { SCOPES, SCOPE_NAMES, scopeChain, scopeClass } from './scopes.js';
7
+ export { tokenize as tokenizeWith, tokenizeToArray } from './tokenize.js';
8
+ export type { CompiledGrammar, Emit, Grammar, Include, Rule, ScopeName, State, StateRule, Token, } from './types.js';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EACL,WAAW,EACX,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,MAAM,YAAY,CAAA;AACnB,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAC3D,OAAO,EACL,cAAc,EACd,eAAe,EACf,UAAU,EACV,SAAS,IAAI,aAAa,EAC1B,cAAc,IAAI,kBAAkB,GACrC,MAAM,UAAU,CAAA;AACjB,YAAY,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AACrC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AACtE,OAAO,EAAE,QAAQ,IAAI,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AACtE,YAAY,EACV,eAAe,EACf,IAAI,EACJ,OAAO,EACP,OAAO,EACP,IAAI,EACJ,SAAS,EACT,KAAK,EACL,SAAS,EACT,KAAK,GACN,MAAM,SAAS,CAAA"}
@@ -0,0 +1,5 @@
1
+ export { compile } from './compile.js';
2
+ export { getLanguage, isRegistered, listLanguages, registerLanguage, resolveGrammar, } from './registry.js';
3
+ export { DEFAULT_PREFIX, escapeAttribute, escapeHtml, highlight as highlightWith, highlightBlock as highlightBlockWith, } from './render.js';
4
+ export { SCOPES, SCOPE_NAMES, scopeChain, scopeClass } from './scopes.js';
5
+ export { tokenize as tokenizeWith, tokenizeToArray } from './tokenize.js';
@@ -0,0 +1,14 @@
1
+ import type { CompiledGrammar, Grammar } from './types.js';
2
+ export declare const registerLanguage: (...grammars: Grammar[]) => void;
3
+ export declare const isRegistered: (name: string) => boolean;
4
+ /** Canonical names of registered languages, without aliases. */
5
+ export declare const listLanguages: () => string[];
6
+ /** Looks up and compiles a language, memoizing the compiled form. */
7
+ export declare const getLanguage: (name: string) => CompiledGrammar | undefined;
8
+ /**
9
+ * Accepts a registered language name, a grammar, or an already-compiled
10
+ * grammar. Throws on an unknown name — silently rendering unhighlighted code
11
+ * hides typos in a `lang` prop for a long time.
12
+ */
13
+ export declare const resolveGrammar: (lang: string | Grammar | CompiledGrammar) => CompiledGrammar;
14
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/core/registry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAYvD,eAAO,MAAM,gBAAgB,GAAI,GAAG,UAAU,OAAO,EAAE,KAAG,IAuBzD,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAE3C,CAAA;AAED,gEAAgE;AAChE,eAAO,MAAM,aAAa,QAAO,MAAM,EAEtC,CAAA;AAMD,qEAAqE;AACrE,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SAc5D,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,GAAG,OAAO,GAAG,eAAe,KAAG,eAczE,CAAA"}