import{getNodeAttribute as n,getVariable as o}from"../../../../shared/markdoc.js";import{isConditionalNode as v,isVariable as w,isNode as N,isPrimitive as S,isTag as T}from"../../../../shared/utils.js";function c(t,r={}){const a=[];for(const e of t){if(S(e)){const i=r.isTrim?h(String(e)):String(e);i&&a.push(i)}else if(N(e))switch(e.type){case"text":a.push(c([n(e,"content")],r));break;case"code":a.push(`\`${c([n(e,"content")],r)}\``);break;case"blockquote":a.push(`> ${c(e.children,r)}
`);break;case"fence":const i=n(e,"title"),l=n(e,"language");a.push(`\`\`\`${l||""}${i?` ${i}`:""}
${c(e.children,r).trimEnd()}
\`\`\`
`);break;case"list":const d=n(e,"ordered")||!1,u=n(e,"marker"),b=e.children.map((s,j)=>`${" ".repeat((r?.indent??0)*2)}${d?j+1:""}${u} ${c([s],{...r??{},indent:(r?.indent??0)+1})}`);a.push(`${b.join("")}
`);break;case"item":a.push(e.children.map(s=>`${c([s],r).trimEnd()}
`).join(""));break;case"thead":const k=e.children.map(s=>c([s],r));a.push(`${k}| ${" --- |".repeat(e.children?.[0]?.children.length)}
`);break;case"tr":const $=e.children.map(s=>c([s],r));a.push(`| ${$.join(" | ")} |
`);break;case"em":a.push(`*${c(e.children,r)}*`);break;case"strong":a.push(`**${c(e.children,r)}**`);break;case"softbreak":case"hardbreak":a.push(`
`);break;case"hr":a.push(`

---

`);break;case"heading":const m=n(e,"level")??0;a.push(`${"#".repeat(m)} ${c(e.children,r)}
`);break;case"image":const f=n(e,"alt")||"",p=n(e,"src")||"";a.push(`![${f}](${p})`);break;case"link":const g=n(e,"href")||"";a.push(`[${c(e.children,r)}](${g})`);break;case"paragraph":case"table":a.push(`${c(e.children,r)}
`);break;case"tag":if(v(e)&&r.skipConditionals)continue;a.push(`${c(e.children,r)}`);break;default:a.push(c(e.children,r));break}else T(e)&&a.push(c(e.children,r));w(e)&&a.push(c([o(e,r.variables)],r))}return r.isTrim?h(a.map(e=>h(e)).join("")):a.join("")}function h(t){return t.replace(/^[ \t\r\f]+|[ \t\r\f]+$/g,"")}export{c as toMarkdown};
