import g from"path";import{REDOCLY_TEAMS_RBAC as D}from"@redocly/config";import{MARKDOC_PARTIALS_DATA_KEY as R}from"../../store.js";import{reporter as F}from"../../utils/index.js";import{extractMdFirstHeading as y,getAst as b}from"./compiler.js";import{getTemplatePath as u}from"./get-template-path.js";import{prepareMarkdocPartials as A}from"./markdoc/partials.js";import{searchResolver as C}from"./search/search-resolver.js";import{markdownStaticDataLoader as T}from"./markdown-static-data-loader.js";import{makeErrorRoute as E}from"../error-route.js";import{validateRbacConfig as I}from"../validate-rbac-config.js";import{findFrontmatterSlugs as M,getSidebarSharedDataId as _,resolveFrontmatterSlugs as G}from"../utils.js";import{isPartial as L}from"./is-partial.js";import{markdownFrontmatterLoader as O}from"./markdown-frontmatter-loader.js";import{resolveRawPartials as j}from"./markdoc/resolve-raw-partials.js";import{getAiDocumentsStore as B}from"./search/get-ai-search-documents.js";import{registerPageProps as K}from"../register-page-props.js";async function at(w){return{id:"markdoc",requiredEntitlements:["markdown"],loaders:{"markdown-frontmatter":O,"markdown-ast":async(e,r)=>{const m=await r.fs.read(e),n=await r.getConfig(),d=await j(m,e,n?.markdown?.partialsFolders,r);return b(e,d)}},processContent:async(e,r)=>{const{markdown:m}=await r.getConfig(),n=m?.partialsFolders??[],d=e.createTemplate("markdown","@redocly/theme/core/templates/Markdown"),h=e.registerServerPropsGetter("markdown",u("./get-server-props.js"));e.createTemplate("error",u("../../../client/app/Error/ErrorDetails.js"));for(const o of await r.fs.scan(/\.md$/))if(!await r.isPathIgnored(o.relativePath)&&!L(o.relativePath,n))try{await k(o)}catch(a){e.addRoute(E(o.relativePath,a)),F.panicOnBuild("Failed to create route for markdown file: %s",a.message)}async function k(o){const{relativePath:a,realRelativePath:P}=o,{data:{frontmatter:t}}=await r.cache.load(a,"markdown-frontmatter"),S=t?.metadata||{},v=await M(a,"markdown-frontmatter",t,r),f=G(v,a);let l=null;t?.sidebar&&(l=await _(t.sidebar,a,r.fs));let c=d;if(t?.template){const p=t.template.startsWith("./")||t.template.startsWith("../");let i=t.template;if(p){const s=g.posix.dirname(P);i=g.resolve(w.contentDir,s,t.template)}c=e.createTemplate(t.template,i)}t?.rbac&&I({content:{[a]:t.rbac}}),await K(o,r.fs,e),(f.length?f.reverse():[void 0]).forEach(p=>{e.addRoute({slug:p,fsPath:a,templateId:c,sharedData:l?[{id:l,key:"sidebar"}]:void 0,redirectFrom:Object.entries(t.redirects||{}).map(([i,{type:s}])=>({type:s||301,from:i})),[D]:t?.rbac,getNavText:async()=>(t?.seo?.title||await y(a,r)||"").toString(),metadata:{type:"markdown",...S},async getStaticData(i,s){return T(o,i,r,t,s)},getSearchDocuments:C(t,a,e.getSearchFacets,e.setSearchFacets),getAiDocumentsStore:B(e.getSearchFacets,t),serverPropsGetterIds:[h]})})}},afterRoutesCreated:async(e,r)=>{const m=await A(r,e);e.setGlobalConfig({[R]:m})}}}export{at as markdownPlugin};
