UNPKG

20.7 kBTypeScriptView Raw
1// Type definitions for Marked 4.0
2// Project: https://github.com/markedjs/marked, https://marked.js.org
3// Definitions by: William Orr <https://github.com/worr>
4// BendingBender <https://github.com/BendingBender>
5// CrossR <https://github.com/CrossR>
6// Mike Wickett <https://github.com/mwickett>
7// Hitomi Hatsukaze <https://github.com/htkzhtm>
8// Ezra Celli <https://github.com/ezracelli>
9// Romain LE BARO <https://github.com/scandinave>
10// Sarun Intaralawan <https://github.com/sarunint>
11// Tony Brix <https://github.com/UziTech>
12// Anatolii Titov <https://github.com/Toliak>
13// Jean-Francois Cere <https://github.com/jfcere>
14// Mykhaylo Stolyarchuk <https://github.com/MykSto>
15// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
16
17/**
18 * Compiles markdown to HTML asynchronously.
19 *
20 * @param src String of markdown source to be compiled
21 * @param options Hash of options, having async: true
22 * @return Promise of string of compiled HTML
23 */
24export function marked(src: string, options: marked.MarkedOptions & {async: true}): Promise<string>;
25
26/**
27 * Compiles markdown to HTML synchronously.
28 *
29 * @param src String of markdown source to be compiled
30 * @param options Optional hash of options
31 * @return String of compiled HTML
32 */
33export function marked(src: string, options?: marked.MarkedOptions): string;
34
35/**
36 * Compiles markdown to HTML asynchronously with a callback.
37 *
38 * @param src String of markdown source to be compiled
39 * @param callback Function called when the markdownString has been fully parsed when using async highlighting
40 */
41export function marked(src: string, callback: (error: any, parseResult: string) => void): void;
42
43/**
44 * Compiles markdown to HTML asynchronously with a callback.
45 *
46 * @param src String of markdown source to be compiled
47 * @param options Hash of options
48 * @param callback Function called when the markdownString has been fully parsed when using async highlighting
49 */
50export function marked(
51 src: string,
52 options: marked.MarkedOptions,
53 callback: (error: any, parseResult: string) => void,
54): void;
55
56export class Lexer extends marked.Lexer {}
57export class Parser extends marked.Parser {}
58export class Tokenizer<T = never> extends marked.Tokenizer<T> {}
59export class Renderer<T = never> extends marked.Renderer<T> {}
60export class TextRenderer extends marked.TextRenderer {}
61export class Slugger extends marked.Slugger {}
62
63export namespace marked {
64 const defaults: MarkedOptions;
65
66 /**
67 * @param src String of markdown source to be compiled
68 * @param options Hash of options
69 */
70 function lexer(src: string, options?: MarkedOptions): TokensList;
71
72 /**
73 * Compiles markdown to HTML.
74 *
75 * @param src String of markdown source to be compiled
76 * @param callback Function called when the markdownString has been fully parsed when using async highlighting
77 * @return String of compiled HTML
78 */
79 function parse(src: string, callback: (error: any, parseResult: string) => void): void;
80
81 /**
82 * Compiles markdown to HTML asynchronously.
83 *
84 * @param src String of markdown source to be compiled
85 * @param options Hash of options having async: true
86 * @return Promise of string of compiled HTML
87 */
88 function parse(src: string, options: MarkedOptions & {async: true}): Promise<string>;
89
90 /**
91 * Compiles markdown to HTML synchronously.
92 *
93 * @param src String of markdown source to be compiled
94 * @param options Optional hash of options
95 * @return String of compiled HTML
96 */
97 function parse(src: string, options?: MarkedOptions): string;
98
99 /**
100 * Compiles markdown to HTML synchronously.
101 *
102 * @param src String of markdown source to be compiled
103 * @param options Optional hash of options
104 * @param callback Function called when the markdownString has been fully parsed when using async highlighting
105 * @return String of compiled HTML
106 */
107 function parse(src: string, options: MarkedOptions, callback: (error: any, parseResult: string) => void): void;
108
109 /**
110 * @param src Tokenized source as array of tokens
111 * @param options Hash of options
112 */
113 function parser(src: Token[] | TokensList, options?: MarkedOptions): string;
114
115 /**
116 * Compiles markdown to HTML without enclosing `p` tag.
117 *
118 * @param src String of markdown source to be compiled
119 * @param options Hash of options
120 * @return String of compiled HTML
121 */
122 function parseInline(src: string, options?: MarkedOptions): string;
123
124 /**
125 * Sets the default options.
126 *
127 * @param options Hash of options
128 */
129 function options(options: MarkedOptions): typeof marked;
130
131 /**
132 * Sets the default options.
133 *
134 * @param options Hash of options
135 */
136 function setOptions(options: MarkedOptions): typeof marked;
137
138 /**
139 * Gets the original marked default options.
140 */
141 function getDefaults(): MarkedOptions;
142
143 function walkTokens(tokens: Token[] | TokensList, callback: (token: Token) => void): typeof marked;
144
145 /**
146 * Use Extension
147 * @param MarkedExtension
148 */
149 function use(...extensions: MarkedExtension[]): void;
150
151 class Tokenizer<T = never> {
152 constructor(options?: MarkedOptions);
153 options: MarkedOptions;
154 space(this: Tokenizer & TokenizerThis, src: string): Tokens.Space | T;
155 code(this: Tokenizer & TokenizerThis, src: string): Tokens.Code | T;
156 fences(this: Tokenizer & TokenizerThis, src: string): Tokens.Code | T;
157 heading(this: Tokenizer & TokenizerThis, src: string): Tokens.Heading | T;
158 hr(this: Tokenizer & TokenizerThis, src: string): Tokens.Hr | T;
159 blockquote(this: Tokenizer & TokenizerThis, src: string): Tokens.Blockquote | T;
160 list(this: Tokenizer & TokenizerThis, src: string): Tokens.List | T;
161 html(this: Tokenizer & TokenizerThis, src: string): Tokens.HTML | T;
162 def(this: Tokenizer & TokenizerThis, src: string): Tokens.Def | T;
163 table(this: Tokenizer & TokenizerThis, src: string): Tokens.Table | T;
164 lheading(this: Tokenizer & TokenizerThis, src: string): Tokens.Heading | T;
165 paragraph(this: Tokenizer & TokenizerThis, src: string): Tokens.Paragraph | T;
166 text(this: Tokenizer & TokenizerThis, src: string): Tokens.Text | T;
167 escape(this: Tokenizer & TokenizerThis, src: string): Tokens.Escape | T;
168 tag(this: Tokenizer & TokenizerThis, src: string): Tokens.Tag | T;
169 link(this: Tokenizer & TokenizerThis, src: string): Tokens.Image | Tokens.Link | T;
170 reflink(
171 this: Tokenizer & TokenizerThis,
172 src: string,
173 links: Tokens.Link[] | Tokens.Image[],
174 ): Tokens.Link | Tokens.Image | Tokens.Text | T;
175 emStrong(
176 this: Tokenizer & TokenizerThis,
177 src: string,
178 maskedSrc: string,
179 prevChar: string,
180 ): Tokens.Em | Tokens.Strong | T;
181 codespan(this: Tokenizer & TokenizerThis, src: string): Tokens.Codespan | T;
182 br(this: Tokenizer & TokenizerThis, src: string): Tokens.Br | T;
183 del(this: Tokenizer & TokenizerThis, src: string): Tokens.Del | T;
184 autolink(this: Tokenizer & TokenizerThis, src: string, mangle: (cap: string) => string): Tokens.Link | T;
185 url(this: Tokenizer & TokenizerThis, src: string, mangle: (cap: string) => string): Tokens.Link | T;
186 inlineText(this: Tokenizer & TokenizerThis, src: string, smartypants: (cap: string) => string): Tokens.Text | T;
187 }
188
189 type TokenizerObject = Partial<Omit<Tokenizer<false>, 'constructor' | 'options'>>;
190
191 class Renderer<T = never> {
192 constructor(options?: MarkedOptions);
193 options: MarkedOptions;
194 code(this: Renderer | RendererThis, code: string, language: string | undefined, isEscaped: boolean): string | T;
195 blockquote(this: Renderer | RendererThis, quote: string): string | T;
196 html(this: Renderer | RendererThis, html: string): string | T;
197 heading(
198 this: Renderer | RendererThis,
199 text: string,
200 level: 1 | 2 | 3 | 4 | 5 | 6,
201 raw: string,
202 slugger: Slugger,
203 ): string | T;
204 hr(this: Renderer | RendererThis): string | T;
205 list(this: Renderer | RendererThis, body: string, ordered: boolean, start: number): string | T;
206 listitem(this: Renderer | RendererThis, text: string, task: boolean, checked: boolean): string | T;
207 checkbox(this: Renderer | RendererThis, checked: boolean): string | T;
208 paragraph(this: Renderer | RendererThis, text: string): string | T;
209 table(this: Renderer | RendererThis, header: string, body: string): string | T;
210 tablerow(this: Renderer | RendererThis, content: string): string | T;
211 tablecell(
212 this: Renderer | RendererThis,
213 content: string,
214 flags: {
215 header: boolean;
216 align: 'center' | 'left' | 'right' | null;
217 },
218 ): string | T;
219 strong(this: Renderer | RendererThis, text: string): string | T;
220 em(this: Renderer | RendererThis, text: string): string | T;
221 codespan(this: Renderer | RendererThis, code: string): string | T;
222 br(this: Renderer | RendererThis): string | T;
223 del(this: Renderer | RendererThis, text: string): string | T;
224 link(this: Renderer | RendererThis, href: string | null, title: string | null, text: string): string | T;
225 image(this: Renderer | RendererThis, href: string | null, title: string | null, text: string): string | T;
226 text(this: Renderer | RendererThis, text: string): string | T;
227 }
228
229 type RendererObject = Partial<Omit<Renderer<false>, 'constructor' | 'options'>>;
230
231 class TextRenderer {
232 strong(text: string): string;
233 em(text: string): string;
234 codespan(text: string): string;
235 del(text: string): string;
236 text(text: string): string;
237 link(href: string | null, title: string | null, text: string): string;
238 image(href: string | null, title: string | null, text: string): string;
239 br(): string;
240 html(text: string): string;
241 }
242
243 class Parser {
244 constructor(options?: MarkedOptions);
245 tokens: Token[] | TokensList;
246 token: Token | null;
247 options: MarkedOptions;
248 renderer: Renderer;
249 textRenderer: TextRenderer;
250 slugger: Slugger;
251 static parse(src: Token[] | TokensList, options?: MarkedOptions): string;
252 static parseInline(src: Token[], options?: MarkedOptions): string;
253 parse(src: Token[] | TokensList): string;
254 parseInline(src: Token[], renderer?: Renderer): string;
255 next(): Token;
256 }
257
258 class Lexer {
259 constructor(options?: MarkedOptions);
260 tokens: TokensList;
261 options: MarkedOptions;
262 rules: Rules;
263 static rules: Rules;
264 static lex(src: string, options?: MarkedOptions): TokensList;
265 static lexInline(src: string, options?: MarkedOptions): Token[];
266 lex(src: string): TokensList;
267 blockTokens(src: string, tokens: Token[]): Token[];
268 blockTokens(src: string, tokens: TokensList): TokensList;
269 inline(src: string, tokens?: Token[]): Token[];
270 inlineTokens(src: string, tokens?: Token[]): Token[];
271 state: {
272 inLink: boolean;
273 inRawBlock: boolean;
274 top: boolean;
275 };
276 }
277
278 class Slugger {
279 seen: { [slugValue: string]: number };
280 slug(value: string, options?: SluggerOptions): string;
281 }
282
283 interface SluggerOptions {
284 dryrun: boolean;
285 }
286
287 interface Rules {
288 [ruleName: string]: RegExp | Rules;
289 }
290
291 type TokensList = Token[] & {
292 links: {
293 [key: string]: { href: string | null; title: string | null };
294 };
295 };
296
297 type Token =
298 | Tokens.Space
299 | Tokens.Code
300 | Tokens.Heading
301 | Tokens.Table
302 | Tokens.Hr
303 | Tokens.Blockquote
304 | Tokens.List
305 | Tokens.ListItem
306 | Tokens.Paragraph
307 | Tokens.HTML
308 | Tokens.Text
309 | Tokens.Def
310 | Tokens.Escape
311 | Tokens.Tag
312 | Tokens.Image
313 | Tokens.Link
314 | Tokens.Strong
315 | Tokens.Em
316 | Tokens.Codespan
317 | Tokens.Br
318 | Tokens.Del;
319
320 namespace Tokens {
321 interface Space {
322 type: 'space';
323 raw: string;
324 }
325
326 interface Code {
327 type: 'code';
328 raw: string;
329 codeBlockStyle?: 'indented' | undefined;
330 lang?: string | undefined;
331 text: string;
332 }
333
334 interface Heading {
335 type: 'heading';
336 raw: string;
337 depth: number;
338 text: string;
339 tokens: Token[];
340 }
341
342 interface Table {
343 type: 'table';
344 raw: string;
345 align: Array<'center' | 'left' | 'right' | null>;
346 header: TableCell[];
347 rows: TableCell[][];
348 }
349
350 interface TableCell {
351 text: string;
352 tokens: Token[];
353 }
354
355 interface Hr {
356 type: 'hr';
357 raw: string;
358 }
359
360 interface Blockquote {
361 type: 'blockquote';
362 raw: string;
363 text: string;
364 tokens: Token[];
365 }
366
367 interface List {
368 type: 'list';
369 raw: string;
370 ordered: boolean;
371 start: number | '';
372 loose: boolean;
373 items: ListItem[];
374 }
375
376 interface ListItem {
377 type: 'list_item';
378 raw: string;
379 task: boolean;
380 checked?: boolean | undefined;
381 loose: boolean;
382 text: string;
383 tokens: Token[];
384 }
385
386 interface Paragraph {
387 type: 'paragraph';
388 raw: string;
389 pre?: boolean | undefined;
390 text: string;
391 tokens: Token[];
392 }
393
394 interface HTML {
395 type: 'html';
396 raw: string;
397 pre: boolean;
398 text: string;
399 }
400
401 interface Text {
402 type: 'text';
403 raw: string;
404 text: string;
405 tokens?: Token[] | undefined;
406 }
407
408 interface Def {
409 type: 'def';
410 raw: string;
411 tag: string;
412 href: string;
413 title: string;
414 }
415
416 interface Escape {
417 type: 'escape';
418 raw: string;
419 text: string;
420 }
421
422 interface Tag {
423 type: 'text' | 'html';
424 raw: string;
425 inLink: boolean;
426 inRawBlock: boolean;
427 text: string;
428 }
429
430 interface Link {
431 type: 'link';
432 raw: string;
433 href: string;
434 title: string;
435 text: string;
436 tokens: Token[];
437 }
438
439 interface Image {
440 type: 'image';
441 raw: string;
442 href: string;
443 title: string;
444 text: string;
445 }
446
447 interface Strong {
448 type: 'strong';
449 raw: string;
450 text: string;
451 tokens: Token[];
452 }
453
454 interface Em {
455 type: 'em';
456 raw: string;
457 text: string;
458 tokens: Token[];
459 }
460
461 interface Codespan {
462 type: 'codespan';
463 raw: string;
464 text: string;
465 }
466
467 interface Br {
468 type: 'br';
469 raw: string;
470 }
471
472 interface Del {
473 type: 'del';
474 raw: string;
475 text: string;
476 tokens: Token[];
477 }
478
479 interface Generic {
480 [index: string]: any;
481 type: string;
482 raw: string;
483 tokens?: Token[] | undefined;
484 }
485 }
486
487 interface TokenizerThis {
488 lexer: Lexer;
489 }
490
491 interface TokenizerExtension {
492 name: string;
493 level: 'block' | 'inline';
494 start?: ((this: TokenizerThis, src: string) => number | void) | undefined;
495 tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | void;
496 childTokens?: string[] | undefined;
497 }
498
499 interface RendererThis {
500 parser: Parser;
501 }
502
503 interface RendererExtension {
504 name: string;
505 renderer: (this: RendererThis, token: Tokens.Generic) => string | false;
506 }
507
508 interface MarkedExtension {
509 /**
510 * True will tell marked to await any walkTokens functions before parsing the tokens and returning an HTML string.
511 */
512 async?: boolean;
513
514 /**
515 * A prefix URL for any relative link.
516 */
517 baseUrl?: string | undefined;
518
519 /**
520 * Enable GFM line breaks. This option requires the gfm option to be true.
521 */
522 breaks?: boolean | undefined;
523
524 /**
525 * Add tokenizers and renderers to marked
526 */
527 extensions?:
528 | Array<TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension)>
529 | undefined;
530
531 /**
532 * Enable GitHub flavored markdown.
533 */
534 gfm?: boolean | undefined;
535
536 /**
537 * Include an id attribute when emitting headings.
538 */
539 headerIds?: boolean | undefined;
540
541 /**
542 * Set the prefix for header tag ids.
543 */
544 headerPrefix?: string | undefined;
545
546 /**
547 * A function to highlight code blocks. The function can either be
548 * synchronous (returning a string) or asynchronous (callback invoked
549 * with an error if any occurred during highlighting and a string
550 * if highlighting was successful)
551 */
552 highlight?(code: string, lang: string, callback?: (error: any, code?: string) => void): string | void;
553
554 /**
555 * Set the prefix for code block classes.
556 */
557 langPrefix?: string | undefined;
558
559 /**
560 * Mangle autolinks (<email@domain.com>).
561 */
562 mangle?: boolean | undefined;
563
564 /**
565 * Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
566 */
567 pedantic?: boolean | undefined;
568
569 /**
570 * Type: object Default: new Renderer()
571 *
572 * An object containing functions to render tokens to HTML.
573 */
574 renderer?: Renderer | RendererObject | undefined;
575
576 /**
577 * Sanitize the output. Ignore any HTML that has been input.
578 */
579 sanitize?: boolean | undefined;
580
581 /**
582 * Optionally sanitize found HTML with a sanitizer function.
583 */
584 sanitizer?(html: string): string;
585
586 /**
587 * Shows an HTML error message when rendering fails.
588 */
589 silent?: boolean | undefined;
590
591 /**
592 * Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
593 */
594 smartLists?: boolean | undefined;
595
596 /**
597 * Use "smart" typograhic punctuation for things like quotes and dashes.
598 */
599 smartypants?: boolean | undefined;
600
601 /**
602 * The tokenizer defines how to turn markdown text into tokens.
603 */
604 tokenizer?: Tokenizer | TokenizerObject | undefined;
605
606 /**
607 * The walkTokens function gets called with every token.
608 * Child tokens are called before moving on to sibling tokens.
609 * Each token is passed by reference so updates are persisted when passed to the parser.
610 * The return value of the function is ignored.
611 */
612 walkTokens?: ((token: Token) => void) | undefined;
613 /**
614 * Generate closing slash for self-closing tags (<br/> instead of <br>)
615 */
616 xhtml?: boolean | undefined;
617 }
618
619 interface MarkedOptions extends Omit<MarkedExtension, 'extensions'> {
620 /**
621 * Type: object Default: new Renderer()
622 *
623 * An object containing functions to render tokens to HTML.
624 */
625 renderer?: Renderer | undefined;
626
627 /**
628 * The tokenizer defines how to turn markdown text into tokens.
629 */
630 tokenizer?: Tokenizer | undefined;
631 }
632}
633
\No newline at end of file