-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
349 lines (323 loc) · 10.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @import {Root} from 'hast'
* @import {IGrammar, IRawGrammar} from 'vscode-textmate'
* @import {Grammar, Options} from './types.js'
*/
import vscodeOniguruma from 'vscode-oniguruma'
import vscodeTextmate from 'vscode-textmate'
import {parse} from './parse.js'
import {theme} from './theme.js'
import {getOniguruma} from '#get-oniguruma'
/**
* Create a `StarryNight` that can highlight things with the given
* `grammars`.
* This is async to allow async loading and registering, which is currently
* only used for WASM.
*
* @param {ReadonlyArray<Grammar>} grammars
* Grammars to support.
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Promise that resolves to an instance which highlights with the bound
* grammars.
*/
export async function createStarryNight(grammars, options) {
/** @type {Map<string, Readonly<Grammar>>} */
const registered = new Map()
/** @type {Map<string, string>} */
const names = new Map()
/** @type {Map<string, string>} */
const extensions = new Map()
/** @type {Map<string, string>} */
const extensionsWithDot = new Map()
let currentRegistry = await createRegistry(grammars, options)
return {flagToScope, highlight, missingScopes, register, scopes}
/**
* Get the grammar scope (such as `text.md`) associated with a grammar name
* (such as `markdown`) or grammar extension (such as `.mdwn`).
*
* This function uses the first word (when splitting on spaces and tabs) that
* is used after the opening of a fenced code block:
*
* ````markdown
* ```js
* console.log(1)
* ```
* ````
*
* To match GitHub, this also accepts entire paths:
*
* ````markdown
* ```path/to/example.js
* console.log(1)
* ```
* ````
*
* > **Note**: languages can use the same extensions.
* > For example, `.h` is reused by many languages.
* > In those cases, you will get one scope back, but it might not be the
* > most popular language associated with an extension.
*
* @param {string} flag
* Grammar name (such as `'markdown'`), grammar extension (such as
* `'.mdwn'`), or path ending in extension.
* @returns {string | undefined}
* Grammar scope (such as `'text.md'`).
* @example
* ```js
* import {common, createStarryNight} from '@wooorm/starry-night'
*
* const starryNight = await createStarryNight(common)
*
* console.log(starryNight.flagToScope('pandoc')) // `'text.md'`
* console.log(starryNight.flagToScope('workbook')) // `'text.md'`
* console.log(starryNight.flagToScope('.workbook')) // `'text.md'`
* console.log(starryNight.flagToScope('path/to/example.js')) // `'source.js'`
* console.log(starryNight.flagToScope('whatever')) // `undefined`
* ```
*/
function flagToScope(flag) {
if (typeof flag !== 'string') {
throw new TypeError('Expected `string` for `flag`, got `' + flag + '`')
}
const normal = flag
.toLowerCase()
.replace(/^[ \t]+/, '')
.replace(/\/*[ \t]*$/g, '')
const scopeByName = names.get(normal)
if (scopeByName) {
return scopeByName
}
const dot = normal.lastIndexOf('.')
if (dot === -1) {
return extensions.get('.' + normal)
}
const extension = normal.slice(dot)
return extensions.get(extension) || extensionsWithDot.get(extension)
}
/**
* Highlight programming code.
*
* @param {string} value
* Code to highlight.
* @param {string} scope
* Registered grammar scope to highlight as (such as `'text.md'`).
* @returns {Root}
* Node representing highlighted code.
* @example
* ```js
* import {createStarryNight} from '@wooorm/starry-night'
* import sourceCss from '@wooorm/starry-night/source.css'
*
* const starryNight = await createStarryNight([sourceCss])
*
* console.log(starryNight.highlight('em { color: red }', 'source.css'))
* ```
*
* Yields:
*
* ```js
* {
* type: 'root',
* children: [
* {type: 'element', tagName: 'span', properties: [Object], children: [Array]},
* {type: 'text', value: ' { '},
* {type: 'element', tagName: 'span', properties: [Object], children: [Array]},
* {type: 'text', value: ': '},
* {type: 'element', tagName: 'span', properties: [Object], children: [Array]},
* {type: 'text', value: ' }'}
* ]
* }
* ```
*/
function highlight(value, scope) {
if (typeof value !== 'string') {
throw new TypeError('Expected `string` for `value`, got `' + value + '`')
}
if (typeof scope !== 'string') {
throw new TypeError('Expected `string` for `scope`, got `' + scope + '`')
}
// Use the private API so we don’t need to cache again.
/** @type {unknown} */
// @ts-expect-error: untyped internals of `vscode-textmate`.
// type-coverage:ignore-next-line
const map = currentRegistry._syncRegistry._grammars
/** @type {IGrammar} */
// @ts-expect-error: untyped internals of `vscode-textmate`.
const grammar = map.get(scope)
if (!grammar) {
throw new Error('Expected grammar `' + scope + '` to be registered')
}
return parse(value, grammar, currentRegistry.getColorMap())
}
/**
* List scopes that are needed by the registered grammars but that are
* missing.
*
* To illustrate, the `text.xml.svg` grammar needs the `text.xml` grammar.
* When you register `text.xml.svg` without `text.xml`, it will be listed
* here.
*
* @returns {ReadonlyArray<string>}
* List of grammar scopes, such as `'text.md'`.
* @example
* ```js
* import {createStarryNight} from '@wooorm/starry-night'
* import textXml from '@wooorm/starry-night/text.xml'
* import textXmlSvg from '@wooorm/starry-night/text.xml.svg'
*
* const svg = await createStarryNight([textXmlSvg])
* console.log(svg.missingScopes()) //=> ['text.xml']
*
* const svgAndXml = await createStarryNight([textXmlSvg, textXml])
* console.log(svgAndXml.missingScopes()) //=> []
* ```
*/
function missingScopes() {
/** @type {Set<string>} */
const available = new Set()
/** @type {Set<string>} */
const needed = new Set()
for (const [scopeName, grammar] of registered) {
available.add(scopeName)
if (grammar.dependencies) {
for (const dep of grammar.dependencies) {
needed.add(dep)
}
}
}
return [...needed]
.filter(function (d) {
return !available.has(d)
})
.sort()
}
/**
* Add more grammars.
*
* @param {ReadonlyArray<Readonly<Grammar>>} grammars
* Grammars to support.
* @returns {Promise<undefined>}
* Promise resolving to nothing.
* @example
* ````js
* import {createStarryNight} from '@wooorm/starry-night'
* import sourceCss from '@wooorm/starry-night/source.css'
* import textMd from '@wooorm/starry-night/text.md'
* import {toHtml} from 'hast-util-to-html'
*
* const markdown = '```css\nem { color: red }\n```'
*
* const starryNight = await createStarryNight([textMd])
*
* console.log(toHtml(starryNight.highlight(markdown, 'text.md')))
*
* await starryNight.register([sourceCss])
*
* console.log(toHtml(starryNight.highlight(markdown, 'text.md')))
* ````
*
* Yields:
*
* ````html
* <span class="pl-s">```</span><span class="pl-en">css</span>
* <span class="pl-c1">em { color: red }</span>
* <span class="pl-s">```</span>
* ````
*
* ````html
* <span class="pl-s">```</span><span class="pl-en">css</span>
* <span class="pl-ent">em</span> { <span class="pl-c1">color</span>: <span class="pl-c1">red</span> }
* <span class="pl-s">```</span>
* ````
*/
async function register(grammars) {
currentRegistry = await createRegistry(grammars)
}
/**
* List all registered scopes.
*
* @returns {ReadonlyArray<string>}
* List of grammar scopes, such as `'text.md'`.
* @example
* ```js
* import {common, createStarryNight} from '@wooorm/starry-night'
*
* const starryNight = await createStarryNight(common)
*
* console.log(starryNight.scopes())
* ```
*
* Yields:
*
* ```js
* [
* 'source.c',
* 'source.c++',
* // …
* 'text.xml',
* 'text.xml.svg'
* ]
* ```
*/
function scopes() {
return [...registered.keys()].sort()
}
/**
* @param {ReadonlyArray<Readonly<Grammar>>} grammars
* Grammars.
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Registry.
*/
async function createRegistry(grammars, options) {
for (const grammar of grammars) {
const scope = grammar.scopeName
for (const d of grammar.extensions) extensions.set(d, scope)
if (grammar.extensionsWithDot)
for (const d of grammar.extensionsWithDot)
extensionsWithDot.set(d, scope)
for (const d of grammar.names) names.set(d, scope)
registered.set(scope, grammar)
}
const registry = new vscodeTextmate.Registry({
async loadGrammar(scopeName) {
// Cast because `vscode-textmate` has much stricter types that needed by
// textmate, or by what they actually support.
// Given that we can’t fix the grammars provided by the world here, and
// given that `vscode-textmate` is crying without a reason, we tell it to
// shut up instead.
const grammar = /** @type {IRawGrammar | undefined} */ (
registered.get(scopeName)
)
return grammar
},
onigLib: createOniguruma(options)
})
registry.setTheme(theme)
await Promise.all(
[...registered.keys()].map(function (d) {
return registry.loadGrammar(d)
})
)
return registry
}
}
/**
* Small function needed for oniguruma to work.
*
* Idea: as this seems to be a singleton, would it help if we call it once and
* keep the promise?
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* `vscode-oniguruma`.
*/
async function createOniguruma(options) {
const wasmBinary = await getOniguruma(options || undefined)
await vscodeOniguruma.loadWASM(wasmBinary)
return vscodeOniguruma
}