c32e3c3c7c1d629c23228b1cd5687d668f763b96707bae82f4816c461c4fdf7752e7b33d5ccf2ca7bc5f76633c7d07c4661b9ce724646d0d502c75fa72de83 9.3 KB

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