UNPKG

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