import{parse,Ast}from"@markdoc/markdoc";import{isString}from"@redocly/theme";import{getTagOrGroup}from"../../models";import{safeSlugify,unescapeHTMLChars}from"../../utils";import{simplifyAstStructure}from"../../utils/simplifyAstStructure";export function addMarkdownItems(t,e,i){const r=isString(t)?simplifyAstStructure(parse(t)):Ast.fromJSON(JSON.stringify(null==t?void 0:t.result)),o=[];let n;const s=(t,e,i=o,r,n)=>{let s;t=unescapeHTMLChars(t),r.attributes.id?s=r.attributes.id:(s=n?`${n}/${safeSlugify(t)}`:`section/${safeSlugify(t)}`,r.attributes.id=s);const l={id:s.toLowerCase(),name:t,level:e,items:[],ast:[r]};return i.push(l),l},l=(t,e)=>{var i,r;null===(r=null===(i=t[t.length-1])||void 0===i?void 0:i.ast)||void 0===r||r.push(e)},u=t=>{var i,r;const u=getMarkdownContent(t)||"",a=e&&"tag"===e.type?e.id:void 0;"heading"===t.type&&1===t.attributes.level?n=s(u,t.attributes.level,void 0,t,a):t.attributes.level?s(u,t.attributes.level,null==n?void 0:n.items,t,null!==(i=null==n?void 0:n.id)&&void 0!==i?i:a):(null===(r=null==n?void 0:n.items)||void 0===r?void 0:r.length)?l(null==n?void 0:n.items,t):(null==o?void 0:o.length)&&l(o,t)};if(Array.isArray(r))for(const t of r)u(t);else u(r);return mapHeadingsDeep(e,o,i)}function mapHeadingsDeep(t,e,i=1){return e.map((e=>{const r=getTagOrGroup("section",e,t);return r.depth=i,e.items&&(r.items=mapHeadingsDeep(r,e.items,i+1)),r}))}function getMarkdownContent(t){var e,i;return"string"==typeof(null===(e=null==t?void 0:t.attributes)||void 0===e?void 0:e.content)?null===(i=t.attributes)||void 0===i?void 0:i.content:(null==t?void 0:t.children)?getMarkdownContent(t.children[0]):void 0}
//# sourceMappingURL=markdown.js.map