markdown-to-jsx 9.7.11 → 9.7.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,573 @@
1
+ import * as React from "react";
2
+ /**
3
+ * Analogous to `node.type`. Please note that the values here may change at any time,
4
+ * so do not hard code against the value directly.
5
+ */
6
+ declare const RuleTypeConst: {
7
+ readonly blockQuote: 0;
8
+ readonly breakLine: 1;
9
+ readonly breakThematic: 2;
10
+ readonly codeBlock: 3;
11
+ readonly codeInline: 4;
12
+ readonly footnote: 5;
13
+ readonly footnoteReference: 6;
14
+ readonly frontmatter: 7;
15
+ readonly gfmTask: 8;
16
+ readonly heading: 9;
17
+ readonly htmlBlock: 10;
18
+ readonly htmlComment: 11;
19
+ readonly htmlSelfClosing: 12;
20
+ readonly image: 13;
21
+ readonly link: 14;
22
+ readonly orderedList: 15;
23
+ readonly paragraph: 16;
24
+ readonly ref: 17;
25
+ readonly refCollection: 18;
26
+ readonly table: 19;
27
+ readonly text: 20;
28
+ readonly textFormatted: 21;
29
+ readonly unorderedList: 22;
30
+ };
31
+ type RuleTypeValue = (typeof RuleTypeConst)[keyof typeof RuleTypeConst];
32
+ /**
33
+ * markdown-to-jsx types and interfaces
34
+ */
35
+ declare namespace MarkdownToJSX {
36
+ /**
37
+ * RequireAtLeastOne<{ ... }> <- only requires at least one key
38
+ */
39
+ type RequireAtLeastOne<
40
+ T,
41
+ Keys extends keyof T = keyof T
42
+ > = Pick<T, Exclude<keyof T, Keys>> & { [K in Keys]-? : Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>> }[Keys];
43
+ /**
44
+ * React.createElement function type
45
+ */
46
+ export type CreateElement = typeof React.createElement;
47
+ /**
48
+ * HTML tag names that can be used in JSX
49
+ */
50
+ export type HTMLTags = keyof React.JSX.IntrinsicElements & (string & {});
51
+ /**
52
+ * Parser and renderer state
53
+ */
54
+ export type State = {
55
+ /** true if the current content is inside anchor link grammar */
56
+ inAnchor?: boolean;
57
+ /** true if inside a blockquote */
58
+ inBlockQuote?: boolean;
59
+ /** true if parsing in an HTML context */
60
+ inHTML?: boolean;
61
+ /** true if in a list */
62
+ inList?: boolean;
63
+ /** true if parsing in an inline context (subset of rules around formatting and links) */
64
+ inline?: boolean;
65
+ /** use this for the `key` prop */
66
+ key?: string | number;
67
+ /** reference definitions (footnotes are stored with '^' prefix) */
68
+ refs?: {
69
+ [key: string]: {
70
+ target: string;
71
+ title: string;
72
+ };
73
+ };
74
+ /** current recursion depth during rendering */
75
+ renderDepth?: number;
76
+ /** internal: block parse recursion depth */
77
+ _depth?: number;
78
+ /** internal: disable setext heading detection (lazy blockquote continuation) */
79
+ _noSetext?: boolean;
80
+ /** internal: HTML nesting depth for stack overflow protection */
81
+ _htmlDepth?: number;
82
+ /** internal: set by collectReferenceDefinitions when input ends inside an unclosed fence */
83
+ _endsInsideFence?: boolean;
84
+ };
85
+ /**
86
+ * Blockquote node in the AST
87
+ */
88
+ export interface BlockQuoteNode {
89
+ /** Optional alert type (Note, Tip, Warning, etc.) */
90
+ alert?: string;
91
+ /** Child nodes within the blockquote */
92
+ children: MarkdownToJSX.ASTNode[];
93
+ type: typeof RuleType2.blockQuote;
94
+ }
95
+ /**
96
+ * Hard line break node
97
+ */
98
+ export interface BreakLineNode {
99
+ type: typeof RuleType2.breakLine;
100
+ }
101
+ /**
102
+ * Thematic break (horizontal rule) node
103
+ */
104
+ export interface BreakThematicNode {
105
+ type: typeof RuleType2.breakThematic;
106
+ }
107
+ /**
108
+ * Code block node (fenced code blocks)
109
+ */
110
+ export interface CodeBlockNode {
111
+ type: typeof RuleType2.codeBlock;
112
+ /** HTML attributes for the code block */
113
+ attrs?: React.JSX.IntrinsicAttributes;
114
+ /** Programming language identifier */
115
+ lang?: string;
116
+ /** Code content */
117
+ text: string;
118
+ }
119
+ /**
120
+ * Inline code node
121
+ */
122
+ export interface CodeInlineNode {
123
+ type: typeof RuleType2.codeInline;
124
+ /** Code text */
125
+ text: string;
126
+ }
127
+ /**
128
+ * Footnote definition node (not rendered, stored in refCollection)
129
+ */
130
+ export interface FootnoteNode {
131
+ type: typeof RuleType2.footnote;
132
+ }
133
+ /**
134
+ * Footnote reference node
135
+ */
136
+ export interface FootnoteReferenceNode {
137
+ type: typeof RuleType2.footnoteReference;
138
+ /** Link target (anchor) */
139
+ target: string;
140
+ /** Display text */
141
+ text: string;
142
+ }
143
+ /**
144
+ * YAML frontmatter node
145
+ */
146
+ export interface FrontmatterNode {
147
+ type: typeof RuleType2.frontmatter;
148
+ /** Frontmatter content */
149
+ text: string;
150
+ }
151
+ /**
152
+ * GFM task list item node
153
+ */
154
+ export interface GFMTaskNode {
155
+ type: typeof RuleType2.gfmTask;
156
+ /** Whether the task is completed */
157
+ completed: boolean;
158
+ }
159
+ /**
160
+ * Heading node
161
+ */
162
+ export interface HeadingNode {
163
+ type: typeof RuleType2.heading;
164
+ /** Child nodes (text content) */
165
+ children: MarkdownToJSX.ASTNode[];
166
+ /** Generated HTML ID for anchor linking */
167
+ id: string;
168
+ /** Heading level (1-6) */
169
+ level: 1 | 2 | 3 | 4 | 5 | 6;
170
+ }
171
+ /**
172
+ * HTML comment node
173
+ */
174
+ export interface HTMLCommentNode {
175
+ type: typeof RuleType2.htmlComment;
176
+ /** Comment text */
177
+ text: string;
178
+ }
179
+ /**
180
+ * Image node
181
+ */
182
+ export interface ImageNode {
183
+ type: typeof RuleType2.image;
184
+ /** Alt text */
185
+ alt?: string;
186
+ /** Image URL */
187
+ target: string;
188
+ /** Title attribute */
189
+ title?: string;
190
+ }
191
+ /**
192
+ * Link node
193
+ */
194
+ export interface LinkNode {
195
+ type: typeof RuleType2.link;
196
+ /** Child nodes (link text) */
197
+ children: MarkdownToJSX.ASTNode[];
198
+ /** Link URL (null for reference links without definition) */
199
+ target: string | null;
200
+ /** Title attribute */
201
+ title?: string;
202
+ }
203
+ /**
204
+ * Ordered list node
205
+ */
206
+ export interface OrderedListNode {
207
+ type: typeof RuleType2.orderedList;
208
+ /** Array of list items, each item is an array of nodes */
209
+ items: MarkdownToJSX.ASTNode[][];
210
+ /** Starting number for the list */
211
+ start?: number;
212
+ }
213
+ /**
214
+ * Unordered list node
215
+ */
216
+ export interface UnorderedListNode {
217
+ type: typeof RuleType2.unorderedList;
218
+ /** Array of list items, each item is an array of nodes */
219
+ items: MarkdownToJSX.ASTNode[][];
220
+ }
221
+ /**
222
+ * Paragraph node
223
+ */
224
+ export interface ParagraphNode {
225
+ type: typeof RuleType2.paragraph;
226
+ /** Child nodes */
227
+ children: MarkdownToJSX.ASTNode[];
228
+ }
229
+ /**
230
+ * Reference definition node (not rendered, stored in refCollection)
231
+ */
232
+ export interface ReferenceNode {
233
+ type: typeof RuleType2.ref;
234
+ }
235
+ /**
236
+ * Reference collection node (appears at AST root, includes footnotes with '^' prefix)
237
+ */
238
+ export interface ReferenceCollectionNode {
239
+ type: typeof RuleType2.refCollection;
240
+ /** Map of reference labels to their definitions */
241
+ refs: {
242
+ [key: string]: {
243
+ target: string;
244
+ title: string;
245
+ };
246
+ };
247
+ }
248
+ /**
249
+ * Table node
250
+ */
251
+ export interface TableNode {
252
+ type: typeof RuleType2.table;
253
+ /**
254
+ * alignment for each table column
255
+ */
256
+ align: ("left" | "right" | "center")[];
257
+ /** Table cells (3D array: rows -> cells -> nodes) */
258
+ cells: MarkdownToJSX.ASTNode[][][];
259
+ /** Table header row */
260
+ header: MarkdownToJSX.ASTNode[][];
261
+ }
262
+ /**
263
+ * Plain text node
264
+ */
265
+ export interface TextNode {
266
+ type: typeof RuleType2.text;
267
+ /** Text content */
268
+ text: string;
269
+ }
270
+ /**
271
+ * Formatted text node (bold, italic, etc.)
272
+ */
273
+ export interface FormattedTextNode {
274
+ type: typeof RuleType2.textFormatted;
275
+ /**
276
+ * the corresponding html tag
277
+ */
278
+ tag: string;
279
+ /** Child nodes */
280
+ children: MarkdownToJSX.ASTNode[];
281
+ }
282
+ /** @deprecated Use `FormattedTextNode` instead. */
283
+ export type TextFormattedNode = FormattedTextNode;
284
+ /**
285
+ * HTML block node (includes JSX components)
286
+ */
287
+ export interface HTMLNode {
288
+ type: typeof RuleType2.htmlBlock;
289
+ /** Parsed HTML attributes */
290
+ attrs?: Record<string, any>;
291
+ /** Parsed child nodes (always parsed, even for verbatim blocks) */
292
+ children?: ASTNode[] | undefined;
293
+ /** @internal Whether this is a closing tag */
294
+ _isClosingTag?: boolean;
295
+ /** @internal Whether this is a verbatim block (script, style, pre, etc.) */
296
+ _verbatim?: boolean;
297
+ /** @internal Original raw attribute string */
298
+ _rawAttrs?: string;
299
+ /** @internal Original raw HTML content (for verbatim blocks) */
300
+ _rawText?: string | undefined;
301
+ /** @deprecated Use `_rawText` instead. This property will be removed in a future major version. */
302
+ text?: string | undefined;
303
+ /** HTML tag name */
304
+ tag: string;
305
+ }
306
+ /**
307
+ * Self-closing HTML tag node
308
+ */
309
+ export interface HTMLSelfClosingNode {
310
+ type: typeof RuleType2.htmlSelfClosing;
311
+ /** Parsed HTML attributes */
312
+ attrs?: Record<string, any>;
313
+ /** @internal Whether this is a closing tag */
314
+ _isClosingTag?: boolean;
315
+ /** HTML tag name */
316
+ tag: string;
317
+ /** @internal Original raw HTML content */
318
+ _rawText?: string;
319
+ }
320
+ /**
321
+ * Union type of all possible AST node types
322
+ */
323
+ export type ASTNode = BlockQuoteNode | BreakLineNode | BreakThematicNode | CodeBlockNode | CodeInlineNode | FootnoteNode | FootnoteReferenceNode | FrontmatterNode | GFMTaskNode | HeadingNode | HTMLCommentNode | ImageNode | LinkNode | OrderedListNode | UnorderedListNode | ParagraphNode | ReferenceNode | ReferenceCollectionNode | TableNode | TextNode | FormattedTextNode | HTMLNode | HTMLSelfClosingNode;
324
+ /**
325
+ * Function type for rendering AST nodes
326
+ */
327
+ export type ASTRender = (ast: MarkdownToJSX.ASTNode | MarkdownToJSX.ASTNode[], state: MarkdownToJSX.State) => React.ReactNode;
328
+ /**
329
+ * Override configuration for HTML tags or custom components
330
+ */
331
+ export type Override = RequireAtLeastOne<{
332
+ component: React.ElementType;
333
+ props: Object;
334
+ }> | React.ElementType;
335
+ /**
336
+ * Map of HTML tags and custom components to their override configurations
337
+ */
338
+ export type Overrides = { [tag in HTMLTags]? : Override } & {
339
+ [customComponent: string]: Override;
340
+ };
341
+ /**
342
+ * Compiler options
343
+ */
344
+ export type Options = Partial<{
345
+ /**
346
+ * Ultimate control over the output of all rendered JSX.
347
+ */
348
+ createElement: (tag: Parameters<CreateElement>[0], props: React.JSX.IntrinsicAttributes, ...children: React.ReactNode[]) => React.ReactNode;
349
+ /**
350
+ * The library automatically generates an anchor tag for bare URLs included in the markdown
351
+ * document, but this behavior can be disabled if desired.
352
+ */
353
+ disableAutoLink: boolean;
354
+ /**
355
+ * Disable the compiler's best-effort transcription of provided raw HTML
356
+ * into JSX-equivalent. This is the functionality that prevents the need to
357
+ * use `dangerouslySetInnerHTML` in React.
358
+ */
359
+ disableParsingRawHTML: boolean;
360
+ /**
361
+ * Disable the compiler's parsing of HTML blocks.
362
+ */
363
+ ignoreHTMLBlocks?: boolean;
364
+ /**
365
+ * Enable GFM tagfilter extension to filter potentially dangerous HTML tags.
366
+ * When enabled, the following tags are escaped: title, textarea, style, xmp,
367
+ * iframe, noembed, noframes, script, plaintext.
368
+ * https://github.github.com/gfm/#disallowed-raw-html-extension-
369
+ * @default true
370
+ */
371
+ tagfilter?: boolean;
372
+ /**
373
+ * Forces the compiler to have space between hash sign and the header text which
374
+ * is explicitly stated in the most of the markdown specs.
375
+ * https://github.github.com/gfm/#atx-heading
376
+ * `The opening sequence of # characters must be followed by a space or by the end of line.`
377
+ */
378
+ enforceAtxHeadings: boolean;
379
+ /**
380
+ * **⚠️ SECURITY WARNING: STRONGLY DISCOURAGED FOR USER INPUTS**
381
+ *
382
+ * When enabled, attempts to eval expressions in JSX props that cannot be serialized
383
+ * as JSON (functions, variables, complex expressions). This uses `eval()` which can
384
+ * execute arbitrary code.
385
+ *
386
+ * **ONLY use this option when:**
387
+ * - The markdown source is completely trusted (e.g., your own documentation)
388
+ * - You control all JSX components and their props
389
+ * - The content is NOT user-generated or user-editable
390
+ *
391
+ * **DO NOT use this option when:**
392
+ * - Processing user-submitted markdown
393
+ * - Rendering untrusted content
394
+ * - Building public-facing applications with user content
395
+ *
396
+ * Example unsafe input: `<Component onClick={() => fetch('/admin/delete-all')} />`
397
+ *
398
+ * When disabled (default), unserializable expressions remain as strings that can be
399
+ * safely inspected or handled on a case-by-case basis via custom renderRule logic.
400
+ *
401
+ * @default false
402
+ */
403
+ evalUnserializableExpressions?: boolean;
404
+ /**
405
+ * Forces the compiler to always output content with a block-level wrapper
406
+ * (`<p>` or any block-level syntax your markdown already contains.)
407
+ */
408
+ forceBlock: boolean;
409
+ /**
410
+ * Forces the compiler to always output content with an inline wrapper (`<span>`)
411
+ */
412
+ forceInline: boolean;
413
+ /**
414
+ * Forces the compiler to wrap results, even if there is only a single
415
+ * child or no children.
416
+ */
417
+ forceWrapper: boolean;
418
+ /**
419
+ * Selectively control the output of particular HTML tags as they would be
420
+ * emitted by the compiler.
421
+ */
422
+ overrides: Overrides;
423
+ /**
424
+ * Allows for full control over rendering of particular rules.
425
+ * For example, to implement a LaTeX renderer such as `react-katex`:
426
+ *
427
+ * ```
428
+ * renderRule(next, node, renderChildren, state) {
429
+ * if (node.type === RuleType.codeBlock && node.lang === 'latex') {
430
+ * return (
431
+ * <TeX as="div" key={state.key}>
432
+ * {String.raw`${node.text}`}
433
+ * </TeX>
434
+ * )
435
+ * }
436
+ *
437
+ * return next();
438
+ * }
439
+ * ```
440
+ *
441
+ * Thar be dragons obviously, but you can do a lot with this
442
+ * (have fun!) To see how things work internally, check the `render`
443
+ * method in source for a particular rule.
444
+ */
445
+ renderRule: (next: () => React.ReactNode, node: ASTNode, renderChildren: ASTRender, state: State) => React.ReactNode;
446
+ /**
447
+ * Override the built-in sanitizer function for URLs, etc if desired. The built-in version is available as a library
448
+ export called `sanitizer`.
449
+ */
450
+ sanitizer: (value: string, tag: string, attribute: string) => string | null;
451
+ /**
452
+ * Override normalization of non-URI-safe characters for use in generating
453
+ * HTML IDs for anchor linking purposes.
454
+ */
455
+ slugify: (input: string, defaultFn: (input: string) => string) => string;
456
+ /**
457
+ * Declare the type of the wrapper to be used when there are multiple
458
+ * children to render. Set to `null` to get an array of children back
459
+ * without any wrapper, or use `React.Fragment` to get a React element
460
+ * that won't show up in the DOM.
461
+ */
462
+ wrapper: React.ElementType | null;
463
+ /**
464
+ * Props to apply to the wrapper element.
465
+ */
466
+ wrapperProps?: React.JSX.IntrinsicAttributes;
467
+ /**
468
+ * Preserve frontmatter in the output by rendering it as a <pre> element.
469
+ * By default, frontmatter is parsed but not rendered.
470
+ * @default false
471
+ */
472
+ preserveFrontmatter?: boolean;
473
+ /**
474
+ * Optimize rendering for streaming scenarios where markdown content arrives
475
+ * incrementally (e.g., from LLM APIs). When enabled, incomplete inline syntax
476
+ * is suppressed to avoid displaying raw markdown characters while waiting
477
+ * for the closing delimiter to arrive.
478
+ *
479
+ * Fenced code blocks render normally with content visible as it streams.
480
+ *
481
+ * @default false
482
+ *
483
+ * @example
484
+ * ```tsx
485
+ * // Streaming markdown example
486
+ * function StreamingMarkdown({ content }) {
487
+ * return (
488
+ * <Markdown options={{ optimizeForStreaming: true }}>
489
+ * {content}
490
+ * </Markdown>
491
+ * )
492
+ * }
493
+ * ```
494
+ */
495
+ optimizeForStreaming?: boolean;
496
+ }>;
497
+ }
498
+ declare const RuleType2: typeof RuleTypeConst;
499
+ type RuleType2 = RuleTypeValue;
500
+ /**
501
+ * Main parser entry point - matches original parser interface
502
+ */
503
+ declare function parser(source: string, options?: MarkdownToJSX.Options): MarkdownToJSX.ASTNode[];
504
+ /**
505
+ * Sanitize URLs and other input values to prevent XSS attacks.
506
+ * Filters out javascript:, vbscript:, and data: URLs (except data:image).
507
+ *
508
+ *
509
+ * @param input - The URL or value to sanitize
510
+ * @returns Sanitized value, or null if unsafe
511
+ */
512
+ declare function sanitizer(input: string): string | null;
513
+ /**
514
+ * Convert a string to a URL-safe slug by normalizing characters and replacing spaces with hyphens.
515
+ * Based on https://stackoverflow.com/a/18123682/1141611
516
+ * Not complete, but probably good enough.
517
+ *
518
+ *
519
+ * @param str - String to slugify
520
+ * @returns URL-safe slug
521
+ */
522
+ declare function slugify(str: string): string;
523
+ /**
524
+ * Override configuration for HTML tags or custom components in HTML output
525
+ */
526
+ type HTMLOverride = {
527
+ component?: string;
528
+ props?: Record<string, string | number | boolean>;
529
+ } | string;
530
+ /**
531
+ * Map of HTML tags and custom components to their override configurations
532
+ */
533
+ type HTMLOverrides = { [tag in MarkdownToJSX.HTMLTags]? : HTMLOverride } & {
534
+ [customComponent: string]: HTMLOverride;
535
+ };
536
+ /**
537
+ * HTML compiler options
538
+ */
539
+ type HTMLOptions = Omit<MarkdownToJSX.Options, "createElement" | "wrapperProps" | "overrides"> & {
540
+ /** Reference definitions for link resolution */
541
+ refs?: {
542
+ [key: string]: {
543
+ target: string;
544
+ title: string | undefined;
545
+ };
546
+ };
547
+ /** Custom rendering function for AST rules */
548
+ renderRule?: (next: () => string, node: MarkdownToJSX.ASTNode, renderChildren: (children: MarkdownToJSX.ASTNode[]) => string, state: MarkdownToJSX.State) => string;
549
+ /** Override configurations for HTML tags */
550
+ overrides?: HTMLOverrides;
551
+ /** Props for wrapper element */
552
+ wrapperProps?: Record<string, string | number | boolean>;
553
+ /** Internal flag for wrapper auto-detection */
554
+ wrapperWasAutoSet?: boolean;
555
+ };
556
+ /**
557
+ * Convert AST nodes to HTML string
558
+ *
559
+ * @param nodes - Array of AST nodes to render
560
+ * @param options - HTML compiler options
561
+ * @returns HTML string
562
+ */
563
+ declare function astToHTML(nodes: MarkdownToJSX.ASTNode[], options?: HTMLOptions): string;
564
+ /**
565
+ * Compiler function that parses markdown and renders to HTML string
566
+ * Convenience function that combines parser() and html()
567
+ *
568
+ * @param markdown - Markdown string to compile
569
+ * @param options - HTML compiler options
570
+ * @returns HTML string
571
+ */
572
+ declare function compiler(markdown: string, options?: HTMLOptions): string;
573
+ export { slugify, sanitizer, parser, compiler, astToHTML, RuleType2 as RuleType, MarkdownToJSX, HTMLOverrides, HTMLOverride, HTMLOptions };