index.d.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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: (codeOrLanguageName: string, optionsOrCode: string | HighlightOptions, ignoreIllegals?: boolean) => HighlightResult
  25. highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult
  26. highlightBlock: (element: HTMLElement) => void
  27. highlightElement: (element: HTMLElement) => void
  28. configure: (options: Partial<HLJSOptions>) => void
  29. initHighlighting: () => void
  30. initHighlightingOnLoad: () => void
  31. highlightAll: () => void
  32. registerLanguage: (languageName: string, language: LanguageFn) => void
  33. unregisterLanguage: (languageName: string) => void
  34. listLanguages: () => string[]
  35. registerAliases: (aliasList: string | string[], { languageName } : {languageName: string}) => void
  36. getLanguage: (languageName: string) => Language | undefined
  37. autoDetection: (languageName: string) => boolean
  38. inherit: <T>(original: T, ...args: Record<string, any>[]) => T
  39. addPlugin: (plugin: HLJSPlugin) => void
  40. removePlugin: (plugin: HLJSPlugin) => void
  41. debugMode: () => void
  42. safeMode: () => void
  43. versionString: string
  44. vuePlugin: () => VuePlugin
  45. regex: {
  46. concat: (...args: (RegExp | string)[]) => string,
  47. lookahead: (re: RegExp | string) => string,
  48. either: (...args: (RegExp | string)[] | [...(RegExp | string)[], RegexEitherOptions]) => string,
  49. optional: (re: RegExp | string) => string,
  50. anyNumberOfTimes: (re: RegExp | string) => string
  51. }
  52. newInstance: () => HLJSApi
  53. }
  54. interface ModesAPI {
  55. SHEBANG: (mode?: Partial<Mode> & {binary?: string | RegExp}) => Mode
  56. BACKSLASH_ESCAPE: Mode
  57. QUOTE_STRING_MODE: Mode
  58. APOS_STRING_MODE: Mode
  59. PHRASAL_WORDS_MODE: Mode
  60. COMMENT: (begin: string | RegExp, end: string | RegExp, modeOpts?: Mode | {}) => Mode
  61. C_LINE_COMMENT_MODE: Mode
  62. C_BLOCK_COMMENT_MODE: Mode
  63. HASH_COMMENT_MODE: Mode
  64. NUMBER_MODE: Mode
  65. C_NUMBER_MODE: Mode
  66. BINARY_NUMBER_MODE: Mode
  67. REGEXP_MODE: Mode
  68. TITLE_MODE: Mode
  69. UNDERSCORE_TITLE_MODE: Mode
  70. METHOD_GUARD: Mode
  71. END_SAME_AS_BEGIN: (mode: Mode) => Mode
  72. // built in regex
  73. IDENT_RE: string
  74. UNDERSCORE_IDENT_RE: string
  75. MATCH_NOTHING_RE: string
  76. NUMBER_RE: string
  77. C_NUMBER_RE: string
  78. BINARY_NUMBER_RE: string
  79. RE_STARTERS_RE: string
  80. }
  81. export type LanguageFn = (hljs: HLJSApi) => Language
  82. export type CompilerExt = (mode: Mode, parent: Mode | Language | null) => void
  83. export interface HighlightResult {
  84. code?: string
  85. relevance : number
  86. value : string
  87. language? : string
  88. illegal : boolean
  89. errorRaised? : Error
  90. // * for auto-highlight
  91. secondBest? : Omit<HighlightResult, 'second_best'>
  92. // private
  93. _illegalBy? : illegalData
  94. _emitter : Emitter
  95. _top? : Language | CompiledMode
  96. }
  97. export interface AutoHighlightResult extends HighlightResult {}
  98. export interface illegalData {
  99. message: string
  100. context: string
  101. index: number
  102. resultSoFar : string
  103. mode: CompiledMode
  104. }
  105. export type BeforeHighlightContext = {
  106. code: string,
  107. language: string,
  108. result?: HighlightResult
  109. }
  110. export type PluginEvent = keyof HLJSPlugin;
  111. export type HLJSPlugin = {
  112. 'after:highlight'?: (result: HighlightResult) => void,
  113. 'before:highlight'?: (context: BeforeHighlightContext) => void,
  114. 'after:highlightElement'?: (data: { el: Element, result: HighlightResult, text: string}) => void,
  115. 'before:highlightElement'?: (data: { el: Element, language: string}) => void,
  116. // TODO: Old API, remove with v12
  117. 'after:highlightBlock'?: (data: { block: Element, result: HighlightResult, text: string}) => void,
  118. 'before:highlightBlock'?: (data: { block: Element, language: string}) => void,
  119. }
  120. interface EmitterConstructor {
  121. new (opts: any): Emitter
  122. }
  123. export interface HighlightOptions {
  124. language: string
  125. ignoreIllegals?: boolean
  126. }
  127. export interface HLJSOptions {
  128. noHighlightRe: RegExp
  129. languageDetectRe: RegExp
  130. classPrefix: string
  131. cssSelector: string
  132. languages?: string[]
  133. __emitter: EmitterConstructor
  134. ignoreUnescapedHTML?: boolean
  135. throwUnescapedHTML?: boolean
  136. }
  137. export interface CallbackResponse {
  138. data: Record<string, any>
  139. ignoreMatch: () => void
  140. isMatchIgnored: boolean
  141. }
  142. export type ModeCallback = (match: RegExpMatchArray, response: CallbackResponse) => void
  143. export type Language = LanguageDetail & Partial<Mode>
  144. export interface Mode extends ModeCallbacks, ModeDetails {}
  145. export interface LanguageDetail {
  146. name?: string
  147. unicodeRegex?: boolean
  148. rawDefinition?: () => Language
  149. aliases?: string[]
  150. disableAutodetect?: boolean
  151. contains: (Mode)[]
  152. case_insensitive?: boolean
  153. keywords?: Record<string, any> | string
  154. isCompiled?: boolean,
  155. exports?: any,
  156. classNameAliases?: Record<string, string>
  157. compilerExtensions?: CompilerExt[]
  158. supersetOf?: string
  159. }
  160. // technically private, but exported for convenience as this has
  161. // been a pretty stable API and is quite useful
  162. export interface Emitter {
  163. addKeyword(text: string, kind: string): void
  164. addText(text: string): void
  165. toHTML(): string
  166. finalize(): void
  167. closeAllNodes(): void
  168. openNode(kind: string): void
  169. closeNode(): 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?: Record<string, any> | 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. }