openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 1.66 kB
JavaScript
import"./src-0ci69Prg.mjs";import{t as expectDefined}from"./expect-DkJCbmLj.mjs";import{t as findMarkdownCodeRegions}from"./markdown-code-regions-DW0eMqgb.mjs";
//!src/shared/text/code-ranges.ts
function isInsideCode(e,t){return t.some(t=>e>=t.start&&e<t.end)}function isInsideSettledBlockCode(e,t,n){return n.some(n=>n.block&&n.start<=e&&e<n.end&&/[\r\n]/.test(t.slice(n.start,e)))}function stripLinesOutsideCode(e,t){return projectLinesOutsideCode(e,t,0).text}function projectLinesOutsideCode(e,t,n){let r;let i=n;return{text:e.replace(/[^\n]*(?:\n|$)/g,(a,o)=>{if(!t(a.endsWith(`
`)?a.slice(0,-1).replace(/\r$/,``):a)||isInsideCode(o,r??=findMarkdownCodeRegions(e)))return a;if(o<n)i-=a.length;return``}),retainStart:n,retainedOutputStart:i}}function indexTextParts(e){const t=[];const n=[];let r=0;e.forEach((e,i)=>{if(!e)return;if(t.length)r+=1;const a=r;n.push(e);r+=e.length;t.push({index:i,start:a,end:r})});return{text:n.join(`
`),spans:t}}function createTextPartCodeRegionResolver(e){const t=indexTextParts(e);const n=new Map(t.spans.map(e=>[e.index,e]));let r;return e=>{const i=n.get(e);if(!i)return[];r??=findMarkdownCodeRegions(t.text);let a=0;let o=r.length;while(a<o){const e=a+o>>>1;if(expectDefined(r[e],`indexed code region`).end<=i.start)a=e+1;else o=e}const s=[];for(let e=a;e<r.length;e++){const t=expectDefined(r[e],`projected code region`);if(t.start>=i.end)break;s.push({start:Math.max(0,t.start-i.start),end:Math.min(i.end,t.end)-i.start,block:t.block})}return s}}export{projectLinesOutsideCode as a,isInsideSettledBlockCode as i,indexTextParts as n,stripLinesOutsideCode as o,isInsideCode as r,createTextPartCodeRegionResolver as t};