Skip to content

Commit 29cc0bd

Browse files
authored
refactor(markdown): share span clipping logic (#134910)
1 parent 2e3f734 commit 29cc0bd

1 file changed

Lines changed: 26 additions & 77 deletions

File tree

‎packages/markdown-core/src/ir-spans.ts‎

Lines changed: 26 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -81,46 +81,42 @@ export function createStyleSpan(params: MarkdownStyleSpan): MarkdownStyleSpan {
8181
return span;
8282
}
8383

84+
function clipSpans<T extends { start: number; end: number }>(
85+
spans: T[],
86+
start: number,
87+
end: number,
88+
copySpan: (span: T) => T,
89+
): T[] {
90+
const clipped: T[] = [];
91+
for (const span of spans) {
92+
const sliceStart = Math.max(span.start, start);
93+
const sliceEnd = Math.min(span.end, end);
94+
if (sliceEnd > sliceStart) {
95+
const copy = copySpan(span);
96+
copy.start = sliceStart - start;
97+
copy.end = sliceEnd - start;
98+
clipped.push(copy);
99+
}
100+
}
101+
return clipped;
102+
}
103+
84104
export function clampStyleSpans(
85105
spans: MarkdownStyleSpan[],
86106
maxLength: number,
87107
): MarkdownStyleSpan[] {
88-
const clamped: MarkdownStyleSpan[] = [];
89-
for (const span of spans) {
90-
const start = Math.max(0, Math.min(span.start, maxLength));
91-
const end = Math.max(start, Math.min(span.end, maxLength));
92-
if (end > start) {
93-
clamped.push(createStyleSpan({ start, end, style: span.style, language: span.language }));
94-
}
95-
}
96-
return clamped;
108+
return clipSpans(spans, 0, maxLength, createStyleSpan);
97109
}
98110

99111
export function clampLinkSpans(spans: MarkdownLinkSpan[], maxLength: number): MarkdownLinkSpan[] {
100-
const clamped: MarkdownLinkSpan[] = [];
101-
for (const span of spans) {
102-
const start = Math.max(0, Math.min(span.start, maxLength));
103-
const end = Math.max(start, Math.min(span.end, maxLength));
104-
if (end > start) {
105-
clamped.push(copyMarkdownLinkSpan(span, { start, end }));
106-
}
107-
}
108-
return clamped;
112+
return clipSpans(spans, 0, maxLength, copyMarkdownLinkSpan);
109113
}
110114

111115
export function clampAnnotationSpans(
112116
spans: MarkdownAnnotationSpan[],
113117
maxLength: number,
114118
): MarkdownAnnotationSpan[] {
115-
const clamped: MarkdownAnnotationSpan[] = [];
116-
for (const span of spans) {
117-
const start = Math.max(0, Math.min(span.start, maxLength));
118-
const end = Math.max(start, Math.min(span.end, maxLength));
119-
if (end > start) {
120-
clamped.push({ ...span, start, end });
121-
}
122-
}
123-
return clamped;
119+
return clipSpans(spans, 0, maxLength, (span) => ({ ...span }));
124120
}
125121

126122
export function mergeAnnotationSpans(spans: MarkdownAnnotationSpan[]): MarkdownAnnotationSpan[] {
@@ -172,73 +168,26 @@ export function mergeStyleSpans(spans: MarkdownStyleSpan[]): MarkdownStyleSpan[]
172168
return merged;
173169
}
174170

175-
function resolveSliceBounds(
176-
span: { start: number; end: number },
177-
start: number,
178-
end: number,
179-
): { start: number; end: number } | null {
180-
const sliceStart = Math.max(span.start, start);
181-
const sliceEnd = Math.min(span.end, end);
182-
return sliceEnd > sliceStart ? { start: sliceStart, end: sliceEnd } : null;
183-
}
184-
185171
export function sliceStyleSpans(
186172
spans: MarkdownStyleSpan[],
187173
start: number,
188174
end: number,
189175
): MarkdownStyleSpan[] {
190-
const sliced: MarkdownStyleSpan[] = [];
191-
for (const span of spans) {
192-
const bounds = resolveSliceBounds(span, start, end);
193-
if (bounds) {
194-
sliced.push(
195-
createStyleSpan({
196-
start: bounds.start - start,
197-
end: bounds.end - start,
198-
style: span.style,
199-
language: span.language,
200-
}),
201-
);
202-
}
203-
}
204-
return mergeStyleSpans(sliced);
176+
return mergeStyleSpans(clipSpans(spans, start, end, createStyleSpan));
205177
}
206178

207179
export function sliceLinkSpans(
208180
spans: MarkdownLinkSpan[],
209181
start: number,
210182
end: number,
211183
): MarkdownLinkSpan[] {
212-
const sliced: MarkdownLinkSpan[] = [];
213-
for (const span of spans) {
214-
const bounds = resolveSliceBounds(span, start, end);
215-
if (bounds) {
216-
sliced.push(
217-
copyMarkdownLinkSpan(span, {
218-
start: bounds.start - start,
219-
end: bounds.end - start,
220-
}),
221-
);
222-
}
223-
}
224-
return sliced;
184+
return clipSpans(spans, start, end, copyMarkdownLinkSpan);
225185
}
226186

227187
export function sliceAnnotationSpans(
228188
spans: MarkdownAnnotationSpan[],
229189
start: number,
230190
end: number,
231191
): MarkdownAnnotationSpan[] {
232-
const sliced: MarkdownAnnotationSpan[] = [];
233-
for (const span of spans) {
234-
const bounds = resolveSliceBounds(span, start, end);
235-
if (bounds) {
236-
sliced.push({
237-
...span,
238-
start: bounds.start - start,
239-
end: bounds.end - start,
240-
});
241-
}
242-
}
243-
return mergeAnnotationSpans(sliced);
192+
return mergeAnnotationSpans(clipSpans(spans, start, end, (span) => ({ ...span })));
244193
}

0 commit comments

Comments
 (0)