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