Skip to content

Commit af49730

Browse files
committed
fix issues with markers on the very start and single Markdown markers
1 parent 5e2f7f9 commit af49730

2 files changed

Lines changed: 102 additions & 20 deletions

File tree

‎src/cmem/markdown/truncateMarkdown.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,42 @@ describe("truncateMarkdown", () => {
199199
"A short paragraph before the code block.\n\n~~~\nsome code here\n~~~\nContinue with the\n\n...",
200200
);
201201
});
202+
203+
it("keeps a leading bold span complete instead of cutting into its markers", () => {
204+
const content = "**A very long important bold warning sentence that goes on** and normal text after it.";
205+
const result = truncateMarkdown(content, 20, "...");
206+
expect(result).toBe("**A very long important bold warning sentence that goes on**\n\n...");
207+
});
208+
209+
it("does not cut inside single marker emphasis", () => {
210+
const content = "Some intro words and *a long italic phrase here* and then more text follows here.";
211+
expect(truncateMarkdown(content, 30, "...")).toBe("Some intro words and\n\n...");
212+
expect(truncateMarkdown(content.replace(/\*/g, "_"), 30, "...")).toBe("Some intro words and\n\n...");
213+
});
214+
215+
it("does not cut inside strikethrough markup", () => {
216+
const content = "Some intro words and ~~a long deleted phrase~~ and then more text follows.";
217+
expect(truncateMarkdown(content, 30, "...")).toBe("Some intro words and\n\n...");
218+
});
219+
220+
it("does not cut inside an inline code span", () => {
221+
const content = "Some intro words and `a_long_code_span_here()` and then more text follows here.";
222+
expect(truncateMarkdown(content, 32, "...")).toBe("Some intro words and\n\n...");
223+
});
224+
225+
it("does not cut inside a reference style link", () => {
226+
const content =
227+
"Some intro words and [a label][ref] and more text after it here.\n\n[ref]: https://example.com";
228+
expect(truncateMarkdown(content, 30, "...")).toBe("Some intro words and\n\n...");
229+
});
230+
231+
it("moves the cutoff out of the outermost span of nested emphasis", () => {
232+
const content = "Some intro words and **bold with *inner* parts here** and then more text.";
233+
expect(truncateMarkdown(content, 32, "...")).toBe("Some intro words and\n\n...");
234+
});
235+
236+
it("does not treat intra-word underscores as emphasis", () => {
237+
const content = "Some intro words and snake_case_name_here plus more words to cut here somewhere.";
238+
expect(truncateMarkdown(content, 30, "...")).toBe("Some intro words and\n\n...");
239+
});
202240
});

‎src/cmem/markdown/truncateMarkdown.ts‎

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface LinkRange {
1212
end: number;
1313
}
1414

15-
interface EmphasisRange {
15+
interface InlineSpanRange {
1616
start: number;
1717
end: number;
1818
}
@@ -94,28 +94,66 @@ const moveCutOffOutsideLink = (cutOff: number, linkRanges: LinkRange[]): number
9494
return cutOff <= activeLink.descriptionEnd ? activeLink.start : activeLink.end;
9595
};
9696

97-
// Matches **bold** and __bold__ spans, keeping the marker pair atomic so a cutoff can't strand a single "*"/"_".
98-
const getEmphasisRanges = (content: string, fenceRanges: FenceRange[]): EmphasisRange[] => {
99-
const emphasisRanges: EmphasisRange[] = [];
100-
const emphasisRegex = /(\*\*|__)(?!\s)(?:(?!\1)[^\n])+?(?<!\s)\1/g;
101-
let emphasisMatch = emphasisRegex.exec(content);
97+
/**
98+
* Inline constructs that must stay atomic. Cutting inside one of them strands an opening marker
99+
* (a lonely "*", "_", "~~", backtick or "[") that is then displayed as raw Markdown syntax.
100+
*/
101+
const inlineSpanRegexes = [
102+
// **bold**, __bold__, ~~deleted~~
103+
/(\*\*|__|~~)(?!\s)(?:(?!\1)[^\n])+?(?<!\s)\1/g,
104+
// *emphasis*, _emphasis_, ignoring intra-word markers and the markers of the doubled variants above
105+
/(?<![*_\w])(\*|_)(?!\s)(?:(?!\1)[^\n])+?(?<!\s)\1(?![*_\w])/g,
106+
// `code span` and ``code span with a ` inside``
107+
/(`+)(?:(?!\1)[^\n])+?\1/g,
108+
// reference and shortcut links, including their image variants: [label][ref], [label][], [label], ![label]
109+
/!?\[[^\]\n]*\](?:\[[^\]\n]*\])?/g,
110+
];
111+
112+
const overlapsFence = (fenceRanges: FenceRange[], start: number, end: number): boolean =>
113+
fenceRanges.some((fenceRange) => start < fenceRange.end && end > fenceRange.start);
102114

103-
while (emphasisMatch !== null) {
104-
const start = emphasisMatch.index;
105-
const end = start + emphasisMatch[0].length;
106-
if (!isInsideFence(fenceRanges, start)) {
107-
emphasisRanges.push({ start, end });
115+
/**
116+
* Collects all atomic inline spans as maximal ranges, so nested constructs like `**bold with *emphasis* inside**`
117+
* are treated as one unit and a cutoff is moved out of the outermost span.
118+
*/
119+
const getInlineSpanRanges = (content: string, fenceRanges: FenceRange[]): InlineSpanRange[] => {
120+
const spanRanges: InlineSpanRange[] = [];
121+
122+
for (const spanRegex of inlineSpanRegexes) {
123+
spanRegex.lastIndex = 0;
124+
let spanMatch = spanRegex.exec(content);
125+
126+
while (spanMatch !== null) {
127+
const start = spanMatch.index;
128+
const end = start + spanMatch[0].length;
129+
if (!overlapsFence(fenceRanges, start, end)) {
130+
spanRanges.push({ start, end });
131+
}
132+
spanRegex.lastIndex = end;
133+
spanMatch = spanRegex.exec(content);
108134
}
109-
emphasisRegex.lastIndex = end;
110-
emphasisMatch = emphasisRegex.exec(content);
111135
}
112136

113-
return emphasisRanges;
137+
return spanRanges
138+
.sort((a, b) => a.start - b.start || b.end - a.end)
139+
.reduce<InlineSpanRange[]>((mergedRanges, spanRange) => {
140+
const lastRange = mergedRanges[mergedRanges.length - 1];
141+
if (lastRange && spanRange.start < lastRange.end) {
142+
lastRange.end = Math.max(lastRange.end, spanRange.end);
143+
return mergedRanges;
144+
}
145+
return [...mergedRanges, { ...spanRange }];
146+
}, []);
114147
};
115148

116-
const moveCutOffOutsideEmphasis = (cutOff: number, emphasisRanges: EmphasisRange[]): number => {
117-
const activeEmphasis = emphasisRanges.find(({ start, end }) => cutOff > start && cutOff < end);
118-
return activeEmphasis ? activeEmphasis.start : cutOff;
149+
const moveCutOffOutsideInlineSpan = (content: string, cutOff: number, spanRanges: InlineSpanRange[]): number => {
150+
const activeSpan = spanRanges.find(({ start, end }) => cutOff > start && cutOff < end);
151+
if (!activeSpan) {
152+
return cutOff;
153+
}
154+
155+
// Cutting in front of a span that opens the content would leave nothing to render, keep the complete span instead.
156+
return content.slice(0, activeSpan.start).trim().length > 0 ? activeSpan.start : activeSpan.end;
119157
};
120158

121159
const getParagraphRanges = (content: string): ParagraphRange[] => {
@@ -207,9 +245,13 @@ export const truncateMarkdown = (content: string, cutOff: number, suffix?: strin
207245
const appendSuffix = (truncated: string) => (suffix ? `${truncated.trimEnd()}\n\n${suffix}` : truncated.trimEnd());
208246
const fenceRanges = getFenceRanges(content);
209247
const linkRanges = getLinkRanges(content, fenceRanges);
210-
const emphasisRanges = getEmphasisRanges(content, fenceRanges);
248+
const inlineSpanRanges = getInlineSpanRanges(content, fenceRanges);
211249
const listItemRanges = getListItemRanges(content, fenceRanges);
212-
const safeCutOff = moveCutOffOutsideEmphasis(moveCutOffOutsideLink(cutOff, linkRanges), emphasisRanges);
250+
const safeCutOff = moveCutOffOutsideInlineSpan(
251+
content,
252+
moveCutOffOutsideLink(cutOff, linkRanges),
253+
inlineSpanRanges,
254+
);
213255

214256
if (safeCutOff >= content.length) {
215257
return content;
@@ -260,7 +302,9 @@ export const truncateMarkdown = (content: string, cutOff: number, suffix?: strin
260302
}
261303

262304
if (cutPoint <= 0) {
263-
cutPoint = safeCutOff > 0 ? safeCutOff : cutOff;
305+
// Never fall back to the raw `cutOff` here. `safeCutOff` was deliberately moved out of links and inline
306+
// spans, re-using `cutOff` would cut into that markup again and leak raw Markdown syntax into the display.
307+
cutPoint = Math.max(safeCutOff, 0);
264308
}
265309

266310
return appendSuffix(content.slice(0, cutPoint));

0 commit comments

Comments
 (0)