UNPKG

@md2docx/react-markdown

Version:

A modern, SSR-friendly React Markdown renderer that preserves the MDAST tree for reuse (e.g., mdast2docx), supports full JSX children, unified plugins, and component overrides.

2 lines (1 loc) 1.09 kB
import P from"remark-parse";import y from"remark-rehype";import{unified as g}from"unified";import{toJsxRuntime as x}from"hast-util-to-jsx-runtime";import{Fragment as d,jsx as A,jsxs as E}from"react/jsx-runtime";import{visit as h}from"unist-util-visit";var w=e=>Object.fromEntries(Object.entries(e).map(([t,s])=>[t.startsWith("data")?t.replace(/[A-Z]+(?![a-z])|[A-Z]/g,p=>"-"+p.toLowerCase()):t.startsWith("aria")?t.replace("aria","aria-").toLowerCase():t,s])),C=()=>crypto.randomUUID();var J=({children:e,remarkPlugins:t=[],rehypePlugins:s=[],remarkRehypeOptions:p,astRef:r,components:f,skipHtml:m})=>{let l=g().use(P).use(t).use(y,{...p,allowDangerousHtml:!m}).use(s),c=l.parse(e!=null?e:""),n=l.runSync(c);return r&&(r.current||(r.current=[]),r.current.push({mdast:c,hast:n})),h(n,(o,i,a)=>{var u;o.type==="raw"&&a&&typeof i=="number"&&(m?a.children.splice(i,1):a.children[i]={type:"text",value:o.value}),o.type==="element"&&((u=o.properties).src||(u.src=null))}),x(n,{Fragment:d,components:f,ignoreInvalidStyle:!0,jsx:A,jsxs:E,passKeys:!0,passNode:!0})};export{w as a,C as b,J as c};