UNPKG

8.87 kBTypeScriptView Raw
1/* eslint-disable no-unused-vars */
2/* eslint-disable no-use-before-define */
3// For TS consumers who use Node and don't have dom in their tsconfig lib, import the necessary types here.
4/// <reference lib="dom" />
5
6declare module 'highlight.js/private' {
7 import { CompiledMode, Mode, Language } from "highlight.js";
8
9 type MatchType = "begin" | "end" | "illegal"
10 type EnhancedMatch = RegExpMatchArray & {rule: CompiledMode, type: MatchType}
11 type AnnotatedError = Error & {mode?: Mode | Language, languageName?: string, badRule?: Mode}
12
13 type KeywordData = [string, number];
14 type KeywordDict = Record<string, KeywordData>
15}
16declare module 'highlight.js' {
17
18 import { KeywordDict } from "highlight.js/private";
19
20 export type HLJSApi = PublicApi & ModesAPI
21
22 export interface VuePlugin {
23 install: (vue: any) => void
24 }
25
26 interface PublicApi {
27 highlight: (codeOrLanguageName: string, optionsOrCode: string | HighlightOptions, ignoreIllegals?: boolean) => HighlightResult
28 highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult
29 highlightBlock: (element: HTMLElement) => void
30 highlightElement: (element: HTMLElement) => void
31 configure: (options: Partial<HLJSOptions>) => void
32 initHighlighting: () => void
33 initHighlightingOnLoad: () => void
34 highlightAll: () => void
35 registerLanguage: (languageName: string, language: LanguageFn) => void
36 unregisterLanguage: (languageName: string) => void
37 listLanguages: () => string[]
38 registerAliases: (aliasList: string | string[], { languageName } : {languageName: string}) => void
39 getLanguage: (languageName: string) => Language | undefined
40 autoDetection: (languageName: string) => boolean
41 inherit: <T>(original: T, ...args: Record<string, any>[]) => T
42 addPlugin: (plugin: HLJSPlugin) => void
43 debugMode: () => void
44 safeMode: () => void
45 versionString: string
46 vuePlugin: () => VuePlugin
47 }
48
49 interface ModesAPI {
50 SHEBANG: (mode?: Partial<Mode> & {binary?: string | RegExp}) => Mode
51 BACKSLASH_ESCAPE: Mode
52 QUOTE_STRING_MODE: Mode
53 APOS_STRING_MODE: Mode
54 PHRASAL_WORDS_MODE: Mode
55 COMMENT: (begin: string | RegExp, end: string | RegExp, modeOpts?: Mode | {}) => Mode
56 C_LINE_COMMENT_MODE: Mode
57 C_BLOCK_COMMENT_MODE: Mode
58 HASH_COMMENT_MODE: Mode
59 NUMBER_MODE: Mode
60 C_NUMBER_MODE: Mode
61 BINARY_NUMBER_MODE: Mode
62 REGEXP_MODE: Mode
63 TITLE_MODE: Mode
64 UNDERSCORE_TITLE_MODE: Mode
65 METHOD_GUARD: Mode
66 END_SAME_AS_BEGIN: (mode: Mode) => Mode
67 // built in regex
68 IDENT_RE: string
69 UNDERSCORE_IDENT_RE: string
70 MATCH_NOTHING_RE: string
71 NUMBER_RE: string
72 C_NUMBER_RE: string
73 BINARY_NUMBER_RE: string
74 RE_STARTERS_RE: string
75 }
76
77 export type LanguageFn = (hljs: HLJSApi) => Language
78 export type CompilerExt = (mode: Mode, parent: Mode | Language | null) => void
79
80 export interface HighlightResult {
81 code?: string
82 relevance : number
83 value : string
84 language? : string
85 illegal : boolean
86 errorRaised? : Error
87 // * for auto-highlight
88 secondBest? : Omit<HighlightResult, 'second_best'>
89 // private
90 _illegalBy? : illegalData
91 _emitter : Emitter
92 _top? : Language | CompiledMode
93 }
94 export interface AutoHighlightResult extends HighlightResult {}
95
96 export interface illegalData {
97 message: string
98 context: string
99 index: number
100 resultSoFar : string
101 mode: CompiledMode
102 }
103
104 export type BeforeHighlightContext = {
105 code: string,
106 language: string,
107 result?: HighlightResult
108 }
109 export type PluginEvent = keyof HLJSPlugin;
110 export type HLJSPlugin = {
111 'after:highlight'?: (result: HighlightResult) => void,
112 'before:highlight'?: (context: BeforeHighlightContext) => void,
113 'after:highlightElement'?: (data: { el: Element, result: HighlightResult, text: string}) => void,
114 'before:highlightElement'?: (data: { el: Element, language: string}) => void,
115 // TODO: Old API, remove with v12
116 'after:highlightBlock'?: (data: { block: Element, result: HighlightResult, text: string}) => void,
117 'before:highlightBlock'?: (data: { block: Element, language: string}) => void,
118 }
119
120 interface EmitterConstructor {
121 new (opts: any): Emitter
122 }
123
124 export interface HighlightOptions {
125 language: string
126 ignoreIllegals?: boolean
127 }
128
129 export interface HLJSOptions {
130 noHighlightRe: RegExp
131 languageDetectRe: RegExp
132 classPrefix: string
133 cssSelector: string
134 languages?: string[]
135 __emitter: EmitterConstructor
136 ignoreUnescapedHTML?: boolean
137 throwUnescapedHTML?: boolean
138 }
139
140 export interface CallbackResponse {
141 data: Record<string, any>
142 ignoreMatch: () => void
143 isMatchIgnored: boolean
144 }
145
146 export type ModeCallback = (match: RegExpMatchArray, response: CallbackResponse) => void
147 export type Language = LanguageDetail & Partial<Mode>
148 export interface Mode extends ModeCallbacks, ModeDetails {}
149
150 export interface LanguageDetail {
151 name?: string
152 unicodeRegex?: boolean
153 rawDefinition?: () => Language
154 aliases?: string[]
155 disableAutodetect?: boolean
156 contains: (Mode)[]
157 case_insensitive?: boolean
158 keywords?: Record<string, any> | string
159 isCompiled?: boolean,
160 exports?: any,
161 classNameAliases?: Record<string, string>
162 compilerExtensions?: CompilerExt[]
163 supersetOf?: string
164 }
165
166 // technically private, but exported for convenience as this has
167 // been a pretty stable API and is quite useful
168 export interface Emitter {
169 addKeyword(text: string, kind: string): void
170 addText(text: string): void
171 toHTML(): string
172 finalize(): void
173 closeAllNodes(): void
174 openNode(kind: string): void
175 closeNode(): void
176 addSublanguage(emitter: Emitter, subLanguageName: string): void
177 }
178
179 export type HighlightedHTMLElement = HTMLElement & {result?: object, secondBest?: object, parentNode: HTMLElement}
180
181 /* modes */
182
183 interface ModeCallbacks {
184 "on:end"?: Function,
185 "on:begin"?: ModeCallback
186 }
187
188 export interface CompiledLanguage extends LanguageDetail, CompiledMode {
189 isCompiled: true
190 contains: CompiledMode[]
191 keywords: Record<string, any>
192 }
193
194 export type CompiledScope = Record<number, string> & {_emit?: Record<number, boolean>, _multi?: boolean, _wrap?: string};
195
196 export type CompiledMode = Omit<Mode, 'contains'> &
197 {
198 begin?: RegExp | string
199 end?: RegExp | string
200 scope?: string
201 contains: CompiledMode[]
202 keywords: KeywordDict
203 data: Record<string, any>
204 terminatorEnd: string
205 keywordPatternRe: RegExp
206 beginRe: RegExp
207 endRe: RegExp
208 illegalRe: RegExp
209 matcher: any
210 isCompiled: true
211 starts?: CompiledMode
212 parent?: CompiledMode
213 beginScope?: CompiledScope
214 endScope?: CompiledScope
215 }
216
217 interface ModeDetails {
218 begin?: RegExp | string | (RegExp | string)[]
219 match?: RegExp | string | (RegExp | string)[]
220 end?: RegExp | string | (RegExp | string)[]
221 // deprecated in favor of `scope`
222 className?: string
223 scope?: string | Record<number, string>
224 beginScope?: string | Record<number, string>
225 endScope?: string | Record<number, string>
226 contains?: ("self" | Mode)[]
227 endsParent?: boolean
228 endsWithParent?: boolean
229 endSameAsBegin?: boolean
230 skip?: boolean
231 excludeBegin?: boolean
232 excludeEnd?: boolean
233 returnBegin?: boolean
234 returnEnd?: boolean
235 __beforeBegin?: Function
236 parent?: Mode
237 starts?:Mode
238 lexemes?: string | RegExp
239 keywords?: Record<string, any> | string
240 beginKeywords?: string
241 relevance?: number
242 illegal?: string | RegExp | Array<string | RegExp>
243 variants?: Mode[]
244 cachedVariants?: Mode[]
245 // parsed
246 subLanguage?: string | string[]
247 isCompiled?: boolean
248 label?: string
249 }
250
251 const hljs : HLJSApi;
252 export default hljs;
253
254}
255
256declare module 'highlight.js/lib/languages/*' {
257 import { LanguageFn } from "highlight.js";
258 const defineLanguage: LanguageFn;
259 export default defineLanguage;
260}
261
262