@scalar/highlight 0.3.1 → 0.4.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 (61) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +52 -13
  3. package/dist/all.d.ts.map +1 -1
  4. package/dist/all.js +2 -0
  5. package/dist/compat/languages.d.ts.map +1 -1
  6. package/dist/compat/languages.js +2 -4
  7. package/dist/compat/syntax-highlight.d.ts.map +1 -1
  8. package/dist/compat/syntax-highlight.js +101 -27
  9. package/dist/core/compile.d.ts +9 -1
  10. package/dist/core/compile.d.ts.map +1 -1
  11. package/dist/core/compile.js +120 -6
  12. package/dist/core/index.d.ts +1 -0
  13. package/dist/core/index.d.ts.map +1 -1
  14. package/dist/core/index.js +1 -0
  15. package/dist/core/tokenize.d.ts.map +1 -1
  16. package/dist/core/tokenize.js +143 -7
  17. package/dist/core/types.d.ts +74 -0
  18. package/dist/core/types.d.ts.map +1 -1
  19. package/dist/core/types.js +5 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -0
  23. package/dist/langs/bash.d.ts.map +1 -1
  24. package/dist/langs/bash.js +60 -13
  25. package/dist/langs/csharp.d.ts.map +1 -1
  26. package/dist/langs/csharp.js +11 -5
  27. package/dist/langs/css.d.ts.map +1 -1
  28. package/dist/langs/css.js +84 -7
  29. package/dist/langs/go.d.ts.map +1 -1
  30. package/dist/langs/go.js +7 -1
  31. package/dist/langs/html.d.ts +0 -10
  32. package/dist/langs/html.d.ts.map +1 -1
  33. package/dist/langs/html.js +63 -24
  34. package/dist/langs/http.d.ts.map +1 -1
  35. package/dist/langs/http.js +25 -9
  36. package/dist/langs/javascript.d.ts.map +1 -1
  37. package/dist/langs/javascript.js +146 -30
  38. package/dist/langs/json.d.ts.map +1 -1
  39. package/dist/langs/json.js +7 -1
  40. package/dist/langs/julia.d.ts +4 -0
  41. package/dist/langs/julia.d.ts.map +1 -0
  42. package/dist/langs/julia.js +181 -0
  43. package/dist/langs/markdown.d.ts +9 -3
  44. package/dist/langs/markdown.d.ts.map +1 -1
  45. package/dist/langs/markdown.js +127 -6
  46. package/dist/langs/ocaml.d.ts.map +1 -1
  47. package/dist/langs/ocaml.js +16 -7
  48. package/dist/langs/php.d.ts.map +1 -1
  49. package/dist/langs/php.js +69 -19
  50. package/dist/langs/r.js +9 -9
  51. package/dist/langs/rust.d.ts.map +1 -1
  52. package/dist/langs/rust.js +19 -10
  53. package/dist/langs/sql.d.ts.map +1 -1
  54. package/dist/langs/sql.js +9 -3
  55. package/dist/langs/yaml.d.ts +0 -7
  56. package/dist/langs/yaml.d.ts.map +1 -1
  57. package/dist/langs/yaml.js +157 -28
  58. package/dist/lazy.d.ts +1 -0
  59. package/dist/lazy.d.ts.map +1 -1
  60. package/dist/lazy.js +2 -0
  61. package/package.json +2 -2
@@ -4,41 +4,104 @@
4
4
  * Keys are the load-bearing thing in a YAML file, so they are matched from the
5
5
  * line start and scoped separately from their values. Block scalars (`|`, `>`)
6
6
  * switch into a literal state so their contents are not re-parsed as YAML.
7
+ *
8
+ * A block scalar ends where the indentation does: at the first non-blank line
9
+ * indented no further than its key. The key's indentation is remembered when
10
+ * the block opens, so prose inside a `description: |` can say "Note: …"
11
+ * without being read as a new key.
12
+ */
13
+ /**
14
+ * A mapping key, quoted or bare.
15
+ *
16
+ * The bare form is length-capped rather than open-ended. It is tried at every
17
+ * column, and an uncapped lazy scan runs to the end of the line before failing
18
+ * on a line with no `:`, which is quadratic in line length — a 20 KB
19
+ * single-line value is seconds of blocking. No real key approaches the cap;
20
+ * past it the text simply renders unscoped. A bare key cannot start with a
21
+ * bracket, a comma or a quote: those are flow and string syntax.
7
22
  */
23
+ const KEY = `(?:"[^"\\n]*"|'[^'\\n]*'|[^\\s#:\\[\\]{},"'][^:\\n]{0,256}?)`;
24
+ /** Inside `[…]` and `{…}`, where commas and brackets end a bare key. */
25
+ const FLOW_KEY = `(?:"[^"\\n]*"|'[^'\\n]*'|[^\\s#:,\\[\\]{}][^:,\\[\\]{}\\n]{0,256}?)`;
26
+ /** A block scalar indicator with its optional chomping and indentation hints. */
27
+ const BLOCK = '[|>][-+1-9]{0,2}';
28
+ /** The rest of a line that opens a block scalar: blanks and an optional comment. */
29
+ const BLOCK_TAIL = '(?:([ \\t]+)(#[^\\n]*))?(?=[ \\t]*$)';
30
+ /** A comment starts at `#` only after whitespace, so `docs#section` stays one scalar. */
31
+ const COMMENT = { match: '(?<![^\\s])#[^\\n]*', scope: 'comment' };
8
32
  const yaml = {
9
33
  name: 'yaml',
10
34
  aliases: ['yml'],
11
35
  states: {
12
36
  root: {
13
37
  rules: [
14
- { match: '#[^\\n]*', scope: 'comment' },
15
- { match: '^(?:---|\\.\\.\\.)$', scope: 'punctuation.delimiter' },
38
+ COMMENT,
39
+ { match: '^(?:---|\\.\\.\\.)(?=[ \\t]|$)', scope: 'punctuation.delimiter' },
40
+ // Block scalars, before the plain key rule. Each remembers the column
41
+ // its content has to stay to the right of.
42
+ //
43
+ // `- key: |` — the column is the key's, not the dash's. The dash and
44
+ // its indent share one scope: whitespace shows no color either way.
45
+ {
46
+ match: `^([ \\t]*-[ \\t]+)(${KEY})([ \\t]*)(:)([ \\t]+)(${BLOCK})${BLOCK_TAIL}`,
47
+ scope: [
48
+ 'punctuation.delimiter',
49
+ 'property',
50
+ null,
51
+ 'punctuation.delimiter',
52
+ null,
53
+ 'operator',
54
+ null,
55
+ 'comment',
56
+ ],
57
+ push: 'block-scalar',
58
+ remember: 1,
59
+ rememberAs: 'indent',
60
+ },
61
+ // `- |` — a list item that is itself a block scalar.
62
+ {
63
+ match: `^([ \\t]*)(-)([ \\t]+)(${BLOCK})${BLOCK_TAIL}`,
64
+ scope: [null, 'punctuation.delimiter', null, 'operator', null, 'comment'],
65
+ push: 'block-scalar',
66
+ remember: 1,
67
+ rememberAs: 'indent',
68
+ },
69
+ // `key: |`
70
+ {
71
+ match: `^([ \\t]*)(?!-[ \\t])(${KEY})([ \\t]*)(:)([ \\t]+)(${BLOCK})${BLOCK_TAIL}`,
72
+ scope: [null, 'property', null, 'punctuation.delimiter', null, 'operator', null, 'comment'],
73
+ push: 'block-scalar',
74
+ remember: 1,
75
+ rememberAs: 'indent',
76
+ },
16
77
  {
17
78
  match: '^([ \\t]*)(-)(?=[ \\t]|$)',
18
79
  scope: [null, 'punctuation.delimiter'],
19
80
  push: 'value',
20
81
  },
82
+ // A plain scalar may wrap: an indented line that is not a key, a list
83
+ // item or a comment continues the value above it. OpenAPI descriptions
84
+ // wrap like this all the time, and without the rule every line after
85
+ // the first lost its string color. The first character leaves out
86
+ // everything that starts other syntax on a line of its own.
87
+ {
88
+ match: `^([ \\t]+)(?!${KEY}[ \\t]*:(?:[ \\t]|$)|[-?:][ \\t]|-$)([^\\s#\\[\\]{}"'&*!|>%@\`][^\\s]*(?:[ \\t]+[^\\s#][^\\s]*)*)`,
89
+ scope: [null, 'string'],
90
+ },
21
91
  // `key:` — quoted or bare, optionally after a list dash.
22
- //
23
- // The bare form is length-capped rather than open-ended. It is tried at
24
- // every column, and an uncapped lazy scan runs to the end of the line
25
- // before failing on a line with no `:`, which is quadratic in line
26
- // length — a 20 KB single-line value is seconds of blocking. No real
27
- // key approaches the cap; past it the text simply renders unscoped.
28
92
  {
29
- match: '((?:"[^"\\n]*"|\'[^\'\\n]*\'|[^\\s#:][^:\\n]{0,256}?))([ \\t]*)(:)(?=[ \\t]|$)',
93
+ match: `(${KEY})([ \\t]*)(:)(?=[ \\t]|$)`,
30
94
  scope: ['property', null, 'punctuation.delimiter'],
31
95
  push: 'value',
32
96
  },
33
97
  { match: '[&*][\\w-]+', scope: 'variable.special' },
34
98
  { match: '!!?[\\w/-]+', scope: 'type' },
35
99
  {
36
- match: '([|>][-+]?)([ \\t]*)$',
100
+ match: `(${BLOCK})([ \\t]*)$`,
37
101
  scope: ['operator', null],
38
- push: 'block-scalar',
102
+ push: 'block-scalar-loose',
39
103
  },
40
- { match: '"(?:[^"\\\\\\n]|\\\\.)*"', scope: 'string' },
41
- { match: "'(?:[^'\\n]|'')*'", scope: 'string' },
104
+ { include: 'strings' },
42
105
  {
43
106
  match: '\\b(?:true|false|yes|no|on|off|True|False|Yes|No|On|Off)\\b',
44
107
  scope: 'boolean',
@@ -48,7 +111,8 @@ const yaml = {
48
111
  match: '(?:^|[\\s,\\[{])(-?\\d+(?:\\.\\d+)?(?:[eE][-+]?\\d+)?)\\b',
49
112
  scope: ['number'],
50
113
  },
51
- { match: '[\\[\\]{}]', scope: 'punctuation.bracket' },
114
+ { match: '[\\[{]', scope: 'punctuation.bracket', push: 'flow' },
115
+ { match: '[\\]}]', scope: 'punctuation.bracket' },
52
116
  { match: ',', scope: 'punctuation.delimiter' },
53
117
  ],
54
118
  },
@@ -65,24 +129,62 @@ const yaml = {
65
129
  */
66
130
  value: {
67
131
  rules: [
68
- { match: '#[^\\n]*', scope: 'comment' },
132
+ COMMENT,
69
133
  {
70
- match: '([|>][-+]?)([ \\t]*)$',
134
+ match: `(${BLOCK})([ \\t]*)$`,
71
135
  scope: ['operator', null],
72
- set: 'block-scalar',
136
+ set: 'block-scalar-loose',
73
137
  },
74
138
  // `- uses: actions/checkout@v4` reaches here through the dash, so the
75
139
  // key form has to be recognised in value position too.
76
140
  {
77
- match: '((?:"[^"\\n]*"|\'[^\'\\n]*\'|[^\\s#:][^:\\n]{0,256}?))([ \\t]*)(:)(?=[ \\t]|$)',
141
+ match: `(${KEY})([ \\t]*)(:)(?=[ \\t]|$)`,
78
142
  scope: ['property', null, 'punctuation.delimiter'],
79
143
  },
80
- { match: '"(?:[^"\\\\\\n]|\\\\.)*"', scope: 'string' },
81
- { match: "'(?:[^'\\n]|'')*'", scope: 'string' },
144
+ { include: 'strings' },
82
145
  { match: '[&*][\\w-]+', scope: 'variable.special' },
83
146
  { match: '!!?[\\w/-]+', scope: 'type' },
84
147
  // Anchored to a value boundary so `no-cache` stays one plain scalar
85
148
  // rather than the boolean `no` followed by `-cache`.
149
+ {
150
+ match: '(?:true|false|yes|no|on|off|True|False|Yes|No|On|Off)(?=[ \\t]*(?:#|$))',
151
+ scope: 'boolean',
152
+ },
153
+ {
154
+ match: '(?:null|Null|NULL|~)(?=[ \\t]*(?:#|$))',
155
+ scope: 'constant.builtin',
156
+ },
157
+ {
158
+ match: '-?\\d+(?:\\.\\d+)?(?:[eE][-+]?\\d+)?(?=[ \\t]*(?:#|$))',
159
+ scope: 'number',
160
+ },
161
+ { match: '[\\[{]', scope: 'punctuation.bracket', push: 'flow' },
162
+ // The plain scalar itself: words separated by blanks, up to a comment
163
+ // or the end of the line. Commas and `#` without a blank before them
164
+ // are part of it in block context — `Hello, world` and
165
+ // `https://x.dev/docs#auth` are one value each. Every repetition starts
166
+ // at a blank, so the scan never backtracks into itself.
167
+ { match: '[^\\s#\\[\\]{}][^\\s]*(?:[ \\t]+[^\\s#][^\\s]*)*', scope: 'string' },
168
+ { match: '$', pop: true },
169
+ ],
170
+ },
171
+ /**
172
+ * A flow collection, `[…]` or `{…}`, which may span lines. Commas and
173
+ * brackets delimit here, which they do not in a block value.
174
+ */
175
+ flow: {
176
+ rules: [
177
+ COMMENT,
178
+ { match: '[\\[{]', scope: 'punctuation.bracket', push: 'flow' },
179
+ { match: '[\\]}]', scope: 'punctuation.bracket', pop: true },
180
+ { match: ',', scope: 'punctuation.delimiter' },
181
+ {
182
+ match: `(${FLOW_KEY})([ \\t]*)(:)(?=[ \\t]|$|[,\\]}])`,
183
+ scope: ['property', null, 'punctuation.delimiter'],
184
+ },
185
+ { include: 'strings' },
186
+ { match: '[&*][\\w-]+', scope: 'variable.special' },
187
+ { match: '!!?[\\w/-]+', scope: 'type' },
86
188
  {
87
189
  match: '(?:true|false|yes|no|on|off|True|False|Yes|No|On|Off)(?=[ \\t]*(?:[,\\]}]|#|$))',
88
190
  scope: 'boolean',
@@ -95,21 +197,48 @@ const yaml = {
95
197
  match: '-?\\d+(?:\\.\\d+)?(?:[eE][-+]?\\d+)?(?=[ \\t]*(?:[,\\]}]|#|$))',
96
198
  scope: 'number',
97
199
  },
98
- { match: '[\\[\\]{}]', scope: 'punctuation.bracket' },
99
- { match: ',', scope: 'punctuation.delimiter' },
100
- // The plain scalar itself. Greedy and unanchored on the right rather
101
- // than a capped lazy scan: a lazy `{0,n}?` here is retried at every
102
- // column and is what makes a long single-line value quadratic.
103
200
  { match: '[^\\s#,\\[\\]{}][^,\\[\\]{}\\n#]*', scope: 'string' },
201
+ ],
202
+ },
203
+ // Quoted scalars are states rather than one pattern each: a pattern that
204
+ // alternates escapes with other characters rescans to the end of the line
205
+ // for every unterminated quote, which is O(n²) on a line of `\"`.
206
+ strings: {
207
+ rules: [
208
+ { match: '"', scope: 'string', push: 'string-double' },
209
+ { match: "'", scope: 'string', push: 'string-single' },
210
+ ],
211
+ },
212
+ 'string-double': {
213
+ default: 'string',
214
+ rules: [
215
+ { match: '\\\\.', scope: 'string.escape' },
216
+ { match: '"', scope: 'string', pop: true },
217
+ { match: '$', pop: true },
218
+ ],
219
+ },
220
+ 'string-single': {
221
+ default: 'string',
222
+ rules: [
223
+ { match: "''", scope: 'string.escape' },
224
+ { match: "'", scope: 'string', pop: true },
104
225
  { match: '$', pop: true },
105
226
  ],
106
227
  },
107
228
  /**
108
- * A block scalar runs until a line indented no further than its
109
- * introducer. Approximated as "until a line that starts a new key", which
110
- * is what the content of a real document looks like.
229
+ * A block scalar whose key's column is remembered: it runs until the first
230
+ * non-blank line that does not indent past that column.
111
231
  */
112
232
  'block-scalar': {
233
+ default: 'string',
234
+ rules: [{ match: '^(?!{{remembered}}[ \\t])(?=[ \\t]*\\S)', pop: true }],
235
+ },
236
+ /**
237
+ * A block scalar opened somewhere its key's column is not known, such as
238
+ * after a tag or an anchor. Approximated as "until a line that starts a new
239
+ * key", which is what the content of a real document looks like.
240
+ */
241
+ 'block-scalar-loose': {
113
242
  default: 'string',
114
243
  rules: [
115
244
  {
package/dist/lazy.d.ts CHANGED
@@ -31,6 +31,7 @@ declare const loaders: {
31
31
  java: () => Promise<typeof import("./langs/java.js")>;
32
32
  javascript: () => Promise<typeof import("./langs/javascript.js")>;
33
33
  json: () => Promise<typeof import("./langs/json.js")>;
34
+ julia: () => Promise<typeof import("./langs/julia.js")>;
34
35
  kotlin: () => Promise<typeof import("./langs/kotlin.js")>;
35
36
  lua: () => Promise<typeof import("./langs/lua.js")>;
36
37
  makefile: () => Promise<typeof import("./langs/makefile.js")>;
@@ -1 +1 @@
1
- {"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCkD,CAAA;AAE/D,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,OAAO,CAAA;AAqGlD,eAAO,MAAM,gBAAgB,EAA2B,eAAe,EAAE,CAAA;AAEzE,sEAAsE;AACtE,eAAO,MAAM,mBAAmB,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SASpE,CAAA;AAID;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAAO,CAAC,eAAe,CA4BlE,CAAA;AAED,uCAAuC;AACvC,eAAO,MAAM,aAAa,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,OAAO,CAAC,eAAe,EAAE,CAE3E,CAAA"}
1
+ {"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0CkD,CAAA;AAE/D,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,OAAO,CAAA;AAsGlD,eAAO,MAAM,gBAAgB,EAA2B,eAAe,EAAE,CAAA;AAEzE,sEAAsE;AACtE,eAAO,MAAM,mBAAmB,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SASpE,CAAA;AAID;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAAO,CAAC,eAAe,CA4BlE,CAAA;AAED,uCAAuC;AACvC,eAAO,MAAM,aAAa,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,OAAO,CAAC,eAAe,EAAE,CAE3E,CAAA"}
package/dist/lazy.js CHANGED
@@ -31,6 +31,7 @@ const loaders = {
31
31
  java: () => import('./langs/java.js'),
32
32
  javascript: () => import('./langs/javascript.js'),
33
33
  json: () => import('./langs/json.js'),
34
+ julia: () => import('./langs/julia.js'),
34
35
  kotlin: () => import('./langs/kotlin.js'),
35
36
  lua: () => import('./langs/lua.js'),
36
37
  makefile: () => import('./langs/makefile.js'),
@@ -93,6 +94,7 @@ const aliases = {
93
94
  js: 'javascript',
94
95
  json5: 'json',
95
96
  jsonc: 'json',
97
+ jl: 'julia',
96
98
  jsx: 'javascript',
97
99
  kt: 'kotlin',
98
100
  kts: 'kotlin',
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "code",
17
17
  "performance"
18
18
  ],
19
- "version": "0.3.1",
19
+ "version": "0.4.0",
20
20
  "engines": {
21
21
  "node": ">=22"
22
22
  },
@@ -72,7 +72,7 @@
72
72
  "@types/hast": "^3.0.4"
73
73
  },
74
74
  "devDependencies": {
75
- "@scalar/code-highlight": "0.4.9",
75
+ "@scalar/code-highlight": "0.4.10",
76
76
  "highlight.js": "^11.11.1",
77
77
  "rolldown": "1.1.5",
78
78
  "vitest": "4.1.10"