Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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.
24 changes: 24 additions & 0 deletions packages/rag/src/utils/format-detector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from "vitest";
import { detectFormat } from "./format-detector";

describe("Markdown table detection", () => {
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 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");
});
});
2 changes: 1 addition & 1 deletion packages/rag/src/utils/format-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function detectFormat(input: string): DetectedFormat {
}

// Table pipe syntax
if (/\n\|.+\|\n/.test(text) && /\|\s*-{2,}\s*\|/.test(text)) {
if (/^\|[^\r\n]+\|[ \t]*\r?\n\|(?:[ \t]*:?-{2,}:?[ \t]*\|)+[ \t]*$/m.test(text)) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return "table";
}

Expand Down