improve: format messages with many links faster (#152112)

This commit is contained in:
Peter Steinberger 2026-09-18 13:32:48 -07:00 • committed by GitHub
parent 554db19854
commit 69448135ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View file

@ -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**",

View file

@ -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);