@discord/markdown-react 0.1.4 → 0.1.6
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.
- package/dist/src/index.js +1 -1
- package/package.json +3 -3
- package/src/index.d.ts +53 -64
package/dist/src/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,Fragment as r}from"react/jsx-runtime";import{parse as t}from"@discord/markdown-wasm";import{
|
|
1
|
+
import{jsx as e,Fragment as r}from"react/jsx-runtime";import{parse as t}from"@discord/markdown-wasm";import{createContext as n,useContext as l,useMemo as o}from"react";let i=n(null);export function useAst(){return l(i)}export const RULES=new Set(["bold","italic","underline","strikethrough","spoiler","emoji","timestamp","mention","link","code","code_block","heading","list","quote","small"]);export function Node({node:r,renderers:t,...n}){let l=t[r.type];if(!function(e,r){if(null==e)throw Error(`Attempted to render "${r}" but no renderer was provided`)}(l,r.type),Array.isArray(r.value))return e(l,{...n,children:e(NodeList,{nodes:r.value,renderers:t})});switch(r.type){case"mention":case"timestamp":case"emoji":case"code_block":return e(l,{...r.value,...n});case"link":{let o=r.value,i=null;if("normal"===o.type){let{value:{text:r,url:n}}=o;i=r?e(NodeList,{nodes:r,renderers:t}):n}return e(l,{...r.value,...n,children:i})}case"text":case"code":return e(l,{...n,children:r.value});case"heading":return e(l,{level:r.value.level,...n,children:e(NodeList,{nodes:r.value.content,renderers:t})});case"list":{let o=t.listItem??"li";return e(l,{...r.value,...n,children:r.value.items.map((n,l)=>e(o,{siblings:r.value.items,index:l,children:e(NodeList,{nodes:n.content,renderers:t})},l))})}case"empty":return e(l,{...n});case"small":return e(l,{...n,children:e(NodeList,{nodes:r.value.content,renderers:t})});default:throw TypeError(`Unknown node type "${r.type}"`)}}export function NodeList({nodes:t,renderers:n}){return e(r,{children:t.map((r,l)=>e(Node,{node:r,renderers:n,siblings:t,index:l},l))})}export default function s({content:r,renderers:n}){let l=o(()=>{let e=Object.keys(n).filter(e=>RULES.has(e));return e.length>0?e:null},[n]),s=o(()=>t(r,l),[r,l]);return e(i.Provider,{value:s,children:e(NodeList,{nodes:s,renderers:n})})}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/markdown-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React library for parsing and rendering Discord Markdown",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"src/index.d.ts"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@discord/markdown-wasm": "0.1.
|
|
16
|
+
"@discord/markdown-wasm": "0.1.6"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@swc/cli": "^0.5.2",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"vite": "^5.4.0",
|
|
27
27
|
"vite-plugin-wasm": "^3.3.0",
|
|
28
28
|
"vitest": "^2.0.3",
|
|
29
|
-
"@discord/markdown-types": "0.1.
|
|
29
|
+
"@discord/markdown-types": "0.1.4"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": "17 - 19"
|
package/src/index.d.ts
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
Node,
|
|
4
|
-
AnyNode,
|
|
5
|
-
CodeBlock,
|
|
6
|
-
Emoji,
|
|
7
|
-
Timestamp,
|
|
8
|
-
Mention,
|
|
9
|
-
Link,
|
|
10
|
-
List,
|
|
11
|
-
Block,
|
|
12
|
-
Node,
|
|
13
|
-
} from "@discord/markdown-types";
|
|
1
|
+
import {} from "@discord/markdown-types";
|
|
2
|
+
import type React from "react";
|
|
14
3
|
|
|
15
4
|
type NodeRenderProps<P> = P & { siblings: Node[]; index: number };
|
|
16
|
-
type NodeRenderPropsWithChildren<P =
|
|
17
|
-
|
|
5
|
+
type NodeRenderPropsWithChildren<P = unknown> = React.PropsWithChildren<
|
|
6
|
+
NodeRenderProps<P>
|
|
18
7
|
>;
|
|
19
8
|
|
|
20
9
|
// inline
|
|
@@ -31,17 +20,17 @@ export type CodeProps = NodeRenderPropsWithChildren;
|
|
|
31
20
|
export type CodeBlockProps = NodeRenderProps<CodeBlock["value"]>;
|
|
32
21
|
|
|
33
22
|
export type InlineProps =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
| BoldProps
|
|
24
|
+
| ItalicProps
|
|
25
|
+
| UnderlineProps
|
|
26
|
+
| StrikethroughProps
|
|
27
|
+
| SpoilerProps
|
|
28
|
+
| EmojiProps
|
|
29
|
+
| TimestampProps
|
|
30
|
+
| MentionProps
|
|
31
|
+
| LinkProps
|
|
32
|
+
| CodeProps
|
|
33
|
+
| CodeBlockProps;
|
|
45
34
|
|
|
46
35
|
// block
|
|
47
36
|
export type HeadingProps = NodeRenderPropsWithChildren<{ level: 1 | 2 | 3 }>;
|
|
@@ -58,62 +47,62 @@ export type EmptyProps = NodeRenderProps;
|
|
|
58
47
|
export type ListItemProps = NodeRenderPropsWithChildren;
|
|
59
48
|
|
|
60
49
|
export type TopLevelProps =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
50
|
+
| TextProps
|
|
51
|
+
| ParagraphProps
|
|
52
|
+
| EmptyProps
|
|
53
|
+
| ListItemProps;
|
|
65
54
|
|
|
66
55
|
export type AllProps = InlineProps | BlockProps | TopLevelProps;
|
|
67
56
|
|
|
68
57
|
export type RendererProps = {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
58
|
+
// inline
|
|
59
|
+
bold: BoldProps;
|
|
60
|
+
italic: ItalicProps;
|
|
61
|
+
underline: UnderlineProps;
|
|
62
|
+
strikethrough: StrikethroughProps;
|
|
63
|
+
spoiler: SpoilerProps;
|
|
64
|
+
emoji: EmojiProps;
|
|
65
|
+
timestamp: TimestampProps;
|
|
66
|
+
mention: MentionProps;
|
|
67
|
+
link: LinkProps;
|
|
68
|
+
code: CodeProps;
|
|
69
|
+
code_block: CodeBlockProps;
|
|
70
|
+
|
|
71
|
+
// block
|
|
72
|
+
heading: HeadingProps;
|
|
73
|
+
list: ListProps;
|
|
74
|
+
quote: QuoteProps;
|
|
75
|
+
small: SmallProps;
|
|
76
|
+
|
|
77
|
+
// top-level
|
|
78
|
+
text: TextProps;
|
|
79
|
+
paragraph: ParagraphProps;
|
|
80
|
+
empty: EmptyProps;
|
|
81
|
+
|
|
82
|
+
// pseudo
|
|
83
|
+
listItem: ListItemProps;
|
|
95
84
|
};
|
|
96
85
|
|
|
97
86
|
export type Renderers = {
|
|
98
|
-
|
|
87
|
+
readonly [K in keyof RendererProps]?: React.FC<RendererProps[K]>;
|
|
99
88
|
};
|
|
100
89
|
|
|
101
90
|
export type NodeProps = {
|
|
102
|
-
|
|
103
|
-
|
|
91
|
+
node: AnyNode;
|
|
92
|
+
renderers: Renderers;
|
|
104
93
|
};
|
|
105
94
|
|
|
106
95
|
export type NodeListProps = {
|
|
107
|
-
|
|
108
|
-
|
|
96
|
+
nodes: AnyNode[];
|
|
97
|
+
renderers: Renderers;
|
|
109
98
|
};
|
|
110
99
|
|
|
111
100
|
export type MarkdownProps = {
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
content: string;
|
|
102
|
+
renderers: Renderers;
|
|
114
103
|
};
|
|
115
104
|
|
|
116
|
-
|
|
105
|
+
declare const RULES: Set<string>;
|
|
117
106
|
|
|
118
107
|
export function useAst(): Block[];
|
|
119
108
|
export function Node(props: NodeProps): React.ReactNode;
|