-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
core.js
285 lines (248 loc) · 6.83 KB
/
core.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
/**
* @typedef _Token A hidden Prism token
* @property {string} type
* @property {string} content
* @property {string} alias
* @property {number} length
*
* @typedef _Env A hidden Prism environment
* @property {string} type
* @property {string} tag
* @property {Text|RefractorElement|Array<Text|RefractorElement>} content
* @property {Array<string>} classes
* @property {Record<string, string>} attributes
* @property {string} language
*
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Text} Text
* @typedef {Omit<Element, 'children'> & {children: Array<RefractorElement|Text>}} RefractorElement
* @typedef {Omit<Root, 'children'> & {children: Array<RefractorElement|Text>}} RefractorRoot
*
* @typedef {import('prismjs').Languages} Languages
* @typedef {import('prismjs').Grammar} Grammar Whatever this is, Prism handles it.
* @typedef {((prism: unknown) => void) & {displayName: string, aliases?: string[]}} Syntax A refractor syntax function
*
* @typedef Refractor Virtual syntax highlighting
* @property {highlight} highlight
* @property {alias} alias
* @property {register} register
* @property {registered} registered
* @property {listLanguages} listLanguages
* @property {Languages} languages
*/
// Load all stuff in `prism.js` itself, except for `prism-file-highlight.js`.
// The wrapped non-leaky grammars are loaded instead of Prism’s originals.
import {h} from 'hastscript'
import {parseEntities} from 'parse-entities'
import {Prism} from './prism-core.js'
const own = {}.hasOwnProperty
// Inherit.
function Refractor() {}
Refractor.prototype = Prism
/** @type {Refractor} */
// @ts-expect-error: TS is wrong.
export const refractor = new Refractor()
// Create.
refractor.highlight = highlight
refractor.register = register
refractor.alias = alias
refractor.registered = registered
refractor.listLanguages = listLanguages
// @ts-expect-error Overwrite Prism.
refractor.util.encode = encode
// @ts-expect-error Overwrite Prism.
refractor.Token.stringify = stringify
/**
* Highlight `value` (code) as `language` (programming language).
*
* @param {string} value
* Code to highlight.
* @param {string|Grammar} language
* Programming language name, alias, or grammar.
* @returns {RefractorRoot}
* Node representing highlighted code.
*/
function highlight(value, language) {
if (typeof value !== 'string') {
throw new TypeError('Expected `string` for `value`, got `' + value + '`')
}
/** @type {Grammar} */
let grammar
/** @type {string|undefined} */
let name
// `name` is a grammar object.
// This was called internally by Prism.js before 1.28.0.
/* c8 ignore next 2 */
if (language && typeof language === 'object') {
grammar = language
} else {
name = language
if (typeof name !== 'string') {
throw new TypeError('Expected `string` for `name`, got `' + name + '`')
}
if (own.call(refractor.languages, name)) {
grammar = refractor.languages[name]
} else {
throw new Error('Unknown language: `' + name + '` is not registered')
}
}
return {
type: 'root',
// @ts-expect-error: we hacked Prism to accept and return the things we want.
children: Prism.highlight.call(refractor, value, grammar, name)
}
}
/**
* Register a syntax.
*
* @param {Syntax} syntax
* Language function made for refractor, as in, the files in
* `refractor/lang/*.js`.
* @returns {void}
*/
function register(syntax) {
if (typeof syntax !== 'function' || !syntax.displayName) {
throw new Error('Expected `function` for `syntax`, got `' + syntax + '`')
}
// Do not duplicate registrations.
if (!own.call(refractor.languages, syntax.displayName)) {
syntax(refractor)
}
}
/**
* Register aliases for already registered languages.
*
* @param {Record<string, string|Array<string>>|string} language
* @param {string|Array<string>} [alias]
* @returns {void}
*/
function alias(language, alias) {
const languages = refractor.languages
/** @type {Record<string, string|Array<string>>} */
let map = {}
if (typeof language === 'string') {
if (alias) {
map[language] = alias
}
} else {
map = language
}
/** @type {string} */
let key
for (key in map) {
if (own.call(map, key)) {
const value = map[key]
const list = typeof value === 'string' ? [value] : value
let index = -1
while (++index < list.length) {
languages[list[index]] = languages[key]
}
}
}
}
/**
* Check whether an `alias` or `language` is registered.
*
* @param {string} aliasOrLanguage
* @returns {boolean}
*/
function registered(aliasOrLanguage) {
if (typeof aliasOrLanguage !== 'string') {
throw new TypeError(
'Expected `string` for `aliasOrLanguage`, got `' + aliasOrLanguage + '`'
)
}
return own.call(refractor.languages, aliasOrLanguage)
}
/**
* List all registered languages (names and aliases).
*
* @returns {Array<string>}
*/
function listLanguages() {
const languages = refractor.languages
/** @type {Array<string>} */
const list = []
/** @type {string} */
let language
for (language in languages) {
if (
own.call(languages, language) &&
typeof languages[language] === 'object'
) {
list.push(language)
}
}
return list
}
/**
* @param {string|_Token|Array<string|_Token>} value
* @param {string} language
* @returns {Text|RefractorElement|Array<Text|RefractorElement>}
*/
function stringify(value, language) {
if (typeof value === 'string') {
return {type: 'text', value}
}
if (Array.isArray(value)) {
/** @type {Array<Text|RefractorElement>} */
const result = []
let index = -1
while (++index < value.length) {
if (
value[index] !== '' &&
value[index] !== null &&
value[index] !== undefined
) {
// @ts-expect-error Assume no sub-arrays.
result.push(stringify(value[index], language))
}
}
return result
}
/** @type {_Env} */
const env = {
type: value.type,
content: stringify(value.content, language),
tag: 'span',
classes: ['token', value.type],
attributes: {},
language
}
if (value.alias) {
env.classes.push(
...(typeof value.alias === 'string' ? [value.alias] : value.alias)
)
}
// @ts-expect-error Prism.
refractor.hooks.run('wrap', env)
// @ts-expect-error Hush, it’s fine.
return h(
env.tag + '.' + env.classes.join('.'),
attributes(env.attributes),
env.content
)
}
/**
* @template {unknown} T
* @param {T} tokens
* @returns {T}
*/
function encode(tokens) {
return tokens
}
/**
* @param {Record<string, string>} attrs
* @returns {Record<string, string>}
*/
function attributes(attrs) {
/** @type {string} */
let key
for (key in attrs) {
if (own.call(attrs, key)) {
attrs[key] = parseEntities(attrs[key])
}
}
return attrs
}