@@ -98,11 +98,55 @@ refractor.util.encode = encode
98
98
// @ts -expect-error Overwrite Prism.
99
99
refractor . Token . stringify = stringify
100
100
101
+ /**
102
+ * Highlight `value` (code) as `language` (programming language).
103
+ *
104
+ * @param {string } value
105
+ * Code to highlight.
106
+ * @param {string|Grammar } language
107
+ * Programming language name, alias, or grammar.
108
+ * @returns {RefractorRoot }
109
+ * Node representing highlighted code.
110
+ */
111
+ function highlight ( value , language ) {
112
+ if ( typeof value !== 'string' ) {
113
+ throw new TypeError ( 'Expected `string` for `value`, got `' + value + '`' )
114
+ }
115
+
116
+ /** @type {Grammar } */
117
+ let grammar
118
+ /** @type {string|undefined } */
119
+ let name
120
+
121
+ // `name` is a grammar object.
122
+ if ( language && typeof language === 'object' ) {
123
+ grammar = language
124
+ } else {
125
+ name = language
126
+
127
+ if ( typeof name !== 'string' ) {
128
+ throw new TypeError ( 'Expected `string` for `name`, got `' + name + '`' )
129
+ }
130
+
131
+ if ( own . call ( refractor . languages , name ) ) {
132
+ grammar = refractor . languages [ name ]
133
+ } else {
134
+ throw new Error ( 'Unknown language: `' + name + '` is not registered' )
135
+ }
136
+ }
137
+
138
+ return {
139
+ type : 'root' ,
140
+ children : Prism . highlight . call ( refractor , value , grammar , name )
141
+ }
142
+ }
143
+
101
144
/**
102
145
* Register a syntax.
103
- * Needed if you’re using `refractor/core.js`.
104
146
*
105
147
* @param {Syntax } syntax
148
+ * Language function made for refractor, as in, the files in
149
+ * `refractor/lang/*.js`.
106
150
* @returns {void }
107
151
*/
108
152
function register ( syntax ) {
@@ -120,23 +164,23 @@ function register(syntax) {
120
164
}
121
165
122
166
/**
123
- * Register a new `alias` for the `name` language .
167
+ * Register aliases for already registered languages .
124
168
*
125
- * @param {Record<string, string|Array<string>>|string } name
169
+ * @param {Record<string, string|Array<string>>|string } language
126
170
* @param {string|Array<string> } [alias]
127
171
* @returns {void }
128
172
*/
129
- function alias ( name , alias ) {
173
+ function alias ( language , alias ) {
130
174
const languages = refractor . languages
131
175
/** @type {Record<string, string|Array<string>> } */
132
176
let map = { }
133
177
134
- if ( typeof name === 'string' ) {
178
+ if ( typeof language === 'string' ) {
135
179
if ( alias ) {
136
- map [ name ] = alias
180
+ map [ language ] = alias
137
181
}
138
182
} else {
139
- map = name
183
+ map = language
140
184
}
141
185
142
186
/** @type {string } */
@@ -156,60 +200,19 @@ function alias(name, alias) {
156
200
}
157
201
158
202
/**
159
- * Parse `value` according to the `language` (name or alias)
160
- * syntax.
203
+ * Check whether an `alias` or `language` is registered.
161
204
*
162
- * @param {string } value
163
- * @param {string|Grammar } nameOrGrammar
164
- * @returns {RefractorRoot }
165
- */
166
- function highlight ( value , nameOrGrammar ) {
167
- if ( typeof value !== 'string' ) {
168
- throw new TypeError ( 'Expected `string` for `value`, got `' + value + '`' )
169
- }
170
-
171
- /** @type {Grammar } */
172
- let grammar
173
- /** @type {string|undefined } */
174
- let name
175
-
176
- // `name` is a grammar object.
177
- if ( nameOrGrammar && typeof nameOrGrammar === 'object' ) {
178
- grammar = nameOrGrammar
179
- } else {
180
- name = nameOrGrammar
181
-
182
- if ( typeof name !== 'string' ) {
183
- throw new TypeError ( 'Expected `string` for `name`, got `' + name + '`' )
184
- }
185
-
186
- if ( own . call ( refractor . languages , name ) ) {
187
- grammar = refractor . languages [ name ]
188
- } else {
189
- throw new Error ( 'Unknown language: `' + name + '` is not registered' )
190
- }
191
- }
192
-
193
- return {
194
- type : 'root' ,
195
- children : Prism . highlight . call ( refractor , value , grammar , name )
196
- }
197
- }
198
-
199
- /**
200
- * Check if a `language` (name or alias) is registered.
201
- *
202
- * @param {string } language
205
+ * @param {string } aliasOrLanguage
203
206
* @returns {boolean }
204
207
*/
205
- function registered ( language ) {
206
- if ( typeof language !== 'string' ) {
208
+ function registered ( aliasOrLanguage ) {
209
+ if ( typeof aliasOrLanguage !== 'string' ) {
207
210
throw new TypeError (
208
- 'Expected `string` for `language `, got `' + language + '`'
211
+ 'Expected `string` for `aliasOrLanguage `, got `' + aliasOrLanguage + '`'
209
212
)
210
213
}
211
214
212
- return own . call ( refractor . languages , language )
215
+ return own . call ( refractor . languages , aliasOrLanguage )
213
216
}
214
217
215
218
/**
0 commit comments