@scalar/highlight 0.3.1 → 0.5.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 (78) hide show
  1. package/CHANGELOG.md +59 -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 +168 -9
  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 +147 -7
  17. package/dist/core/types.d.ts +76 -1
  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/cpp.d.ts.map +1 -1
  26. package/dist/langs/cpp.js +8 -0
  27. package/dist/langs/csharp.d.ts.map +1 -1
  28. package/dist/langs/csharp.js +11 -5
  29. package/dist/langs/css.d.ts.map +1 -1
  30. package/dist/langs/css.js +111 -9
  31. package/dist/langs/diff.d.ts +2 -1
  32. package/dist/langs/diff.d.ts.map +1 -1
  33. package/dist/langs/diff.js +19 -7
  34. package/dist/langs/go.d.ts.map +1 -1
  35. package/dist/langs/go.js +22 -1
  36. package/dist/langs/haskell.d.ts.map +1 -1
  37. package/dist/langs/haskell.js +33 -0
  38. package/dist/langs/html.d.ts +0 -10
  39. package/dist/langs/html.d.ts.map +1 -1
  40. package/dist/langs/html.js +63 -24
  41. package/dist/langs/http.d.ts.map +1 -1
  42. package/dist/langs/http.js +25 -9
  43. package/dist/langs/javascript.d.ts.map +1 -1
  44. package/dist/langs/javascript.js +279 -30
  45. package/dist/langs/json.d.ts.map +1 -1
  46. package/dist/langs/json.js +7 -1
  47. package/dist/langs/julia.d.ts +4 -0
  48. package/dist/langs/julia.d.ts.map +1 -0
  49. package/dist/langs/julia.js +181 -0
  50. package/dist/langs/kotlin.d.ts.map +1 -1
  51. package/dist/langs/kotlin.js +8 -1
  52. package/dist/langs/markdown.d.ts +9 -3
  53. package/dist/langs/markdown.d.ts.map +1 -1
  54. package/dist/langs/markdown.js +127 -6
  55. package/dist/langs/nginx.d.ts.map +1 -1
  56. package/dist/langs/nginx.js +26 -1
  57. package/dist/langs/ocaml.d.ts.map +1 -1
  58. package/dist/langs/ocaml.js +16 -7
  59. package/dist/langs/php.d.ts.map +1 -1
  60. package/dist/langs/php.js +69 -19
  61. package/dist/langs/python.d.ts.map +1 -1
  62. package/dist/langs/python.js +9 -2
  63. package/dist/langs/r.js +9 -9
  64. package/dist/langs/ruby.d.ts.map +1 -1
  65. package/dist/langs/ruby.js +8 -7
  66. package/dist/langs/rust.d.ts.map +1 -1
  67. package/dist/langs/rust.js +27 -11
  68. package/dist/langs/sql.d.ts.map +1 -1
  69. package/dist/langs/sql.js +9 -3
  70. package/dist/langs/swift.d.ts.map +1 -1
  71. package/dist/langs/swift.js +27 -1
  72. package/dist/langs/yaml.d.ts +0 -7
  73. package/dist/langs/yaml.d.ts.map +1 -1
  74. package/dist/langs/yaml.js +157 -28
  75. package/dist/lazy.d.ts +1 -0
  76. package/dist/lazy.d.ts.map +1 -1
  77. package/dist/lazy.js +2 -0
  78. package/package.json +2 -2
@@ -11,7 +11,9 @@ const json = {
11
11
  root: {
12
12
  rules: [
13
13
  { match: '//[^\\n]*', scope: 'comment' },
14
- { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
14
+ // A state, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and fails
15
+ // once per opener, which is O(n²) on a run of unterminated `/*`.
16
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
15
17
  // A string followed by `:` is a key, whatever nesting it sits in.
16
18
  {
17
19
  match: '("(?:[^"\\\\\\n]|\\\\.)*")(\\s*)(:)',
@@ -41,6 +43,10 @@ const json = {
41
43
  { match: '"', scope: 'string', pop: true },
42
44
  ],
43
45
  },
46
+ 'block-comment': {
47
+ default: 'comment',
48
+ rules: [{ match: '\\*/', scope: 'comment', pop: true }],
49
+ },
44
50
  },
45
51
  };
46
52
  export default json;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const julia: Grammar;
3
+ export default julia;
4
+ //# sourceMappingURL=julia.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"julia.d.ts","sourceRoot":"","sources":["../../src/langs/julia.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAyB5C,QAAA,MAAM,KAAK,EAAE,OA6KZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Julia.
3
+ *
4
+ * Things worth knowing about:
5
+ *
6
+ * - `'` is both a character literal and the transpose operator. It opens a
7
+ * literal only where a value cannot end — not after a name, a closing
8
+ * bracket, a number or another `'` — so `A'` and `x'y` stay operators.
9
+ * - `:` is a range, a ternary branch and the start of a symbol. A symbol needs
10
+ * a name straight after the colon and nothing value-like straight before it,
11
+ * which keeps `1:n` and `a ? b : c` out.
12
+ * - Interpolation is real: `"$(user.name) has $n items"` tokenizes the
13
+ * expressions. Prefixed string macros (`r"…"`, `raw"…"`, `b"…"`) do not
14
+ * interpolate, so they get a state without it.
15
+ * - Block comments `#= … =#` nest.
16
+ * - Names may end in `!` (`push!`) and carry Unicode (`α`, `∇f`).
17
+ */
18
+ const ID = '[A-Za-z_\\u00A1-\\uFFFF][\\w!\\u00A1-\\uFFFF]{0,128}';
19
+ /** Something after which `'` is a transpose rather than a character literal. */
20
+ const VALUE_END = "[\\w)\\]}'.\\u00A1-\\uFFFF]";
21
+ const julia = {
22
+ name: 'julia',
23
+ aliases: ['jl'],
24
+ states: {
25
+ root: { rules: [{ include: 'expression' }] },
26
+ expression: {
27
+ rules: [
28
+ { match: '#=', scope: 'comment', push: 'block-comment' },
29
+ { match: '#[^\\n]*', scope: 'comment' },
30
+ // ---- strings -------------------------------------------------------
31
+ { match: '"""', scope: 'string', push: 'string-triple' },
32
+ {
33
+ match: `(${ID})(""")`,
34
+ scope: ['string.special', 'string'],
35
+ push: 'string-raw-triple',
36
+ },
37
+ { match: `(${ID})(")`, scope: ['string.special', 'string'], push: 'string-raw' },
38
+ { match: '"', scope: 'string', push: 'string' },
39
+ { match: '`', scope: 'string', push: 'command' },
40
+ {
41
+ // The lookbehind sits after the quote so the rule still starts with a
42
+ // literal character, which keeps the engine's first-character scan.
43
+ match: `'(?<!${VALUE_END}')(?:[^'\\\\\\n]|\\\\(?:x[0-9a-fA-F]{1,2}|u[0-9a-fA-F]{1,4}|U[0-9a-fA-F]{1,8}|[0-7]{1,3}|[^\\n]))'`,
44
+ scope: 'string',
45
+ },
46
+ // ---- numbers -------------------------------------------------------
47
+ { match: '\\b0x[0-9a-fA-F_]+(?:p[-+]?\\d+)?\\b', scope: 'number' },
48
+ { match: '\\b0b[01_]+\\b', scope: 'number' },
49
+ { match: '\\b0o[0-7_]+\\b', scope: 'number' },
50
+ {
51
+ match: '(?:\\b\\d[\\d_]*(?:\\.(?!\\.)[\\d_]*)?|\\.\\d[\\d_]*)(?:[eEf][-+]?\\d+)?(?:im\\b)?',
52
+ scope: 'number',
53
+ },
54
+ // ---- macros, symbols and types -------------------------------------
55
+ { match: `@${ID}(?:\\.${ID})*`, scope: 'decorator' },
56
+ {
57
+ match: `(:(?<![\\w)\\]}:'".\\u00A1-\\uFFFF]:))(${ID})`,
58
+ scope: ['constant', 'constant'],
59
+ },
60
+ {
61
+ match: `(::|<:|>:)([ \\t]*)(${ID}(?:\\.${ID})*)`,
62
+ scope: ['operator', null, 'type'],
63
+ },
64
+ // ---- definitions ---------------------------------------------------
65
+ {
66
+ match: `\\b(function|macro)([ \\t]+)(${ID}(?:\\.${ID})*)`,
67
+ scope: ['keyword.declaration', null, 'function'],
68
+ },
69
+ {
70
+ match: `\\b((?:mutable[ \\t]+)?struct|abstract[ \\t]+type|primitive[ \\t]+type)([ \\t]+)(${ID})`,
71
+ scope: ['keyword.declaration', null, 'class'],
72
+ },
73
+ {
74
+ match: `\\b((?:bare)?module)([ \\t]+)(${ID})`,
75
+ scope: ['keyword.declaration', null, 'namespace'],
76
+ },
77
+ // The short form `area(r) = π * r^2` defines a function. The argument
78
+ // list cannot hold parentheses here, which covers the one-liners this
79
+ // form is used for and keeps the scan bounded.
80
+ {
81
+ match: `^([ \\t]*)(${ID})(?=\\([^()\\n]{0,256}\\)[ \\t]*(?:where[^=\\n]{0,256})?=(?!=))`,
82
+ scope: [null, 'function'],
83
+ },
84
+ // ---- keywords ------------------------------------------------------
85
+ {
86
+ match: '\\b(?:if|elseif|else|end|for|while|break|continue|return|try|catch|finally|do|begin|let|quote)\\b',
87
+ scope: 'keyword.control',
88
+ },
89
+ {
90
+ match: '\\b(?:function|macro|struct|mutable|abstract|primitive|type|module|baremodule|const|global|local|where|outer)\\b',
91
+ scope: 'keyword.declaration',
92
+ },
93
+ { match: '\\b(?:using|import|export|public|as)\\b', scope: 'keyword.import' },
94
+ { match: '\\b(?:in|isa)\\b', scope: 'keyword.operator' },
95
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
96
+ { match: '\\b(?:nothing|missing|NaN|Inf|NaN32|Inf32|undef|stdout|stderr|stdin)\\b', scope: 'constant.builtin' },
97
+ // ---- calls ---------------------------------------------------------
98
+ {
99
+ match: '\\b(?:println|print|show|display|string|length|size|push!|pop!|append!|map|filter|reduce|sum|prod|maximum|minimum|collect|typeof|isa|convert|parse|zeros|ones|rand|round|floor|ceil|abs|sqrt|exp|log|error|throw|isempty|haskey|get|keys|values|pairs|enumerate|zip|sort|sort!|occursin|replace|split|join|startswith|endswith|open|read|write|close|eachline|readline|readlines|sprint|repr|Dict|Vector|Array|Tuple)(?=\\.?\\()',
100
+ scope: 'function.builtin',
101
+ },
102
+ { match: `(\\.)(${ID})(?=\\.?\\()`, scope: ['punctuation', 'function.method'] },
103
+ { match: `(\\.)(${ID})`, scope: ['punctuation', 'variable.member'] },
104
+ // `f(x)` and the broadcast `f.(x)`.
105
+ { match: `${ID}(?=\\.?\\()`, scope: 'function.call' },
106
+ // CapWords reads as a type, which is how Julia names them.
107
+ { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
108
+ // ---- syntax --------------------------------------------------------
109
+ {
110
+ match: "\\.?(?:===|!==|==|!=|<=|>=|=>|->|\\|\\||&&|\\|>|<\\||\\.\\.\\.|[-+*/\\\\^%÷<>=!&|~⊻∈∉≤≥≠]=?)|'",
111
+ scope: 'operator',
112
+ },
113
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
114
+ { match: '[,;]', scope: 'punctuation.delimiter' },
115
+ { match: '[.:?]', scope: 'punctuation' },
116
+ ],
117
+ },
118
+ 'block-comment': {
119
+ default: 'comment',
120
+ rules: [
121
+ { match: '#=', scope: 'comment', push: 'block-comment' },
122
+ { match: '=#', scope: 'comment', pop: true },
123
+ ],
124
+ },
125
+ // ---- strings -------------------------------------------------------------
126
+ string: {
127
+ default: 'string',
128
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '"', scope: 'string', pop: true }],
129
+ },
130
+ 'string-triple': {
131
+ default: 'string',
132
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '"""', scope: 'string', pop: true }],
133
+ },
134
+ /** Prefixed string macros: no interpolation, and `\"` is the only escape. */
135
+ 'string-raw': {
136
+ default: 'string',
137
+ rules: [
138
+ { match: '\\\\"', scope: 'string.escape' },
139
+ { match: '"', scope: 'string', pop: true },
140
+ ],
141
+ },
142
+ 'string-raw-triple': {
143
+ default: 'string',
144
+ rules: [{ match: '"""', scope: 'string', pop: true }],
145
+ },
146
+ command: {
147
+ default: 'string',
148
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '`', scope: 'string', pop: true }],
149
+ },
150
+ escapes: {
151
+ rules: [
152
+ {
153
+ match: '\\\\(?:x[0-9a-fA-F]{1,2}|u[0-9a-fA-F]{1,4}|U[0-9a-fA-F]{1,8}|[0-7]{1,3}|[\\s\\S])',
154
+ scope: 'string.escape',
155
+ },
156
+ ],
157
+ },
158
+ interpolation: {
159
+ rules: [
160
+ { match: '\\$\\(', scope: 'interpolation', push: 'interpolation-body' },
161
+ { match: `(\\$)(${ID})`, scope: ['interpolation', 'variable'] },
162
+ ],
163
+ },
164
+ /** The expression inside `$( … )`. Nested parentheses are tracked. */
165
+ 'interpolation-body': {
166
+ rules: [
167
+ { match: '\\)', scope: 'interpolation', pop: true },
168
+ { match: '\\(', scope: 'punctuation.bracket', push: 'parens' },
169
+ { include: 'expression' },
170
+ ],
171
+ },
172
+ parens: {
173
+ rules: [
174
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
175
+ { match: '\\(', scope: 'punctuation.bracket', push: 'parens' },
176
+ { include: 'expression' },
177
+ ],
178
+ },
179
+ },
180
+ };
181
+ export default julia;
@@ -1 +1 @@
1
- {"version":3,"file":"kotlin.d.ts","sourceRoot":"","sources":["../../src/langs/kotlin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiC5C,QAAA,MAAM,MAAM,EAAE,OAiNb,CAAA;AAED,eAAe,MAAM,CAAA"}
1
+ {"version":3,"file":"kotlin.d.ts","sourceRoot":"","sources":["../../src/langs/kotlin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiC5C,QAAA,MAAM,MAAM,EAAE,OAwNb,CAAA;AAED,eAAe,MAAM,CAAA"}
@@ -87,9 +87,16 @@ const kotlin = {
87
87
  scope: 'keyword.control',
88
88
  },
89
89
  {
90
- match: '\\b(?:fun|val|var|class|interface|object|typealias|const(?:ructor)?|init|companion|enum|data|sealed|annotation|inner|open|override|abstract|final|private|protected|public|internal|lateinit|(?:no|cross)?inline|reified|vararg|suspend|operator|infix|actual|expect|by|where|get|set|out)\\b',
90
+ match: '\\b(?:fun|val|var|class|interface|object|typealias|const(?:ructor)?|init|companion|enum|data|sealed|annotation|inner|open|override|abstract|final|private|protected|public|internal|lateinit|(?:no|cross)?inline|reified|vararg|suspend|operator|infix|actual|expect|by|where|get|set|out|tailrec|external)\\b',
91
91
  scope: 'keyword.declaration',
92
92
  },
93
+ // `value` is a modifier only in front of `class`; anywhere else it is
94
+ // one of the most common names there is.
95
+ { match: '\\bvalue(?=\\s+class\\b)', scope: 'keyword.declaration' },
96
+ {
97
+ match: '\\b(package|import)(\\s+)([A-Za-z_]\\w*(?:\\.[A-Za-z_]\\w*)*(?:\\.\\*)?)',
98
+ scope: ['keyword.import', null, 'namespace'],
99
+ },
93
100
  { match: '\\b(?:package|import)\\b', scope: 'keyword.import' },
94
101
  // `as?` cannot use a trailing `\b`: the boundary after `?` needs a word
95
102
  // character next, and a cast is followed by a space.
@@ -2,9 +2,15 @@ import type { Grammar } from '../core/types.js';
2
2
  /**
3
3
  * Markdown.
4
4
  *
5
- * Prose is left alone; only the marks are styled. Fenced code blocks become a
6
- * literal state so their contents are never mistaken for Markdown syntax —
7
- * highlighting the fence body with its own language is a follow-up.
5
+ * Prose is left alone; only the marks are styled. Three kinds of block belong
6
+ * to other languages and are handed to them when they are registered:
7
+ *
8
+ * - a fenced code block that names its language
9
+ * - YAML front matter
10
+ * - HTML that starts a line, and `<script>`, `<pre>` and `<style>` blocks
11
+ *
12
+ * Any other fence becomes a literal state, so its contents are never mistaken
13
+ * for Markdown syntax.
8
14
  */
9
15
  declare const markdown: Grammar;
10
16
  export default markdown;
@@ -1 +1 @@
1
- {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/langs/markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;GAMG;AACH,QAAA,MAAM,QAAQ,EAAE,OAqEf,CAAA;AAED,eAAe,QAAQ,CAAA"}
1
+ {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/langs/markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAS,MAAM,eAAe,CAAA;AA2CnD;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,QAAQ,EAAE,OAqJf,CAAA;AAED,eAAe,QAAQ,CAAA"}
@@ -1,9 +1,52 @@
1
+ /** HTML blocks that end at their closing tag rather than at a blank line. */
2
+ const RAW_TAGS = 'script|pre|style|textarea';
3
+ /**
4
+ * `RAW_TAGS` matched in any case, for the root state, which is case-sensitive
5
+ * everywhere else. HTML tag names are not: `<SCRIPT>` opens the same block.
6
+ */
7
+ const RAW_TAGS_ANY_CASE = RAW_TAGS.replace(/[a-z]/g, (c) => `[${c}${c.toUpperCase()}]`);
8
+ /**
9
+ * Right after the `>` that ends a tag, and not one inside a quoted attribute
10
+ * value: `<div title="a>b">` ends at the last `>`. Matched backwards from the
11
+ * `>`, which is checked first, so every other position fails on one character.
12
+ * The repeats are capped so a `>` on a long line costs a bounded scan.
13
+ */
14
+ const AFTER_TAG = `(?<=<\\/?[A-Za-z][\\w-]{0,64}(?:[^<>"'\\n]|"[^"\\n]{0,512}"|'[^'\\n]{0,512}'){0,512}>)`;
15
+ /**
16
+ * The states between a language fence and its closing fence: `<name>-code`
17
+ * scopes the closing fence and `<name>-body` hands the body to the language.
18
+ * `close` is a run of the opening character, which CommonMark lets be longer
19
+ * than the opener.
20
+ */
21
+ const codeFence = (name, close) => {
22
+ const closing = `^[ \\t]*${close}[ \\t]*$`;
23
+ return {
24
+ [`${name}-code`]: {
25
+ default: 'string',
26
+ rules: [
27
+ { match: closing, scope: 'punctuation', pop: true },
28
+ { match: '^', push: `${name}-body` },
29
+ ],
30
+ },
31
+ [`${name}-body`]: {
32
+ default: 'string',
33
+ embed: { language: '{{remembered}}', until: closing },
34
+ rules: [],
35
+ },
36
+ };
37
+ };
1
38
  /**
2
39
  * Markdown.
3
40
  *
4
- * Prose is left alone; only the marks are styled. Fenced code blocks become a
5
- * literal state so their contents are never mistaken for Markdown syntax —
6
- * highlighting the fence body with its own language is a follow-up.
41
+ * Prose is left alone; only the marks are styled. Three kinds of block belong
42
+ * to other languages and are handed to them when they are registered:
43
+ *
44
+ * - a fenced code block that names its language
45
+ * - YAML front matter
46
+ * - HTML that starts a line, and `<script>`, `<pre>` and `<style>` blocks
47
+ *
48
+ * Any other fence becomes a literal state, so its contents are never mistaken
49
+ * for Markdown syntax.
7
50
  */
8
51
  const markdown = {
9
52
  name: 'markdown',
@@ -11,13 +54,53 @@ const markdown = {
11
54
  states: {
12
55
  root: {
13
56
  rules: [
57
+ // A three-character fence that names a language hands its body to that
58
+ // language. Longer fences keep the literal form below, so a ```` block
59
+ // that shows ``` inside still closes only on its own run.
60
+ // The two fence characters get a state each, because only a run of the
61
+ // character that opened the block closes it.
62
+ {
63
+ match: '^([ \\t]*)(```(?!`))([ \\t]*)([A-Za-z][\\w+#.-]*)([^\\n]*)$',
64
+ scope: [null, 'punctuation', null, 'type', 'type'],
65
+ push: 'backtick-code',
66
+ remember: 4,
67
+ },
68
+ {
69
+ match: '^([ \\t]*)(~~~(?!~))([ \\t]*)([A-Za-z][\\w+#.-]*)([^\\n]*)$',
70
+ scope: [null, 'punctuation', null, 'type', 'type'],
71
+ push: 'tilde-code',
72
+ remember: 4,
73
+ },
74
+ // The fence remembers its run, so only a run of the same character at
75
+ // least as long closes it: a ```` block can show ``` inside.
14
76
  {
15
77
  match: '^([ \\t]*)(```+|~~~+)([^\\n]*)$',
16
78
  scope: [null, 'punctuation', 'type'],
17
79
  push: 'fence',
80
+ remember: 2,
81
+ },
82
+ { match: '<!--', scope: 'comment', push: 'html-comment' },
83
+ // Front matter: a `---` block at the very start, and only when it is
84
+ // closed. An unclosed `---` on the first line is a thematic break, and
85
+ // reading it as YAML would take the whole document with it. The
86
+ // lookahead to the closing line runs once, at position 0.
87
+ {
88
+ match: '(?<![\\s\\S])---(?=[ \\t]*\\r?\\n[\\s\\S]*?^(?:---|\\.\\.\\.)[ \\t]*$)',
89
+ scope: 'punctuation',
90
+ push: 'front-matter',
18
91
  },
92
+ // HTML at the start of a line goes to the HTML grammar. `<script>`,
93
+ // `<pre>`, `<style>` and `<textarea>` run to the line that closes them,
94
+ // as in CommonMark. Any other tag gets its line at most: CommonMark would
95
+ // run the block to the next blank line, but MDX and most docs
96
+ // renderers read Markdown inside components, and holding the lines
97
+ // after `<Steps>` as HTML would strip their fences and emphasis.
98
+ // Inline tags inside a paragraph keep the rule below.
99
+ { match: `^(?=[ \\t]*<(?:${RAW_TAGS_ANY_CASE})(?:[\\s>]|$))`, push: 'html-raw' },
100
+ { match: '^(?=[ \\t]*<\\/?[A-Za-z][\\w-]*(?:[\\s/>]|$))', push: 'html-line' },
19
101
  { match: '^#{1,6}[ \\t][^\\n]*', scope: 'heading' },
20
- { match: '^[^\\n]+\\n(?:={2,}|-{2,})[ \\t]*$', scope: 'heading' },
102
+ // `[^\\r\\n]`, so the `\\r` of a blank CRLF line does not count as text.
103
+ { match: '^[^\\r\\n]+\\r?\\n(?:={2,}|-{2,})[ \\t]*$', scope: 'heading' },
21
104
  {
22
105
  match: '^[ \\t]*(?:\\*[ \\t]*){3,}$|^[ \\t]*(?:-[ \\t]*){3,}$|^[ \\t]*(?:_[ \\t]*){3,}$',
23
106
  scope: 'punctuation',
@@ -53,19 +136,57 @@ const markdown = {
53
136
  { match: '~~[^~\\n]+~~', scope: 'comment' },
54
137
  { match: '^([ \\t]*)(\\|)', scope: [null, 'punctuation'] },
55
138
  { match: '\\|', scope: 'punctuation' },
56
- { match: '</?[A-Za-z][\\w:-]*(?:\\s[^>\\n]*)?/?>', scope: 'tag' },
139
+ // The attribute run stops at `<` too, so a line of unclosed `<a ` costs
140
+ // O(n) per tag rather than a scan to the end of the line each.
141
+ { match: '</?[A-Za-z][\\w:-]*(?:[ \\t][^<>\\n]*)?/?>', scope: 'tag' },
57
142
  ],
58
143
  },
59
144
  fence: {
60
145
  default: 'string',
61
146
  rules: [
62
147
  {
63
- match: '^[ \\t]*(?:```+|~~~+)[ \\t]*$',
148
+ match: '^[ \\t]*{{remembered}}[`~]*[ \\t]*$',
64
149
  scope: 'punctuation',
65
150
  pop: true,
66
151
  },
67
152
  ],
68
153
  },
154
+ 'front-matter': {
155
+ rules: [
156
+ { match: '^(?:---|\\.\\.\\.)[ \\t]*$', scope: 'punctuation', pop: true },
157
+ { match: '^', push: 'front-matter-body' },
158
+ ],
159
+ },
160
+ 'front-matter-body': {
161
+ embed: { language: 'yaml', until: '^(?:---|\\.\\.\\.)[ \\t]*$' },
162
+ rules: [],
163
+ },
164
+ // Without the HTML grammar registered, HTML renders unscoped, the way a
165
+ // fence in an unregistered language falls back to a plain string.
166
+ 'html-raw': {
167
+ // The closing tag may be in any case, as the opening one may.
168
+ ignoreCase: true,
169
+ embed: {
170
+ language: 'html',
171
+ // The line break that ends the line holding the closing tag. The
172
+ // lookahead goes first so the lookbehind only runs at line ends.
173
+ until: `(?=\\n|(?![\\s\\S]))(?<=<\\/(?:${RAW_TAGS})[ \\t]*>[^\\n]*)`,
174
+ },
175
+ rules: [],
176
+ },
177
+ // To the end of the line, or to the first tag-ending `>` followed by text
178
+ // rather than by another tag: in `<span>x</span> **y**` only the opening
179
+ // tag is HTML, and the rest of the line stays Markdown for the inline rules.
180
+ 'html-line': {
181
+ embed: { language: 'html', until: `$|${AFTER_TAG}(?![ \\t]*(?:<|$))` },
182
+ rules: [],
183
+ },
184
+ ...codeFence('backtick', '`{3,}'),
185
+ ...codeFence('tilde', '~{3,}'),
186
+ 'html-comment': {
187
+ default: 'comment',
188
+ rules: [{ match: '-->', scope: 'comment', pop: true }],
189
+ },
69
190
  },
70
191
  };
71
192
  export default markdown;
@@ -1 +1 @@
1
- {"version":3,"file":"nginx.d.ts","sourceRoot":"","sources":["../../src/langs/nginx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6E5C,QAAA,MAAM,KAAK,EAAE,OA4GZ,CAAA;AAED,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"nginx.d.ts","sourceRoot":"","sources":["../../src/langs/nginx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6E5C,QAAA,MAAM,KAAK,EAAE,OAuIZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -82,7 +82,14 @@ const nginx = {
82
82
  scope: ['property'],
83
83
  push: 'regex',
84
84
  },
85
- { match: `${STATEMENT}(${NAME})${OPENS_BLOCK}`, scope: ['keyword'] },
85
+ // A map defines its second argument; the first is an expression that
86
+ // keeps ordinary variable and string highlighting.
87
+ { match: `${STATEMENT}(map)\\b${OPENS_BLOCK}`, scope: ['keyword'], push: 'map-arguments' },
88
+ // An upstream defines its first argument, used by `proxy_pass http://name`.
89
+ {
90
+ match: `${STATEMENT}(?:(upstream)([ \\t]+)([$\\w.-]+)|(${NAME}))${OPENS_BLOCK}`,
91
+ scope: ['keyword', null, 'type', 'keyword'],
92
+ },
86
93
  { match: `${STATEMENT}(${NAME})`, scope: ['property'] },
87
94
  // Quotes get a state because nginx allows a value to run across lines
88
95
  // and expands `$variables` inside single quotes as well as double.
@@ -97,6 +104,16 @@ const nginx = {
97
104
  // `-F` of a header name like `X-Forwarded-For` out of this.
98
105
  { match: '\\B-!?[fdex]\\b', scope: 'keyword.operator' },
99
106
  { match: '\\b(?:on|off)\\b', scope: 'boolean' },
107
+ // The few argument values nginx defines itself — log levels, event
108
+ // methods, the default log format — and the flags that end a rewrite.
109
+ // Bounded on both sides by anything that can continue a path, so the
110
+ // `error` of `logs/error.log` stays part of the path. One rule for
111
+ // both, since every alternative costs on files this regular, and the
112
+ // check on what comes before runs last, once a word has matched.
113
+ {
114
+ match: '\\b(?:(debug|info|notice|warn|error|crit|alert|emerg|main|combined|epoll|kqueue|eventport|devpoll)(?![\\w/.-])|(last|break|redirect|permanent)(?=[ \\t]*;))(?<![/.$-][a-z]*)',
115
+ scope: ['constant.builtin', 'keyword'],
116
+ },
100
117
  // Paths and URLs are most of what a config actually says, so they are
101
118
  // worth a colour. The scheme is capped rather than open-ended: an
102
119
  // unbounded scan for `://` would be retried at every dotted segment of
@@ -120,6 +137,14 @@ const nginx = {
120
137
  block: {
121
138
  rules: [{ match: '\\}', scope: 'punctuation.bracket', pop: true }, { include: 'root' }],
122
139
  },
140
+ /** Scope the map destination without consuming or recoloring its source. */
141
+ 'map-arguments': {
142
+ rules: [
143
+ { match: '\\$\\w+(?=[ \\t]*\\{)', scope: 'type', pop: true },
144
+ { match: '(?=[;{}\\n])', pop: true },
145
+ { include: 'root' },
146
+ ],
147
+ },
123
148
  variables: {
124
149
  rules: [
125
150
  { match: '\\$\\{[A-Za-z_]\\w*\\}', scope: 'variable' },
@@ -1 +1 @@
1
- {"version":3,"file":"ocaml.d.ts","sourceRoot":"","sources":["../../src/langs/ocaml.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA2D5C,QAAA,MAAM,KAAK,EAAE,OAsRZ,CAAA;AAED,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"ocaml.d.ts","sourceRoot":"","sources":["../../src/langs/ocaml.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA2D5C,QAAA,MAAM,KAAK,EAAE,OA+RZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -155,13 +155,14 @@ const ocaml = {
155
155
  ],
156
156
  },
157
157
  // Comments nest, so the opener pushes another copy of this state and only
158
- // the matching `*)` pops. A `"*)"` inside a string inside a comment ends the
159
- // comment early — OCaml lexes strings in comments, we do not.
158
+ // the matching `*)` pops. OCaml lexes string literals inside comments, so a
159
+ // `"*)"` there does not close the comment; the string stays comment-coloured.
160
160
  comment: {
161
161
  default: 'comment',
162
162
  rules: [
163
163
  { match: '\\(\\*', push: 'comment' },
164
164
  { match: '\\*\\)', pop: true },
165
+ { match: '"', push: 'comment-string' },
165
166
  ],
166
167
  },
167
168
  'comment-doc': {
@@ -169,15 +170,23 @@ const ocaml = {
169
170
  rules: [
170
171
  { match: '\\(\\*', push: 'comment-doc' },
171
172
  { match: '\\*\\)', pop: true },
173
+ { match: '"', push: 'comment-string' },
174
+ ],
175
+ },
176
+ /** A string literal inside a comment. Still a comment to the reader. */
177
+ 'comment-string': {
178
+ default: 'comment',
179
+ rules: [
180
+ { match: '\\\\[\\s\\S]', scope: 'comment' },
181
+ { match: '"', scope: 'comment', pop: true },
172
182
  ],
173
183
  },
174
184
  // ---- strings, chars and numbers ------------------------------------------
175
185
  strings: {
176
186
  rules: [
177
- // `{id|raw|id}`. Matching the identifier on both ends needs a
178
- // backreference, so any closing delimiter is accepted: a body
179
- // containing `|other}` ends the literal early.
180
- { match: '\\{[a-z_]*\\|', scope: 'string', push: 'quoted-string' },
187
+ // `{id|raw|id}`. The identifier is remembered, so only `|id}` closes
188
+ // the literal and a body can hold `|other}`.
189
+ { match: '\\{([a-z_]*)\\|', scope: 'string', push: 'quoted-string', remember: 1 },
181
190
  { match: '"', scope: 'string', push: 'string' },
182
191
  // A char literal closes; a type variable does not. The lookbehind is
183
192
  // what keeps the trailing quote of `acc'` from opening one.
@@ -195,7 +204,7 @@ const ocaml = {
195
204
  // Deliberately no escape rule: a quoted string literal is raw.
196
205
  'quoted-string': {
197
206
  default: 'string',
198
- rules: [{ match: '\\|[a-z_]*\\}', scope: 'string', pop: true }],
207
+ rules: [{ match: '\\|{{remembered}}\\}', scope: 'string', pop: true }],
199
208
  },
200
209
  numbers: {
201
210
  rules: [
@@ -1 +1 @@
1
- {"version":3,"file":"php.d.ts","sourceRoot":"","sources":["../../src/langs/php.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA+B5C,QAAA,MAAM,GAAG,EAAE,OAyUV,CAAA;AAED,eAAe,GAAG,CAAA"}
1
+ {"version":3,"file":"php.d.ts","sourceRoot":"","sources":["../../src/langs/php.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAwC5C,QAAA,MAAM,GAAG,EAAE,OAqXV,CAAA;AAED,eAAe,GAAG,CAAA"}