UNPKG

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