index.d.ts (6187B)
1 // For TS consumers who use Node and don't have dom in their tsconfig lib, import the necessary types here. 2 /// <reference lib="dom" /> 3 4 /* Public API */ 5 6 // eslint-disable-next-line 7 declare const hljs : HLJSApi; 8 9 type HLJSApi = PublicApi & ModesAPI 10 11 interface VuePlugin { 12 install: (vue: any) => void 13 } 14 15 interface PublicApi { 16 highlight: (languageName: string, code: string, ignoreIllegals?: boolean, continuation?: Mode) => HighlightResult 17 highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult 18 fixMarkup: (html: string) => string 19 highlightBlock: (element: HTMLElement) => void 20 configure: (options: Partial<HLJSOptions>) => void 21 initHighlighting: () => void 22 initHighlightingOnLoad: () => void 23 registerLanguage: (languageName: string, language: LanguageFn) => void 24 listLanguages: () => string[] 25 registerAliases: (aliasList: string | string[], { languageName } : {languageName: string}) => void 26 getLanguage: (languageName: string) => Language | undefined 27 requireLanguage: (languageName: string) => Language | never 28 autoDetection: (languageName: string) => boolean 29 inherit: <T>(original: T, ...args: Record<string, any>[]) => T 30 addPlugin: (plugin: HLJSPlugin) => void 31 debugMode: () => void 32 safeMode: () => void 33 versionString: string 34 vuePlugin: () => VuePlugin 35 } 36 37 interface ModesAPI { 38 SHEBANG: (mode?: Partial<Mode> & {binary?: string | RegExp}) => Mode 39 BACKSLASH_ESCAPE: Mode 40 QUOTE_STRING_MODE: Mode 41 APOS_STRING_MODE: Mode 42 PHRASAL_WORDS_MODE: Mode 43 COMMENT: (begin: string | RegExp, end: string | RegExp, modeOpts?: Mode | {}) => Mode 44 C_LINE_COMMENT_MODE: Mode 45 C_BLOCK_COMMENT_MODE: Mode 46 HASH_COMMENT_MODE: Mode 47 NUMBER_MODE: Mode 48 C_NUMBER_MODE: Mode 49 BINARY_NUMBER_MODE: Mode 50 CSS_NUMBER_MODE: Mode 51 REGEXP_MODE: Mode 52 TITLE_MODE: Mode 53 UNDERSCORE_TITLE_MODE: Mode 54 METHOD_GUARD: Mode 55 END_SAME_AS_BEGIN: (mode: Mode) => Mode 56 // built in regex 57 IDENT_RE: string 58 UNDERSCORE_IDENT_RE: string 59 NUMBER_RE: string 60 C_NUMBER_RE: string 61 BINARY_NUMBER_RE: string 62 RE_STARTERS_RE: string 63 } 64 65 type LanguageFn = (hljs?: HLJSApi) => Language 66 67 interface HighlightResult { 68 relevance : number 69 value : string 70 language? : string 71 emitter : Emitter 72 illegal : boolean 73 top? : Language | CompiledMode 74 illegalBy? : illegalData 75 sofar? : string 76 errorRaised? : Error 77 // * for auto-highlight 78 second_best? : Omit<HighlightResult, 'second_best'> 79 } 80 interface AutoHighlightResult extends HighlightResult {} 81 82 interface illegalData { 83 msg: string 84 context: string 85 mode: CompiledMode 86 } 87 88 type PluginEvent = 89 'before:highlight' 90 | 'after:highlight' 91 | 'before:highlightBlock' 92 | 'after:highlightBlock' 93 94 type HLJSPlugin = { 95 [K in PluginEvent]? : any 96 } 97 98 interface EmitterConstructor { 99 new (opts: any): Emitter 100 } 101 102 interface HLJSOptions { 103 noHighlightRe: RegExp 104 languageDetectRe: RegExp 105 classPrefix: string 106 tabReplace?: string 107 useBR: boolean 108 languages?: string[] 109 __emitter: EmitterConstructor 110 } 111 112 interface CallbackResponse { 113 data: Record<string, any> 114 ignoreMatch: () => void 115 } 116 117 /************ 118 PRIVATE API 119 ************/ 120 121 /* for jsdoc annotations in the JS source files */ 122 123 type AnnotatedError = Error & {mode?: Mode | Language, languageName?: string, badRule?: Mode} 124 125 type ModeCallback = (match: RegExpMatchArray, response: CallbackResponse) => void 126 type HighlightedHTMLElement = HTMLElement & {result?: object, second_best?: object, parentNode: HTMLElement} 127 type EnhancedMatch = RegExpMatchArray & {rule: CompiledMode, type: MatchType} 128 type MatchType = "begin" | "end" | "illegal" 129 130 interface Emitter { 131 addKeyword(text: string, kind: string): void 132 addText(text: string): void 133 toHTML(): string 134 finalize(): void 135 closeAllNodes(): void 136 openNode(kind: string): void 137 closeNode(): void 138 addSublanguage(emitter: Emitter, subLanguageName: string): void 139 } 140 141 /* modes */ 142 143 interface ModeCallbacks { 144 "on:end"?: Function, 145 "on:begin"?: Function, 146 } 147 148 interface Mode extends ModeCallbacks, ModeDetails { 149 150 } 151 152 interface LanguageDetail { 153 name?: string 154 rawDefinition?: () => Language 155 aliases?: string[] 156 disableAutodetect?: boolean 157 contains: ("self"|Mode)[] 158 case_insensitive?: boolean 159 keywords?: Record<string, any> | string 160 compiled?: boolean 161 } 162 163 type Language = LanguageDetail & Partial<Mode> 164 165 interface CompiledLanguage extends LanguageDetail, CompiledMode { 166 compiled: true 167 contains: CompiledMode[] 168 keywords: Record<string, any> 169 } 170 171 type KeywordData = [string, number]; 172 type KeywordDict = Record<string, KeywordData> 173 174 type CompiledMode = Omit<Mode, 'contains'> & 175 { 176 contains: CompiledMode[] 177 keywords: KeywordDict 178 data: Record<string, any> 179 terminator_end: string 180 keywordPatternRe: RegExp 181 beginRe: RegExp 182 endRe: RegExp 183 illegalRe: RegExp 184 matcher: any 185 compiled: true 186 starts?: CompiledMode 187 parent?: CompiledMode 188 } 189 190 interface ModeDetails { 191 begin?: RegExp | string 192 end?: RegExp | string 193 className?: string 194 contains?: ("self" | Mode)[] 195 endsParent?: boolean 196 endsWithParent?: boolean 197 endSameAsBegin?: boolean 198 skip?: boolean 199 excludeBegin?: boolean 200 excludeEnd?: boolean 201 returnBegin?: boolean 202 returnEnd?: boolean 203 __beforeBegin?: Function 204 parent?: Mode 205 starts?:Mode 206 lexemes?: string | RegExp 207 keywords?: Record<string, any> | string 208 beginKeywords?: string 209 relevance?: number 210 illegal?: string | RegExp 211 variants?: Mode[] 212 cached_variants?: Mode[] 213 // parsed 214 subLanguage?: string | string[] 215 compiled?: boolean 216 } 217 218 // deprecated API since v10 219 // declare module 'highlight.js/lib/highlight.js'; 220 221 declare module 'highlight.js' { 222 export = hljs; 223 } 224 225 declare module 'highlight.js/lib/core' { 226 export = hljs; 227 } 228 229 declare module 'highlight.js/lib/core.js' { 230 export = hljs; 231 } 232 233 declare module 'highlight.js/lib/languages/*' { 234 export default function(hljs?: HLJSApi): LanguageDetail; 235 }