import y from"@markdoc/markdoc";import{formatMarkdocError as P}from"./errors.js";import{logger as E,reporter as S,sha1 as F}from"../../utils/index.js";import{extractFirstHeading as O,visit as z}from"./markdoc/plugins/utils.js";import{attributeResolvers as p}from"./attribute-resolvers/index.js";import{MdResolveError as A}from"./attribute-resolvers/md-resolve-error.js";import{processHtmlTokens as H}from"./html/process-html-tokens.js";import{resolveRawPartials as $}from"./markdoc/resolve-raw-partials.js";import{GATED_MARKDOC_TAGS as I}from"../../entitlements/constants.js";import{EntitlementsProvider as j}from"../../entitlements/entitlements-provider.js";async function G(t,s,r){const o=j.instance(),{actions:d}=r,v=new Set,w=new Set,c=new Set,l={},i=new Set,{relativePath:m,resolveErrors:f}=t,u=[];return t.resolveErrors.length=0,z(t.ast,a=>{try{const e=T();u.push(e.then(n=>{n?.sharedDataIds&&n.sharedDataIds.forEach(g=>v.add(g))}).catch(D))}catch(e){D(e)}async function T(){const e=[];switch(a.type){case"link":case"image":await p.nativeMdLink(a,a.type==="link"?"href":"src",m,r);break;case"tag":if(!a.tag)break;const n=s?.tags?.[a.tag];if(!n){I[a.tag]&&!o.canAccessFeature(I[a.tag])&&(a.errors.push({level:"warning",id:"invalid-tag",message:`Undefined tag: ${a.tag}`}),a.tag="",a.type="error",a.attributes={});break}const g=a.tag==="html"?a.attributes.name:a.tag;if((a.tag==="html"?i:c).add(g),l[g]=(l[g]||0)+1,n.attributes?.__idx&&(a.attributes.__idx=l[a.tag]),n.dynamicComponentLib&&w.add(n.dynamicComponentLib),!n.attributes)break;for(const[h,k]of Object.entries(n.attributes)){const b=k.type;if(b&&(b.resolver||k.resolver)){const _=p[b.resolver||k.resolver],M=await _?.(a,h,m,r);M?.sharedDataId&&e.push(M.sharedDataId)}}const R=a.attributes?.attrs;if(R&&a.tag==="html")for(const h of Object.keys(R))(h==="src"||h==="srcSet")&&await p.htmlSourceAttribute(a,h,m,r);break}return{sharedDataIds:e}}function D(e){if(!(e instanceof A)){S.panicOnBuild(e.message);return}f.push(P({type:"resolve_link",meta:{...e.meta,link:e.meta.link??e.meta.rawLink},lines:a.lines,location:a.location,error:{id:"",level:"error",message:e.message,location:a.location}},t.relativePath,t.rawContent))}}),await Promise.all(u),t.sharedDataDeps=v,t.dynamicMarkdocComponents=w,t.validatedAtRevision<d.buildRevision&&(t.markdocErrors=y.validate(t.ast,s).map(a=>P(a,m,t.rawContent)),t.validatedAtRevision=d.buildRevision),{sharedDataDeps:t.sharedDataDeps,dynamicMarkdocComponents:Array.from(t.dynamicMarkdocComponents?.values()??[]),tagList:Array.from(c),htmlTagsList:Array.from(i),tagOccurrence:l}}const L=new y.Tokenizer({html:!0,allowIndentation:!0,allowComments:!0});L.parser.block.ruler.getRules("reference").length=0;function x(t,s){const r=L.tokenize(s),o=H(r);return{ast:y.parse(o,{file:t,slots:!0}),rawContent:s,resolveErrors:[],markdocErrors:[],relativePath:t,sharedDataDeps:void 0,validatedAtRevision:-1}}async function Z(t,s){const{data:r}=await s.cache.load(t,"markdown-ast");return O(r.ast)}async function C(t,s){S.clearMarkdocProblems();const r=t.getAllRoutes();let o=0;for(const d of r)await t.resolveRouteStaticData(d,s,!0),o++,E.isInteractive()&&o%100===0&&E.logSticky("validate",`  \u{1F50D} Status: validating markdoc (${o}/${r.length})`)}async function tt(t,s,r){const o=t.content?F(t.content):"";return await r.context.cache.load(t.cacheId||t.relativePath,{loader:v,name:"markdown-inline-parser"},[o,String(r.actions.buildRevision)]);async function v(w,c,l){let i;if(t.content){const f=t.content?t.content:await c.fs.read(t.relativePath),u=await c.getConfig(),a=await $(f,t.relativePath,u.markdown?.partialsFolders,c);i=x(t.relativePath,a)}else i=(await c.cache.load(t.relativePath,"markdown-ast")).data;let m={};if(r.actions.buildRevision!==0&&(m=await G(i,s,r)),!t.isVirtual){for(const f of i.markdocErrors)l(f);for(const f of i.resolveErrors)l(f)}return{ast:i.ast,info:m}}}export{Z as extractMdFirstHeading,x as getAst,tt as parseAndResolveMarkdoc,G as resolveAndValidateMarkdoc,C as validateAllMarkdowns};
