Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: keep reply tables inside longer code fences

Mohammad Maraqa84b4b0899259
diff --git a/src/lib/formatted-reply.test.ts b/src/lib/formatted-reply.test.ts
index b994d2c1ef..04b9e7cac5 100644
--- a/src/lib/formatted-reply.test.ts
+++ b/src/lib/formatted-reply.test.ts
@@ -28,6 +28,15 @@ describe("isFormattedReply", () => {
describe("splitReplyBlocks", () => {
const table = "| Sender | Emails |\n|---|---:|\n| Acme | 4 |\n| Priya | 2 |";
+ it.each(["`", "~"])("keeps tables inside longer %s fences as code", (character) => {
+ const outer = character.repeat(4);
+ const fenced = [`${outer}md`, character.repeat(3), table, `${outer} still code`, table, character.repeat(5)].join("\n");
+ expect(splitReplyBlocks(`${fenced}\n\n${table}`)).toEqual([
+ { kind: "prose", text: fenced },
+ { kind: "table", text: table },
+ ]);
+ });
+
it("keeps a reply with no table as one prose block", () => {
expect(splitReplyBlocks("Hello\n\n- one\n- two")).toEqual([{ kind: "prose", text: "Hello\n\n- one\n- two" }]);
});
diff --git a/src/lib/formatted-reply.ts b/src/lib/formatted-reply.ts
index a7cafbeb00..cb2d7d345c 100644
--- a/src/lib/formatted-reply.ts
+++ b/src/lib/formatted-reply.ts
@@ -27,7 +27,7 @@ export interface ReplyBlock {
const PIPE_ROW = /^ {0,3}\|/;
const DELIMITER_ROW = /^ {0,3}\|?(?: *:?-+:? *\|)+(?: *:?-+:? *)?$/;
-const FENCE = /^ {0,3}(```|~~~)/;
+const FENCE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
/** Splits a reply into prose and its tables, so Simple mode can draw each table as its own card. A table starts at a
* top-level line opening with "|" that a delimiter row follows, and runs while its lines open with "|"; nothing inside a
@@ -46,8 +46,16 @@ export function splitReplyBlocks(text: string): ReplyBlock[] {
let fence: string | null = null;
for (let index = 0; index < lines.length; index++) {
const line = lines[index]!;
- const marker = FENCE.exec(line)?.[1];
- if (marker && (!fence || marker === fence)) fence = fence ? null : marker;
+ const match = FENCE.exec(line);
+ const marker = match?.[1];
+ if (match && marker) {
+ const suffix = match[2] ?? "";
+ if (!fence) {
+ if (marker[0] === "~" || !suffix.includes("`")) fence = marker;
+ } else if (marker[0] === fence[0] && marker.length >= fence.length && /^[ \t]*$/.test(suffix)) {
+ fence = null;
+ }
+ }
if (fence || marker || !PIPE_ROW.test(line) || !DELIMITER_ROW.test(lines[index + 1] ?? "")) {
prose.push(line);
continue;