aboutsummaryrefslogtreecommitdiff
path: root/src/lib/types/highlight.js/index.d.ts
blob: 98dfac25523e4facd641ed86d76aa9a251ca889b (plain)
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
/*
 Yoinked from https://github.com/highlightjs/highlight.js/blob/main/types/index.d.ts
*/

/* eslint-disable no-unused-vars */
/* eslint-disable no-use-before-define */
// For TS consumers who use Node and don't have dom in their tsconfig lib, import the necessary types here.
/// <reference lib="dom" />

declare module 'highlight.js/private' {
	import { CompiledMode, Mode, Language } from 'highlight.js'

	type MatchType = 'begin' | 'end' | 'illegal'
	type EnhancedMatch = RegExpMatchArray & {
		rule: CompiledMode
		type: MatchType
	}
	type AnnotatedError = Error & {
		mode?: Mode | Language
		languageName?: string
		badRule?: Mode
	}

	type KeywordData = [string, number]
	type KeywordDict = Record<string, KeywordData>
}
declare module 'highlight.js' {
	import { KeywordDict } from 'highlight.js/private'

	export type HLJSApi = PublicApi & ModesAPI

	export interface VuePlugin {
		install: (vue: any) => void
	}

	// perhaps make this an interface?
	type RegexEitherOptions = {
		capture?: boolean
	}

	interface PublicApi {
		highlight: (
			codeOrLanguageName: string,
			optionsOrCode: string | HighlightOptions,
			ignoreIllegals?: boolean
		) => HighlightResult
		highlightAuto: (
			code: string,
			languageSubset?: string[]
		) => AutoHighlightResult
		highlightBlock: (element: HTMLElement) => void
		highlightElement: (element: HTMLElement) => void
		configure: (options: Partial<HLJSOptions>) => void
		initHighlighting: () => void
		initHighlightingOnLoad: () => void
		highlightAll: () => void
		registerLanguage: (languageName: string, language: LanguageFn) => void
		unregisterLanguage: (languageName: string) => void
		listLanguages: () => string[]
		registerAliases: (
			aliasList: string | string[],
			{ languageName }: { languageName: string }
		) => void
		getLanguage: (languageName: string) => Language | undefined
		autoDetection: (languageName: string) => boolean
		inherit: <T>(original: T, ...args: Record<string, any>[]) => T
		addPlugin: (plugin: HLJSPlugin) => void
		removePlugin: (plugin: HLJSPlugin) => void
		debugMode: () => void
		safeMode: () => void
		versionString: string
		vuePlugin: () => VuePlugin
		regex: {
			concat: (...args: (RegExp | string)[]) => string
			lookahead: (re: RegExp | string) => string
			either: (
				...args:
					| (RegExp | string)[]
					| [...(RegExp | string)[], RegexEitherOptions]
			) => string
			optional: (re: RegExp | string) => string
			anyNumberOfTimes: (re: RegExp | string) => string
		}
		newInstance: () => HLJSApi
	}

	interface ModesAPI {
		SHEBANG: (mode?: Partial<Mode> & { binary?: string | RegExp }) => Mode
		BACKSLASH_ESCAPE: Mode
		QUOTE_STRING_MODE: Mode
		APOS_STRING_MODE: Mode
		PHRASAL_WORDS_MODE: Mode
		COMMENT: (
			begin: string | RegExp,
			end: string | RegExp,
			modeOpts?: Mode | {}
		) => Mode
		C_LINE_COMMENT_MODE: Mode
		C_BLOCK_COMMENT_MODE: Mode
		HASH_COMMENT_MODE: Mode
		NUMBER_MODE: Mode
		C_NUMBER_MODE: Mode
		BINARY_NUMBER_MODE: Mode
		REGEXP_MODE: Mode
		TITLE_MODE: Mode
		UNDERSCORE_TITLE_MODE: Mode
		METHOD_GUARD: Mode
		END_SAME_AS_BEGIN: (mode: Mode) => Mode
		// built in regex
		IDENT_RE: string
		UNDERSCORE_IDENT_RE: string
		MATCH_NOTHING_RE: string
		NUMBER_RE: string
		C_NUMBER_RE: string
		BINARY_NUMBER_RE: string
		RE_STARTERS_RE: string
	}

	export type LanguageFn = (hljs: HLJSApi) => Language
	export type CompilerExt = (mode: Mode, parent: Mode | Language | null) => void

	export interface HighlightResult {
		code?: string
		relevance: number
		value: string
		language?: string
		illegal: boolean
		errorRaised?: Error
		// * for auto-highlight
		secondBest?: Omit<HighlightResult, 'second_best'>
		// private
		_illegalBy?: illegalData
		_emitter: Emitter
		_top?: Language | CompiledMode
	}
	export interface AutoHighlightResult extends HighlightResult {}

	export interface illegalData {
		message: string
		context: string
		index: number
		resultSoFar: string
		mode: CompiledMode
	}

	export type BeforeHighlightContext = {
		code: string
		language: string
		result?: HighlightResult
	}
	export type PluginEvent = keyof HLJSPlugin
	export type HLJSPlugin = {
		'after:highlight'?: (result: HighlightResult) => void
		'before:highlight'?: (context: BeforeHighlightContext) => void
		'after:highlightElement'?: (data: {
			el: Element
			result: HighlightResult
			text: string
		}) => void
		'before:highlightElement'?: (data: {
			el: Element
			language: string
		}) => void
		// TODO: Old API, remove with v12
		'after:highlightBlock'?: (data: {
			block: Element
			result: HighlightResult
			text: string
		}) => void
		'before:highlightBlock'?: (data: {
			block: Element
			language: string
		}) => void
	}

	interface EmitterConstructor {
		new (opts: any): Emitter
	}

	export interface HighlightOptions {
		language: string
		ignoreIllegals?: boolean
	}

	export interface HLJSOptions {
		noHighlightRe: RegExp
		languageDetectRe: RegExp
		classPrefix: string
		cssSelector: string
		languages?: string[]
		__emitter: EmitterConstructor
		ignoreUnescapedHTML?: boolean
		throwUnescapedHTML?: boolean
	}

	export interface CallbackResponse {
		data: Record<string, any>
		ignoreMatch: () => void
		isMatchIgnored: boolean
	}

	export type ModeCallback = (
		match: RegExpMatchArray,
		response: CallbackResponse
	) => void
	export type Language = LanguageDetail & Partial<Mode>
	export interface Mode extends ModeCallbacks, ModeDetails {}

	export interface LanguageDetail {
		name?: string
		unicodeRegex?: boolean
		rawDefinition?: () => Language
		aliases?: string[]
		disableAutodetect?: boolean
		contains: Mode[]
		case_insensitive?: boolean
		keywords?: string | string[] | Record<string, string | string[]>
		isCompiled?: boolean
		exports?: any
		classNameAliases?: Record<string, string>
		compilerExtensions?: CompilerExt[]
		supersetOf?: string
	}

	// technically private, but exported for convenience as this has
	// been a pretty stable API and is quite useful
	export interface Emitter {
		startScope(name: string): void
		endScope(): void
		addText(text: string): void
		toHTML(): string
		finalize(): void
		__addSublanguage(emitter: Emitter, subLanguageName: string): void
	}

	export type HighlightedHTMLElement = HTMLElement & {
		result?: object
		secondBest?: object
		parentNode: HTMLElement
	}

	/* modes */

	interface ModeCallbacks {
		'on:end'?: Function
		'on:begin'?: ModeCallback
	}

	export interface CompiledLanguage extends LanguageDetail, CompiledMode {
		isCompiled: true
		contains: CompiledMode[]
		keywords: Record<string, any>
	}

	export type CompiledScope = Record<number, string> & {
		_emit?: Record<number, boolean>
		_multi?: boolean
		_wrap?: string
	}

	export type CompiledMode = Omit<Mode, 'contains'> & {
		begin?: RegExp | string
		end?: RegExp | string
		scope?: string
		contains: CompiledMode[]
		keywords: KeywordDict
		data: Record<string, any>
		terminatorEnd: string
		keywordPatternRe: RegExp
		beginRe: RegExp
		endRe: RegExp
		illegalRe: RegExp
		matcher: any
		isCompiled: true
		starts?: CompiledMode
		parent?: CompiledMode
		beginScope?: CompiledScope
		endScope?: CompiledScope
	}

	interface ModeDetails {
		begin?: RegExp | string | (RegExp | string)[]
		match?: RegExp | string | (RegExp | string)[]
		end?: RegExp | string | (RegExp | string)[]
		// deprecated in favor of `scope`
		className?: string
		scope?: string | Record<number, string>
		beginScope?: string | Record<number, string>
		endScope?: string | Record<number, string>
		contains?: ('self' | Mode)[]
		endsParent?: boolean
		endsWithParent?: boolean
		endSameAsBegin?: boolean
		skip?: boolean
		excludeBegin?: boolean
		excludeEnd?: boolean
		returnBegin?: boolean
		returnEnd?: boolean
		__beforeBegin?: Function
		parent?: Mode
		starts?: Mode
		lexemes?: string | RegExp
		keywords?: string | string[] | Record<string, string | string[]>
		beginKeywords?: string
		relevance?: number
		illegal?: string | RegExp | Array<string | RegExp>
		variants?: Mode[]
		cachedVariants?: Mode[]
		// parsed
		subLanguage?: string | string[]
		isCompiled?: boolean
		label?: string
	}

	const hljs: HLJSApi
	export default hljs
}

declare module 'highlight.js/lib/languages/*' {
	import { LanguageFn } from 'highlight.js'
	const defineLanguage: LanguageFn
	export default defineLanguage
}