@discord/markdown-react 0.1.3 → 0.1.5
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 +8 -5
- package/src/index.d.ts +25 -22
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{useMemo as n,createContext as l,useContext as o}from"react";let i=l(null);export function useAst(){return o(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 s=t.listItem??"li";return e(l,{...r.value,...n,children:r.value.items.map((n,l)=>e(s,{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:l}){let o=n(()=>{let e=Object.keys(l).filter(e=>RULES.has(e));return e.length>0?e:null},[l]),s=n(()=>t(r,o),[r,o]);return e(i.Provider,{value:s,children:e(NodeList,{nodes:s,renderers:l})})}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/markdown-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React library for parsing and rendering Discord Markdown",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -13,8 +13,7 @@
|
|
|
13
13
|
"src/index.d.ts"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"
|
|
17
|
-
"@discord/markdown-wasm": "0.1.3"
|
|
16
|
+
"@discord/markdown-wasm": "0.1.5"
|
|
18
17
|
},
|
|
19
18
|
"devDependencies": {
|
|
20
19
|
"@swc/cli": "^0.5.2",
|
|
@@ -23,13 +22,17 @@
|
|
|
23
22
|
"@testing-library/react": "^16.0.0",
|
|
24
23
|
"@types/react": "^18",
|
|
25
24
|
"jsdom": "^24.1.1",
|
|
25
|
+
"react": "^18.3.1",
|
|
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.3"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "17 - 19"
|
|
30
33
|
},
|
|
31
34
|
"scripts": {
|
|
32
|
-
"test": "vitest run",
|
|
35
|
+
"test": "pnpm build && vitest run",
|
|
33
36
|
"build": "swc src/index.jsx -d dist"
|
|
34
37
|
}
|
|
35
38
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -9,22 +9,26 @@ import {
|
|
|
9
9
|
Link,
|
|
10
10
|
List,
|
|
11
11
|
Block,
|
|
12
|
+
Node,
|
|
12
13
|
} from "@discord/markdown-types";
|
|
13
14
|
|
|
14
|
-
type NodeRenderProps<P = {
|
|
15
|
+
type NodeRenderProps<P> = P & { siblings: Node[]; index: number };
|
|
16
|
+
type NodeRenderPropsWithChildren<P = {}> = React.PropsWithChildren<
|
|
17
|
+
NodeRenderProps<P>
|
|
18
|
+
>;
|
|
15
19
|
|
|
16
20
|
// inline
|
|
17
|
-
export type BoldProps =
|
|
18
|
-
export type ItalicProps =
|
|
19
|
-
export type UnderlineProps =
|
|
20
|
-
export type StrikethroughProps =
|
|
21
|
-
export type SpoilerProps =
|
|
22
|
-
export type EmojiProps = Emoji["value"]
|
|
23
|
-
export type TimestampProps = Timestamp["value"]
|
|
24
|
-
export type MentionProps = Mention["value"]
|
|
25
|
-
export type LinkProps =
|
|
26
|
-
export type CodeProps =
|
|
27
|
-
export type CodeBlockProps = CodeBlock["value"]
|
|
21
|
+
export type BoldProps = NodeRenderPropsWithChildren;
|
|
22
|
+
export type ItalicProps = NodeRenderPropsWithChildren;
|
|
23
|
+
export type UnderlineProps = NodeRenderPropsWithChildren;
|
|
24
|
+
export type StrikethroughProps = NodeRenderPropsWithChildren;
|
|
25
|
+
export type SpoilerProps = NodeRenderPropsWithChildren;
|
|
26
|
+
export type EmojiProps = NodeRenderProps<Emoji["value"]>;
|
|
27
|
+
export type TimestampProps = NodeRenderProps<Timestamp["value"]>;
|
|
28
|
+
export type MentionProps = NodeRenderProps<Mention["value"]>;
|
|
29
|
+
export type LinkProps = NodeRenderPropsWithChildren<Link["value"]>;
|
|
30
|
+
export type CodeProps = NodeRenderPropsWithChildren;
|
|
31
|
+
export type CodeBlockProps = NodeRenderProps<CodeBlock["value"]>;
|
|
28
32
|
|
|
29
33
|
export type InlineProps =
|
|
30
34
|
| BoldProps
|
|
@@ -40,18 +44,18 @@ export type InlineProps =
|
|
|
40
44
|
| CodeBlockProps;
|
|
41
45
|
|
|
42
46
|
// block
|
|
43
|
-
export type HeadingProps =
|
|
44
|
-
export type ListProps =
|
|
45
|
-
export type QuoteProps =
|
|
46
|
-
export type SmallProps =
|
|
47
|
+
export type HeadingProps = NodeRenderPropsWithChildren<{ level: 1 | 2 | 3 }>;
|
|
48
|
+
export type ListProps = NodeRenderPropsWithChildren<List["value"]>;
|
|
49
|
+
export type QuoteProps = NodeRenderPropsWithChildren;
|
|
50
|
+
export type SmallProps = NodeRenderPropsWithChildren;
|
|
47
51
|
|
|
48
52
|
export type BlockProps = HeadingProps | ListProps | QuoteProps | SmallProps;
|
|
49
53
|
|
|
50
54
|
// top-level
|
|
51
|
-
export type TextProps =
|
|
52
|
-
export type ParagraphProps =
|
|
53
|
-
export type EmptyProps =
|
|
54
|
-
export type ListItemProps =
|
|
55
|
+
export type TextProps = NodeRenderPropsWithChildren;
|
|
56
|
+
export type ParagraphProps = NodeRenderPropsWithChildren;
|
|
57
|
+
export type EmptyProps = NodeRenderProps;
|
|
58
|
+
export type ListItemProps = NodeRenderPropsWithChildren;
|
|
55
59
|
|
|
56
60
|
export type TopLevelProps =
|
|
57
61
|
| TextProps
|
|
@@ -109,8 +113,7 @@ export type MarkdownProps = {
|
|
|
109
113
|
renderers: Renderers;
|
|
110
114
|
};
|
|
111
115
|
|
|
112
|
-
export const
|
|
113
|
-
export const BLOCK_RULES: string[];
|
|
116
|
+
export const RULES: Set<string>;
|
|
114
117
|
|
|
115
118
|
export function useAst(): Block[];
|
|
116
119
|
export function Node(props: NodeProps): React.ReactNode;
|