import m,{useMemo as h}from"react";import{parse as p,transform as u,renderers as b,Tag as g,nodes as d}from"@markdoc/markdoc";import{CodeBlock as k}from"@redocly/theme/components/CodeBlock/CodeBlock";import{tags as C,components as y}from"@redocly/theme/markdoc/default";const N=Object.fromEntries(Object.entries(y).filter(([,t])=>typeof t=="function")),T=t=>/\{%\s*(?:\/?\w+(?:-\w+)*|\/)(?:\s+[^%]+)?\s*(?:\/%|%\})/.test(t),w=(t,e)=>{try{const r=u(t,e);return Array.isArray(r)?r:[r]}catch(r){console.error("Error transforming node:",r)}return[]},l=(t,e,r,n)=>new g("CodeBlock",{...t,header:{title:n,controls:{copy:{}}},source:e,lang:r},[]),A=(t,e)=>{const r=t.transformAttributes(e),n=r["data-language"]||"markdoc",c=r["data-title"],o=t.attributes.content||"",a=l(r,o,n,c),s=[];if(!t.children||t.children.length<=1)return{codeNode:a,contentNodes:s};for(const i of t.children){if(!i)continue;const f=w(i,e);s.push(...f)}return{codeNode:a,contentNodes:s}},F=(t,e)=>{const r=t.transformAttributes(e),n=r["data-language"],c=r["data-title"],o=t.attributes.content||"";if(!T(o)||!t.children||t.children.length===0)return l(r,o,n,c);const a=t.children[0];if(!a||a.type!=="tag")return l(r,o,n,c);const{codeNode:s,contentNodes:i}=A(t,e);return new g("Fragment",{},[s,...i])},x=t=>h(()=>{if(!t)return null;const r=p(t),n=u(r,{tags:C,nodes:{document:{...d.document,render:"div"},fence:{...d.fence,attributes:{...d.fence.attributes,label:{type:String,render:"data-label"},title:{type:String,render:"data-title"},highlight:{type:String,render:"data-highlight"}},transform:F}}});return b.react(n,m,{components:{...N,CodeBlock:k,Fragment:m.Fragment}})},[t]);export{T as containsMarkdocTags,x as useMarkdownText};
