fix: preserve markdown code fences

This commit is contained in:
Peter Steinberger
2026-01-15 00:27:27 +00:00
parent 2c312e20f1
commit e5c8abab9e
2 changed files with 27 additions and 4 deletions

View File

@@ -459,11 +459,18 @@ export function markdownToIR(markdown: string, options: MarkdownParseOptions = {
const trimmedText = state.text.trimEnd();
const trimmedLength = trimmedText.length;
let codeBlockEnd = 0;
for (const span of state.styles) {
if (span.style !== "code_block") continue;
if (span.end > codeBlockEnd) codeBlockEnd = span.end;
}
const finalLength = Math.max(trimmedLength, codeBlockEnd);
const finalText = finalLength === state.text.length ? state.text : state.text.slice(0, finalLength);
return {
text: trimmedText,
styles: mergeStyleSpans(clampStyleSpans(state.styles, trimmedLength)),
links: clampLinkSpans(state.links, trimmedLength),
text: finalText,
styles: mergeStyleSpans(clampStyleSpans(state.styles, finalLength)),
links: clampLinkSpans(state.links, finalLength),
};
}

View File

@@ -24,7 +24,7 @@ function isAllowedSlackAngleToken(token: string): boolean {
);
}
function escapeSlackMrkdwnText(text: string): string {
function escapeSlackMrkdwnContent(text: string): string {
if (!text.includes("&") && !text.includes("<") && !text.includes(">")) {
return text;
}
@@ -49,6 +49,22 @@ function escapeSlackMrkdwnText(text: string): string {
return out.join("");
}
function escapeSlackMrkdwnText(text: string): string {
if (!text.includes("&") && !text.includes("<") && !text.includes(">")) {
return text;
}
return text
.split("\n")
.map((line) => {
if (line.startsWith("> ")) {
return `> ${escapeSlackMrkdwnContent(line.slice(2))}`;
}
return escapeSlackMrkdwnContent(line);
})
.join("\n");
}
function buildSlackLink(link: MarkdownLinkSpan, text: string) {
const href = link.href.trim();
if (!href) return null;