Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/detect-header-only-tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/rag": patch
---

Recognize header-only Markdown tables at the beginning of a document with either LF or CRLF line endings.
71 changes: 71 additions & 0 deletions packages/rag/src/utils/format-detector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, it } from "vitest";
import { detectFormat } from "./format-detector";

describe("Markdown table detection", () => {
it("accepts an initial byte order mark", () => {
expect(detectFormat("\uFEFF| Name | Value |\n| --- | --- |")).toBe("table");
});

it.each(["", "intro\n"])("preserves code indentation after %j", (prefix) => {
expect(detectFormat(prefix + " | Name |\n | --- |")).toBe("text");
});

it("accepts three-space indentation and escaped header pipes", () => {
expect(detectFormat(" | A \\| B | C |\n | --- | --- |")).toBe("table");
});

it("rejects a delimiter with a different number of cells", () => {
expect(detectFormat("| Name | Value |\n| ---")).toBe("text");
});

it("rejects long whitespace followed by an invalid delimiter character", () => {
expect(detectFormat("| Name |\n|--" + " ".repeat(100000) + "x")).toBe("text");
});

it.each(["\n", "\r\n"])("detects a table with only a header and delimiter (%j)", (newline) => {
const table = ["| Name | Value |", "| --- | --- |"].join(newline);

expect(detectFormat(table)).toBe("table");
});

it("treats a pipe row without a delimiter as text", () => {
expect(detectFormat("| Name | Value |")).toBe("text");
});

it.each(["\n", "\r\n"])("accepts a delimiter without a trailing pipe (%j)", (newline) => {
const header = "| Name | Value |";
const delimiter = "| :--- | ---:";

expect(detectFormat([header, delimiter].join(newline))).toBe("table");
expect(detectFormat([header, delimiter, "| one | two |"].join(newline))).toBe("table");
});

it.each(["\n", "\r\n"])("accepts aligned delimiter cells (%j)", (newline) => {
const table = ["| Name | Value |", "| :--- | ---: |"].join(newline);

expect(detectFormat(table)).toBe("table");
});

it("does not match an unrelated delimiter in prose", () => {
expect(detectFormat("| note |\nprose containing | -- | marker")).toBe("text");
});

it("accepts headers without a trailing pipe and delimiters without a leading pipe", () => {
expect(detectFormat("| A | B\n| --- | --- |")).toBe("table");
expect(detectFormat("| A | B |\n--- | ---")).toBe("table");
expect(detectFormat("A | B\n--- | ---")).toBe("table");
});

it("accepts single-hyphen delimiter cells", () => {
expect(detectFormat("| A | B |\n| - | - |")).toBe("table");
expect(detectFormat("| A | B |\n| :- | -: |")).toBe("table");
});

it("does not treat a setext heading without pipes as a table", () => {
expect(detectFormat("Title\n---")).toBe("text");
});

it("preserves code indentation for pipe-less outer edge rows", () => {
expect(detectFormat(" A | B\n - | -")).toBe("text");
});
});
65 changes: 63 additions & 2 deletions packages/rag/src/utils/format-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,52 @@ const markdownSignals = [
/\[.+?\]\(.+?\)/m,
];

const splitTableRow = (line: string, allowEscapedPipes: boolean): string[] | null => {
let trimmed = line;
let hasPipe = false;

if (trimmed.startsWith("|")) {
hasPipe = true;
trimmed = trimmed.slice(1);
}
if (trimmed.endsWith("|")) {
let backslashes = 0;
if (allowEscapedPipes) {
for (let k = trimmed.length - 2; k >= 0 && trimmed[k] === "\\"; k--) {
backslashes++;
}
}
if (backslashes % 2 === 0) {
hasPipe = true;
trimmed = trimmed.slice(0, -1);
}
}

const cells: string[] = [];
let cell = "";
for (let idx = 0; idx < trimmed.length; idx++) {
const ch = trimmed[idx];
if (allowEscapedPipes && ch === "\\" && idx + 1 < trimmed.length) {
const next = trimmed[idx + 1];
if (next === "\\" || next === "|") {
cell += next;
idx++;
continue;
}
}
if (ch === "|") {
hasPipe = true;
cells.push(cell);
cell = "";
continue;
}
cell += ch;
}
cells.push(cell);

return hasPipe ? cells : null;
};

export function detectFormat(input: string): DetectedFormat {
const text = input.trim();
if (!text) return "text";
Expand Down Expand Up @@ -47,8 +93,23 @@ export function detectFormat(input: string): DetectedFormat {
}

// Table pipe syntax
if (/\n\|.+\|\n/.test(text) && /\|\s*-{2,}\s*\|/.test(text)) {
return "table";
const lines = input.replace(/^\uFEFF/, "").split(/\r?\n/);
for (let i = 1; i < lines.length; i++) {
const header = lines[i - 1].replace(/^ {0,3}/, "").trimEnd();
const delimiter = lines[i].replace(/^ {0,3}/, "").trimEnd();
if (!header || !delimiter || /^\s/.test(header) || /^\s/.test(delimiter)) {
continue;
}
const headerCells = splitTableRow(header, true);
const delimiterCells = splitTableRow(delimiter, false);
if (
headerCells &&
delimiterCells &&
headerCells.length === delimiterCells.length &&
delimiterCells.every((cell) => /^:?-+:?$/.test(cell.trim()))
) {
return "table";
}
}

// Markdown-ish signals
Expand Down