UNPKG

@m2d/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) 2.97 kB
"use strict";var C=Object.create;var P=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var S=(r,e)=>{for(var o in e)P(r,o,{get:e[o],enumerable:!0})},w=(r,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of L(e))!H.call(r,t)&&t!==o&&P(r,t,{get:()=>e[t],enumerable:!(s=W(e,t))||s.enumerable});return r};var R=(r,e,o)=>(o=r!=null?C(T(r)):{},w(e||!r||!r.__esModule?P(o,"default",{value:r,enumerable:!0}):o,r)),U=r=>w(P({},"__esModule",{value:!0}),r);var D={};S(D,{Md:()=>j,Mdx:()=>J,Omit:()=>F,Unwrap:()=>X});module.exports=U(D);var b=R(require("remark-parse")),h=R(require("remark-rehype")),v=require("unified"),A=require("hast-util-to-jsx-runtime"),c=require("react/jsx-runtime"),I=require("unist-util-visit");var O=()=>crypto.randomUUID();var d=({children:r,remarkPlugins:e=[],rehypePlugins:o=[],remarkRehypeOptions:s,astRef:t,components:l,skipHtml:a})=>{let m=(0,v.unified)().use(b.default).use(e).use(h.default,{...s,allowDangerousHtml:!a}).use(o),n=m.parse(r!=null?r:""),p=m.runSync(n);return t&&(t.current||(t.current=[]),t.current.push({mdast:n,hast:p})),(0,I.visit)(p,(u,g,x)=>{var k;u.type==="raw"&&x&&typeof g=="number"&&(a?x.children.splice(g,1):x.children[g]={type:"text",value:u.value}),u.type==="element"&&((k=u.properties).src||(k.src=null))}),(0,A.toJsxRuntime)(p,{Fragment:c.Fragment,components:l,ignoreInvalidStyle:!0,jsx:c.jsx,jsxs:c.jsxs,passKeys:!0,passNode:!0})};var f=require("react"),y=({children:r,markdownProps:e})=>{if(typeof r=="string")return(0,c.jsx)(d,{...e,children:r});if(Array.isArray(r)){let o=O();return r.map((s,t)=>(0,c.jsx)(y,{markdownProps:e,children:s},o+t))}if((0,f.isValidElement)(r)){let o=r.type,s=r.props;return(0,c.jsx)(o,{...s,children:(0,c.jsx)(y,{markdownProps:e,children:s.children})})}return r},J=({children:r,wrapper:e,remarkPlugins:o,rehypePlugins:s,remarkRehypeOptions:t,astRef:l,components:a,skipHtml:m,...n})=>{let p=e!=null?e:Object.keys(n).length?"div":f.Fragment;return(0,c.jsx)(p,{...n,children:(0,c.jsx)(y,{markdownProps:{remarkPlugins:o,rehypePlugins:s,remarkRehypeOptions:t,astRef:l,components:a,skipHtml:m},children:r})})};var F=({noWarning:r,node:{tagName:e}})=>(r||console.warn(`The <${e}> element has been omitted along with its content. Use 'noWarning' to suppress this message.`),null);var X=({children:r,noWarning:e,node:{tagName:o}})=>(e||console.warn(`The <${o}> element has been unwrapped. Only its children will be rendered; all attributes are ignored. Use 'noWarning' to suppress this message.`),r);var j=({children:r,wrapper:e,remarkPlugins:o,rehypePlugins:s,remarkRehypeOptions:t,astRef:l,components:a,skipHtml:m,...n})=>{let p=e!=null?e:Object.keys(n).length?"div":f.Fragment;return(0,c.jsx)(p,{...n,children:(0,c.jsx)(d,{remarkPlugins:o,rehypePlugins:s,remarkRehypeOptions:t,astRef:l,components:a,skipHtml:m,children:r})})};0&&(module.exports={Md,Mdx,Omit,Unwrap});