import p from"node:path";import _ from"is-glob";import{slash as $}from"../../../../utils/path/slash.js";import{reporter as d}from"../../../tools/notifiers/reporter.js";import{convertGlobToRegex as h}from"../../../utils/index.js";import{formatMarkdocError as k}from"../errors.js";import{isPartial as E}from"../is-partial.js";const b=/\{\% ?raw-partial file="([^"]+)" ?\/?\%\}/gm,y=async(t,a,i=[],o,l=[a])=>{for(const s of i){const r=_(s)?h(s):new RegExp(`^${s}/`);await o.fs.scan(r)}const n=[],f=t.replace(b,(s,r)=>(n.push(u(s,r)),"_$redocly$"+(n.length-1)+"$")),w=await Promise.all(n);return f.replace(/_\$redocly\$(\d+)\$/g,(s,r)=>w[+r]);async function u(s,r){const e=r.startsWith("/")?r.slice(1):$(p.relative(o.fs.cwd,p.resolve(o.fs.cwd,p.dirname(a),r)));if(l.includes(e)){const c=[...l,e];return await d.panicOnBuildContentError(`Circular reference chain detected when processing raw partials: ${c.map(()=>"%rp").join(" -> ")}`,...c),""}const m=!o.fs.exists(e),g=!e.endsWith(".md")||await o.isPathIgnored(e)||!E(e,i);if(m||g){const R={type:"resolve_link",lines:[1,t.split(`
`).length],meta:{rawLink:r,link:e},error:{id:"error",level:"error",message:m?"Raw partial file does not exist":"Could not resolve raw partial. Starting with 0.73.0 version all partials should be placed in partials folders"}};return d.reportMarkdocProblem(k(R,a,t)),""}const v=await o.fs.read(e);return await y(v,e,i,o,[...l,e])}};export{y as resolveRawPartials};
