openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
96 lines (95 loc) • 1.81 kB
JavaScript
import { c as scanFenceSpans } from "./code-regions-CfNY0afj.mjs";
//#region packages/markdown-core/src/code-spans.ts
/** Creates the carry-forward state used when scanning inline code across chunks. */
function createInlineCodeState() {
return {
open: false,
ticks: 0
};
}
/** Builds a lookup for fenced and inline code spans while preserving scanner state. */
function buildCodeSpanIndex(e, t, n) {
const { spans: r, state: i } = scanFenceSpans(e, n);
const a = t ?? createInlineCodeState();
const o = [];
let s = a.open;
let c = a.ticks;
let l = s ? 0 : -1;
let u = !s && a.escaped === true;
let d = 0;
let f = 0;
while (d < e.length) {
const t = r[f];
if (t && d >= t.start) {
d = t.end;
f += 1;
u = false;
continue;
}
if (u) {
u = false;
if (e[d] === "`" || e[d] === "\\") {
d += 1;
continue;
}
}
if (e[d] === "\\" && !s) {
u = true;
d += 1;
continue;
}
if (e[d] !== "`") {
d += 1;
continue;
}
const n = d;
let i = 0;
while (d < e.length && e[d] === "`") {
i += 1;
d += 1;
}
if (!s) {
s = true;
c = i;
l = n;
continue;
}
if (i === c) {
o.push({
start: l,
end: d
});
s = false;
c = 0;
l = -1;
}
}
if (s) o.push({
start: l,
end: e.length
});
return {
inlineState: {
open: s,
ticks: c,
...u ? { escaped: u } : {}
},
fenceState: i,
isInside: (e) => isInsideSpan(e, r) || isInsideSpan(e, o)
};
}
function isInsideSpan(e, t) {
let n = 0;
let r = t.length;
while (n < r) {
const i = Math.floor((n + r) / 2);
const a = t[i];
if (!a) return false;
if (e >= a.end) n = i + 1;
else r = i;
}
const i = t[n];
return i !== void 0 && e >= i.start && e < i.end;
}
//#endregion
export { createInlineCodeState as n, buildCodeSpanIndex as t };