774a78e3a6f5cd60d1b1d08e30345be9691dcab601d90cf4e8b18308cb5a2e0a9169b990fab7bdd9db030bac0e47fdb89422026cb84af055768d14e29721f3 7.2 KB

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