UNPKG

5.38 kBTypeScriptView Raw
1/**
2 * markdown-to-jsx is a fork of [simple-markdown v0.2.2](https://github.com/Khan/simple-markdown)
3 * from Khan Academy. Thank you Khan devs for making such an awesome and extensible
4 * parsing infra... without it, half of the optimizations here wouldn't be feasible. 🙏🏼
5 */
6import * as React from 'react';
7export declare namespace MarkdownToJSX {
8 /**
9 * RequireAtLeastOne<{ ... }> <- only requires at least one key
10 */
11 type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
12 [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
13 }[Keys];
14 export type CreateElement = typeof React.createElement;
15 export type HTMLTags = keyof JSX.IntrinsicElements;
16 export type State = {
17 _inAnchor?: boolean;
18 _inline?: boolean;
19 _inTable?: boolean;
20 _key?: React.Key;
21 _list?: boolean;
22 _simple?: boolean;
23 };
24 export type ParserResult = {
25 [key: string]: any;
26 type?: string;
27 };
28 export type NestedParser = (input: string, state?: MarkdownToJSX.State) => MarkdownToJSX.ParserResult;
29 export type Parser<ParserOutput> = (capture: RegExpMatchArray, nestedParse: NestedParser, state?: MarkdownToJSX.State) => ParserOutput;
30 export type RuleOutput = (ast: MarkdownToJSX.ParserResult, state: MarkdownToJSX.State) => JSX.Element;
31 export type Rule<ParserOutput = MarkdownToJSX.ParserResult> = {
32 _match: (source: string, state: MarkdownToJSX.State, prevCapturedString?: string) => RegExpMatchArray;
33 _order: Priority;
34 _parse: MarkdownToJSX.Parser<ParserOutput>;
35 _react?: (node: ParserOutput, output: RuleOutput, state?: MarkdownToJSX.State) => React.ReactChild;
36 };
37 export type Rules = {
38 [key: string]: Rule;
39 };
40 export type Override = RequireAtLeastOne<{
41 component: React.ElementType;
42 props: Object;
43 }> | React.ElementType;
44 export type Overrides = {
45 [tag in HTMLTags]?: Override;
46 } & {
47 [customComponent: string]: Override;
48 };
49 export type Options = Partial<{
50 /**
51 * Ultimate control over the output of all rendered JSX.
52 */
53 createElement: (tag: Parameters<CreateElement>[0], props: React.Props<any>, ...children: React.ReactChild[]) => JSX.Element;
54 /**
55 * Disable the compiler's best-effort transcription of provided raw HTML
56 * into JSX-equivalent. This is the functionality that prevents the need to
57 * use `dangerouslySetInnerHTML` in React.
58 */
59 disableParsingRawHTML: boolean;
60 /**
61 * Forces the compiler to always output content with a block-level wrapper
62 * (`<p>` or any block-level syntax your markdown already contains.)
63 */
64 forceBlock: boolean;
65 /**
66 * Forces the compiler to always output content with an inline wrapper (`<span>`)
67 */
68 forceInline: boolean;
69 /**
70 * Supply additional HTML entity: unicode replacement mappings.
71 *
72 * Pass only the inner part of the entity as the key,
73 * e.g. `&le;` -> `{ "le": "\u2264" }`
74 *
75 * By default
76 * the following entites are replaced with their unicode equivalents:
77 *
78 * ```
79 * &amp;
80 * &apos;
81 * &gt;
82 * &lt;
83 * &nbsp;
84 * &quot;
85 * ```
86 */
87 namedCodesToUnicode: {
88 [key: string]: string;
89 };
90 /**
91 * Selectively control the output of particular HTML tags as they would be
92 * emitted by the compiler.
93 */
94 overrides: Overrides;
95 /**
96 * Declare the type of the wrapper to be used when there are multiple
97 * children to render. Set to `null` to get an array of children back
98 * without any wrapper, or use `React.Fragment` to get a React element
99 * that won't show up in the DOM.
100 */
101 wrapper: React.ElementType | null;
102 /**
103 * Forces the compiler to wrap results, even if there is only a single
104 * child or no children.
105 */
106 forceWrapper: boolean;
107 /**
108 * Override normalization of non-URI-safe characters for use in generating
109 * HTML IDs for anchor linking purposes.
110 */
111 slugify: (source: string) => string;
112 }>;
113 export {};
114}
115declare enum Priority {
116 /**
117 * anything that must scan the tree before everything else
118 */
119 MAX = 0,
120 /**
121 * scans for block-level constructs
122 */
123 HIGH = 1,
124 /**
125 * inline w/ more priority than other inline
126 */
127 MED = 2,
128 /**
129 * inline elements
130 */
131 LOW = 3,
132 /**
133 * bare text and stuff that is considered leftovers
134 */
135 MIN = 4
136}
137export declare function compiler(markdown: string, options?: MarkdownToJSX.Options): JSX.Element;
138/**
139 * A simple HOC for easy React use. Feed the markdown content as a direct child
140 * and the rest is taken care of automatically.
141 */
142declare const Markdown: React.FC<{
143 [key: string]: any;
144 children: string;
145 options?: MarkdownToJSX.Options;
146}>;
147export default Markdown;