@discord/markdown-react 0.1.5 → 0.1.7

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 CHANGED
@@ -1 +1 @@
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})})}
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.5",
3
+ "version": "0.1.7",
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.5"
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.3"
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,18 @@
1
- import React from "react";
2
- import {
3
- Node,
4
- AnyNode,
5
- CodeBlock,
6
- Emoji,
7
- Timestamp,
8
- Mention,
9
- Link,
10
- List,
11
- Block,
12
- Node,
1
+ import type {
2
+ AnyNode,
3
+ Block,
4
+ CodeBlock,
5
+ Emoji,
6
+ Link,
7
+ List,
8
+ Mention,
9
+ Timestamp,
13
10
  } from "@discord/markdown-types";
11
+ import type React from "react";
14
12
 
15
13
  type NodeRenderProps<P> = P & { siblings: Node[]; index: number };
16
- type NodeRenderPropsWithChildren<P = {}> = React.PropsWithChildren<
17
- NodeRenderProps<P>
14
+ type NodeRenderPropsWithChildren<P = unknown> = React.PropsWithChildren<
15
+ NodeRenderProps<P>
18
16
  >;
19
17
 
20
18
  // inline
@@ -31,17 +29,17 @@ export type CodeProps = NodeRenderPropsWithChildren;
31
29
  export type CodeBlockProps = NodeRenderProps<CodeBlock["value"]>;
32
30
 
33
31
  export type InlineProps =
34
- | BoldProps
35
- | ItalicProps
36
- | UnderlineProps
37
- | StrikethroughProps
38
- | SpoilerProps
39
- | EmojiProps
40
- | TimestampProps
41
- | MentionProps
42
- | LinkProps
43
- | CodeProps
44
- | CodeBlockProps;
32
+ | BoldProps
33
+ | ItalicProps
34
+ | UnderlineProps
35
+ | StrikethroughProps
36
+ | SpoilerProps
37
+ | EmojiProps
38
+ | TimestampProps
39
+ | MentionProps
40
+ | LinkProps
41
+ | CodeProps
42
+ | CodeBlockProps;
45
43
 
46
44
  // block
47
45
  export type HeadingProps = NodeRenderPropsWithChildren<{ level: 1 | 2 | 3 }>;
@@ -54,66 +52,66 @@ export type BlockProps = HeadingProps | ListProps | QuoteProps | SmallProps;
54
52
  // top-level
55
53
  export type TextProps = NodeRenderPropsWithChildren;
56
54
  export type ParagraphProps = NodeRenderPropsWithChildren;
57
- export type EmptyProps = NodeRenderProps;
55
+ export type EmptyProps = NodeRenderProps<Record<string, never>>;
58
56
  export type ListItemProps = NodeRenderPropsWithChildren;
59
57
 
60
58
  export type TopLevelProps =
61
- | TextProps
62
- | ParagraphProps
63
- | EmptyProps
64
- | ListItemProps;
59
+ | TextProps
60
+ | ParagraphProps
61
+ | EmptyProps
62
+ | ListItemProps;
65
63
 
66
64
  export type AllProps = InlineProps | BlockProps | TopLevelProps;
67
65
 
68
66
  export type RendererProps = {
69
- // inline
70
- bold: BoldProps;
71
- italic: ItalicProps;
72
- underline: UnderlineProps;
73
- strikethrough: StrikethroughProps;
74
- spoiler: SpoilerProps;
75
- emoji: EmojiProps;
76
- timestamp: TimestampProps;
77
- mention: MentionProps;
78
- link: LinkProps;
79
- code: CodeProps;
80
- code_block: CodeBlockProps;
81
-
82
- // block
83
- heading: HeadingProps;
84
- list: ListProps;
85
- quote: QuoteProps;
86
- small: SmallProps;
87
-
88
- // top-level
89
- text: TextProps;
90
- paragraph: ParagraphProps;
91
- empty: EmptyProps;
92
-
93
- // pseudo
94
- listItem: ListItemProps;
67
+ // inline
68
+ bold: BoldProps;
69
+ italic: ItalicProps;
70
+ underline: UnderlineProps;
71
+ strikethrough: StrikethroughProps;
72
+ spoiler: SpoilerProps;
73
+ emoji: EmojiProps;
74
+ timestamp: TimestampProps;
75
+ mention: MentionProps;
76
+ link: LinkProps;
77
+ code: CodeProps;
78
+ code_block: CodeBlockProps;
79
+
80
+ // block
81
+ heading: HeadingProps;
82
+ list: ListProps;
83
+ quote: QuoteProps;
84
+ small: SmallProps;
85
+
86
+ // top-level
87
+ text: TextProps;
88
+ paragraph: ParagraphProps;
89
+ empty: EmptyProps;
90
+
91
+ // pseudo
92
+ listItem: ListItemProps;
95
93
  };
96
94
 
97
95
  export type Renderers = {
98
- readonly [K in keyof RendererProps]?: React.FC<RendererProps[K]>;
96
+ readonly [K in keyof RendererProps]?: React.FC<RendererProps[K]>;
99
97
  };
100
98
 
101
99
  export type NodeProps = {
102
- node: AnyNode;
103
- renderers: Renderers;
100
+ node: AnyNode;
101
+ renderers: Renderers;
104
102
  };
105
103
 
106
104
  export type NodeListProps = {
107
- nodes: AnyNode[];
108
- renderers: Renderers;
105
+ nodes: AnyNode[];
106
+ renderers: Renderers;
109
107
  };
110
108
 
111
109
  export type MarkdownProps = {
112
- content: string;
113
- renderers: Renderers;
110
+ content: string;
111
+ renderers: Renderers;
114
112
  };
115
113
 
116
- export const RULES: Set<string>;
114
+ declare const RULES: Set<string>;
117
115
 
118
116
  export function useAst(): Block[];
119
117
  export function Node(props: NodeProps): React.ReactNode;