mirror of
https://github.com/openclaw/openclaw.git
synced 2026-10-03 01:29:56 +00:00
improve: format messages with many links faster (#152112)
This commit is contained in:
parent
554db19854
commit
69448135ae
2 changed files with 25 additions and 6 deletions
|
|
@ -86,6 +86,20 @@ describe("markdownToSignalText", () => {
|
|||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "nested styles across multiple expanded links and emoji",
|
||||
markdown: "**[😀](https://a.test) _[two](https://b.test) tail_**",
|
||||
expected: {
|
||||
text: "😀 (https://a.test) two (https://b.test) tail",
|
||||
styles: [
|
||||
{ start: 0, length: 2, style: "BOLD" },
|
||||
{ start: 19, length: 4, style: "BOLD" },
|
||||
{ start: 20, length: 3, style: "ITALIC" },
|
||||
{ start: 40, length: 5, style: "BOLD" },
|
||||
{ start: 40, length: 5, style: "ITALIC" },
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nested style around an expanded link",
|
||||
markdown: "**[docs](https://example.com) _nested_ tail**",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import type { FormatCapabilityProfile, FormatConstruct } from "./format-capabilities.js";
|
||||
import { copyHtmlTags } from "./ir-metadata.js";
|
||||
import { sliceMarkdownIRRanges } from "./ir-slice.js";
|
||||
import {
|
||||
createStyleSpan,
|
||||
mergeAnnotationSpans,
|
||||
|
|
@ -8,7 +9,7 @@ import {
|
|||
type MarkdownStyle,
|
||||
type MarkdownStyleSpan,
|
||||
} from "./ir-spans.js";
|
||||
import { appendMarkdownIR, sliceMarkdownIR, type MarkdownIR } from "./ir.js";
|
||||
import { appendMarkdownIR, type MarkdownIR } from "./ir.js";
|
||||
|
||||
type TextEdit = { start: number; end: number; text: string };
|
||||
|
||||
|
|
@ -119,14 +120,18 @@ function applyTextEdits(ir: MarkdownIR, edits: TextEdit[]): MarkdownIR {
|
|||
links: ir.links,
|
||||
annotations: ir.annotations,
|
||||
});
|
||||
const result: MarkdownIR = { text: "", styles: [], links: [] };
|
||||
let cursor = 0;
|
||||
for (const edit of ordered) {
|
||||
appendMarkdownIR(result, sliceMarkdownIR(content, cursor, edit.start));
|
||||
result.text += edit.text;
|
||||
const ranges = ordered.map((edit) => {
|
||||
const range = { start: cursor, end: edit.start };
|
||||
cursor = edit.end;
|
||||
return range;
|
||||
});
|
||||
ranges.push({ start: cursor, end: ir.text.length });
|
||||
const result: MarkdownIR = { text: "", styles: [], links: [] };
|
||||
for (const [index, slice] of sliceMarkdownIRRanges(content, ranges).entries()) {
|
||||
appendMarkdownIR(result, slice);
|
||||
result.text += ordered[index]?.text ?? "";
|
||||
}
|
||||
appendMarkdownIR(result, sliceMarkdownIR(content, cursor, ir.text.length));
|
||||
result.styles = mergeStyleSpans(result.styles);
|
||||
if (result.annotations) {
|
||||
result.annotations = mergeAnnotationSpans(result.annotations);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue