@discord/markdown-react 0.1.1 → 0.1.3

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/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright 2025 Discord, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
package/dist/src/index.js CHANGED
@@ -1 +1 @@
1
- import{parse as e}from"@discord/markdown-wasm";import*as t from"react";import{memo as r,useMemo as n}from"react";function l(e,t){if(null==e)throw Error(`Attempted to render "${t}" but no renderer was provided`)}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:e,renderers:r}){let n=r[e.type];if(l(n,e.type),Array.isArray(e.value))return t.createElement(n,null,t.createElement(NodeList,{nodes:e.value,renderers:r}));switch(e.type){case"mention":case"timestamp":case"emoji":case"code_block":return t.createElement(n,e.value);case"link":let a=e.value,o=null;if("normal"===a.type){let{value:{text:e,url:n}}=a;o=e?t.createElement(NodeList,{nodes:e,renderers:r}):n}return t.createElement(n,e.value,o);case"quote":let s=r.paragraph;return l(s,"paragraph"),t.createElement(n,null,e.value.lines.map((e,n)=>t.createElement(s,{key:n,value:e,renderers:r},t.createElement(NodeList,{nodes:e,renderers:r}))));case"text":case"code":return t.createElement(n,null,e.value);case"heading":return t.createElement(n,{level:e.value.level},t.createElement(NodeList,{nodes:e.value.content,renderers:r}));case"list":let c=r.listItem??"li";return t.createElement(n,e.value,e.value.items.map((e,n)=>t.createElement(c,{key:n},t.createElement(NodeList,{nodes:e.content,renderers:r}))));case"empty":return t.createElement(n,null);case"small":return t.createElement(n,null,t.createElement(NodeList,{nodes:e.value.content,renderers:r}));default:throw TypeError(`Unknown node type "${e.type}"`)}}export const NodeList=r(function({nodes:e,renderers:r}){return t.createElement(t.Fragment,null,e.map((e,n)=>t.createElement(Node,{key:n,node:e,renderers:r})))});export default function a({content:r,renderers:l}){let a=n(()=>{let e=Object.keys(l).filter(e=>RULES.has(e));return e.length>0?e:null},[l]),o=n(()=>e(r,a),[r,a]);return t.createElement(NodeList,{nodes:o,renderers:l})}
1
+ import{jsx as e,Fragment as r}from"react/jsx-runtime";import{parse as t}from"@discord/markdown-wasm";import{memo as n,useMemo as o,createContext as l,useContext as s}from"react";let i=l(null);export function useAst(){return s(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}){let n=t[r.type];if(!function(e,r){if(null==e)throw Error(`Attempted to render "${r}" but no renderer was provided`)}(n,r.type),Array.isArray(r.value))return e(n,{children:e(NodeList,{nodes:r.value,renderers:t})});switch(r.type){case"mention":case"timestamp":case"emoji":case"code_block":return e(n,{...r.value});case"link":let o=r.value,l=null;if("normal"===o.type){let{value:{text:r,url:n}}=o;l=r?e(NodeList,{nodes:r,renderers:t}):n}return e(n,{...r.value,children:l});case"text":case"code":return e(n,{children:r.value});case"heading":return e(n,{level:r.value.level,children:e(NodeList,{nodes:r.value.content,renderers:t})});case"list":let s=t.listItem??"li";return e(n,{...r.value,children:r.value.items.map((r,n)=>e(s,{children:e(NodeList,{nodes:r.content,renderers:t})},n))});case"empty":return e(n,{});case"small":return e(n,{children:e(NodeList,{nodes:r.value.content,renderers:t})});default:throw TypeError(`Unknown node type "${r.type}"`)}}export const NodeList=n(function({nodes:t,renderers:n}){return e(r,{children:t.map((r,t)=>e(Node,{node:r,renderers:n},t))})});export default function d({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,14 +1,10 @@
1
1
  {
2
2
  "name": "@discord/markdown-react",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "React library for parsing and rendering Discord Markdown",
6
6
  "main": "dist/src/index.js",
7
7
  "types": "src/index.d.ts",
8
- "scripts": {
9
- "test": "vitest run",
10
- "build": "swc src/index.jsx -d dist"
11
- },
12
8
  "keywords": [],
13
9
  "author": "Will Nelson",
14
10
  "license": "MIT",
@@ -17,11 +13,10 @@
17
13
  "src/index.d.ts"
18
14
  ],
19
15
  "dependencies": {
20
- "@discord/markdown-wasm": "workspace:*",
21
- "react": "^18.3.1"
16
+ "react": "^18.3.1",
17
+ "@discord/markdown-wasm": "0.1.3"
22
18
  },
23
19
  "devDependencies": {
24
- "@discord/markdown-types": "workspace:*",
25
20
  "@swc/cli": "^0.5.2",
26
21
  "@swc/core": "^1.10.1",
27
22
  "@testing-library/jest-dom": "^6.4.8",
@@ -30,6 +25,11 @@
30
25
  "jsdom": "^24.1.1",
31
26
  "vite": "^5.4.0",
32
27
  "vite-plugin-wasm": "^3.3.0",
33
- "vitest": "^2.0.3"
28
+ "vitest": "^2.0.3",
29
+ "@discord/markdown-types": "0.1.2"
30
+ },
31
+ "scripts": {
32
+ "test": "vitest run",
33
+ "build": "swc src/index.jsx -d dist"
34
34
  }
35
- }
35
+ }
package/src/index.d.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  Mention,
9
9
  Link,
10
10
  List,
11
+ Block,
11
12
  } from "@discord/markdown-types";
12
13
 
13
14
  type NodeRenderProps<P = {}> = React.PropsWithChildren<P>;
@@ -111,6 +112,7 @@ export type MarkdownProps = {
111
112
  export const INLINE_RULES: string[];
112
113
  export const BLOCK_RULES: string[];
113
114
 
115
+ export function useAst(): Block[];
114
116
  export function Node(props: NodeProps): React.ReactNode;
115
117
  export function NodeList(props: NodeListProps): React.ReactNode;
116
118
  export default function Markdown(props: MarkdownProps): React.ReactNode;