mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 10:07:41 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -35,15 +35,21 @@ const STYLE_RANK = new Map<MarkdownStyle, number>(
|
||||
|
||||
function sortStyleSpans(spans: MarkdownStyleSpan[]): MarkdownStyleSpan[] {
|
||||
return [...spans].toSorted((a, b) => {
|
||||
if (a.start !== b.start) return a.start - b.start;
|
||||
if (a.end !== b.end) return b.end - a.end;
|
||||
if (a.start !== b.start) {
|
||||
return a.start - b.start;
|
||||
}
|
||||
if (a.end !== b.end) {
|
||||
return b.end - a.end;
|
||||
}
|
||||
return (STYLE_RANK.get(a.style) ?? 0) - (STYLE_RANK.get(b.style) ?? 0);
|
||||
});
|
||||
}
|
||||
|
||||
export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions): string {
|
||||
const text = ir.text ?? "";
|
||||
if (!text) return "";
|
||||
if (!text) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const styleMarkers = options.styleMarkers;
|
||||
const styled = sortStyleSpans(ir.styles.filter((span) => Boolean(styleMarkers[span.style])));
|
||||
@@ -54,16 +60,23 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
|
||||
|
||||
const startsAt = new Map<number, MarkdownStyleSpan[]>();
|
||||
for (const span of styled) {
|
||||
if (span.start === span.end) continue;
|
||||
if (span.start === span.end) {
|
||||
continue;
|
||||
}
|
||||
boundaries.add(span.start);
|
||||
boundaries.add(span.end);
|
||||
const bucket = startsAt.get(span.start);
|
||||
if (bucket) bucket.push(span);
|
||||
else startsAt.set(span.start, [span]);
|
||||
if (bucket) {
|
||||
bucket.push(span);
|
||||
} else {
|
||||
startsAt.set(span.start, [span]);
|
||||
}
|
||||
}
|
||||
for (const spans of startsAt.values()) {
|
||||
spans.sort((a, b) => {
|
||||
if (a.end !== b.end) return b.end - a.end;
|
||||
if (a.end !== b.end) {
|
||||
return b.end - a.end;
|
||||
}
|
||||
return (STYLE_RANK.get(a.style) ?? 0) - (STYLE_RANK.get(b.style) ?? 0);
|
||||
});
|
||||
}
|
||||
@@ -71,14 +84,21 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
|
||||
const linkStarts = new Map<number, RenderLink[]>();
|
||||
if (options.buildLink) {
|
||||
for (const link of ir.links) {
|
||||
if (link.start === link.end) continue;
|
||||
if (link.start === link.end) {
|
||||
continue;
|
||||
}
|
||||
const rendered = options.buildLink(link, text);
|
||||
if (!rendered) continue;
|
||||
if (!rendered) {
|
||||
continue;
|
||||
}
|
||||
boundaries.add(rendered.start);
|
||||
boundaries.add(rendered.end);
|
||||
const openBucket = linkStarts.get(rendered.start);
|
||||
if (openBucket) openBucket.push(rendered);
|
||||
else linkStarts.set(rendered.start, [rendered]);
|
||||
if (openBucket) {
|
||||
openBucket.push(rendered);
|
||||
} else {
|
||||
linkStarts.set(rendered.start, [rendered]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +123,9 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
|
||||
// Close ALL elements (styles and links) in LIFO order at this position
|
||||
while (stack.length && stack[stack.length - 1]?.end === pos) {
|
||||
const item = stack.pop();
|
||||
if (item) out += item.close;
|
||||
if (item) {
|
||||
out += item.close;
|
||||
}
|
||||
}
|
||||
|
||||
const openingItems: OpeningItem[] = [];
|
||||
@@ -125,7 +147,9 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
|
||||
if (openingStyles) {
|
||||
for (const [index, span] of openingStyles.entries()) {
|
||||
const marker = styleMarkers[span.style];
|
||||
if (!marker) continue;
|
||||
if (!marker) {
|
||||
continue;
|
||||
}
|
||||
openingItems.push({
|
||||
end: span.end,
|
||||
open: marker.open,
|
||||
@@ -139,8 +163,12 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
|
||||
|
||||
if (openingItems.length > 0) {
|
||||
openingItems.sort((a, b) => {
|
||||
if (a.end !== b.end) return b.end - a.end;
|
||||
if (a.kind !== b.kind) return a.kind === "link" ? -1 : 1;
|
||||
if (a.end !== b.end) {
|
||||
return b.end - a.end;
|
||||
}
|
||||
if (a.kind !== b.kind) {
|
||||
return a.kind === "link" ? -1 : 1;
|
||||
}
|
||||
if (a.kind === "style" && b.kind === "style") {
|
||||
return (STYLE_RANK.get(a.style) ?? 0) - (STYLE_RANK.get(b.style) ?? 0);
|
||||
}
|
||||
@@ -155,7 +183,9 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
|
||||
}
|
||||
|
||||
const next = points[i + 1];
|
||||
if (next === undefined) break;
|
||||
if (next === undefined) {
|
||||
break;
|
||||
}
|
||||
if (next > pos) {
|
||||
out += options.escapeText(text.slice(pos, next));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user