Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(parser): keep the paragraphs of a Markdown blockquote apart (#21347)

Lukas34839b71457a
diff --git a/internal/parser/parser/markdown_parser.go b/internal/parser/parser/markdown_parser.go
index 2371af0fe1..260d045e63 100644
--- a/internal/parser/parser/markdown_parser.go
+++ b/internal/parser/parser/markdown_parser.go
@@ -901,6 +901,16 @@ func walkLeaf(n ast.Node, buf *bytes.Buffer) {
}
walkLeaf(c, buf)
}
+ case *ast.BlockQuote:
+ // The blocks of a blockquote carry no separator of their own, so two quoted
+ // paragraphs ran together as "First quoteSecond quote". Put each block on its
+ // own line, as for the blocks of a list item.
+ for i, c := range n.GetChildren() {
+ if i > 0 {
+ buf.WriteByte('\n')
+ }
+ walkLeaf(c, buf)
+ }
default:
for _, c := range n.GetChildren() {
walkLeaf(c, buf)
diff --git a/internal/parser/parser/markdown_parser_test.go b/internal/parser/parser/markdown_parser_test.go
index b43966133e..f7b116fce0 100644
--- a/internal/parser/parser/markdown_parser_test.go
+++ b/internal/parser/parser/markdown_parser_test.go
@@ -39,6 +39,32 @@ func TestMarkdownParser_ParseWithResult_Basic(t *testing.T) {
}
}
+func TestMarkdownParser_BlockquoteKeepsItsBlocksApart(t *testing.T) {
+ p, err := NewMarkdownParser(GoMarkdown)
+ if err != nil {
+ t.Fatalf("NewMarkdownParser: %v", err)
+ }
+ for _, tc := range []struct {
+ md string
+ want string
+ }{
+ {"> First quote\n>\n> Second quote\n", "First quote\nSecond quote"},
+ {"> One\n>\n> Two\n>\n> Three\n", "One\nTwo\nThree"},
+ {"> Intro\n>\n> - item one\n> - item two\n", "Intro\nitem one\nitem two"},
+ } {
+ res := p.ParseWithResult(t.Context(), "test.md", []byte(tc.md))
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult(%q): %v", tc.md, res.Err)
+ }
+ if len(res.JSON) != 1 {
+ t.Fatalf("ParseWithResult(%q) returned %d items, want 1", tc.md, len(res.JSON))
+ }
+ if got, _ := res.JSON[0].ToMap()["text"].(string); got != tc.want {
+ t.Fatalf("ParseWithResult(%q) text = %q, want %q", tc.md, got, tc.want)
+ }
+ }
+}
+
// A UTF-8 BOM in front of the first heading must not demote it to a paragraph.
func TestMarkdownParser_ParseWithResult_UTF8BOM(t *testing.T) {
p, err := NewMarkdownParser(GoMarkdown)