Skip to content

Commit b4f3780

Browse files
committed
Update docs
1 parent 68f87bc commit b4f3780

File tree

4 files changed

+312
-187
lines changed

4 files changed

+312
-187
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
/**
2+
* Old export names, remove next major.
23
* @typedef {import('./lib/core.js').RefractorRoot} RefractorRoot
34
* @typedef {import('./lib/core.js').RefractorElement} RefractorElement
45
* @typedef {import('./lib/core.js').Text} Text
6+
*
7+
* Old and new names:
58
* @typedef {import('./lib/core.js').Grammar} Grammar
69
* @typedef {import('./lib/core.js').Syntax} Syntax
10+
*
11+
* New names:
12+
* @typedef {import('./lib/core.js').RefractorRoot} Root
713
*/
814
export {refractor} from './lib/common.js'

lib/core.js

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,55 @@ refractor.util.encode = encode
9898
// @ts-expect-error Overwrite Prism.
9999
refractor.Token.stringify = stringify
100100

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+
101144
/**
102145
* Register a syntax.
103-
* Needed if you’re using `refractor/core.js`.
104146
*
105147
* @param {Syntax} syntax
148+
* Language function made for refractor, as in, the files in
149+
* `refractor/lang/*.js`.
106150
* @returns {void}
107151
*/
108152
function register(syntax) {
@@ -120,23 +164,23 @@ function register(syntax) {
120164
}
121165

122166
/**
123-
* Register a new `alias` for the `name` language.
167+
* Register aliases for already registered languages.
124168
*
125-
* @param {Record<string, string|Array<string>>|string} name
169+
* @param {Record<string, string|Array<string>>|string} language
126170
* @param {string|Array<string>} [alias]
127171
* @returns {void}
128172
*/
129-
function alias(name, alias) {
173+
function alias(language, alias) {
130174
const languages = refractor.languages
131175
/** @type {Record<string, string|Array<string>>} */
132176
let map = {}
133177

134-
if (typeof name === 'string') {
178+
if (typeof language === 'string') {
135179
if (alias) {
136-
map[name] = alias
180+
map[language] = alias
137181
}
138182
} else {
139-
map = name
183+
map = language
140184
}
141185

142186
/** @type {string} */
@@ -156,60 +200,19 @@ function alias(name, alias) {
156200
}
157201

158202
/**
159-
* Parse `value` according to the `language` (name or alias)
160-
* syntax.
203+
* Check whether an `alias` or `language` is registered.
161204
*
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
203206
* @returns {boolean}
204207
*/
205-
function registered(language) {
206-
if (typeof language !== 'string') {
208+
function registered(aliasOrLanguage) {
209+
if (typeof aliasOrLanguage !== 'string') {
207210
throw new TypeError(
208-
'Expected `string` for `language`, got `' + language + '`'
211+
'Expected `string` for `aliasOrLanguage`, got `' + aliasOrLanguage + '`'
209212
)
210213
}
211214

212-
return own.call(refractor.languages, language)
215+
return own.call(refractor.languages, aliasOrLanguage)
213216
}
214217

215218
/**

0 commit comments

Comments
 (0)