Skip to content

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

fix(parser): recognize a Table of Contents heading when removing a TOC (#21331)

Lukas2d4f2796b226
diff --git a/internal/parser/parser/text_toc.go b/internal/parser/parser/text_toc.go
index e31e42d4d6..5137a1d99f 100644
--- a/internal/parser/parser/text_toc.go
+++ b/internal/parser/parser/text_toc.go
@@ -9,7 +9,9 @@ import (
// rag/nlp/__init__.py:945 — case-insensitive match against
// contents/目录/目次/table of contents/致谢/acknowledge after
// stripping all spaces (ASCII + fullwidth).
-var tocHeadingPattern = regexp.MustCompile(`(?i)^(contents|目录|目次|table of contents|致谢|acknowledge)$`)
+// The heading is matched after whitespacePattern has removed its spaces, so a
+// multi-word heading is listed without them: "Table of Contents" reads "tableofcontents".
+var tocHeadingPattern = regexp.MustCompile(`(?i)^(contents|目录|目次|tableofcontents|致谢|acknowledge)$`)
// whitespacePattern matches ASCII spaces and fullwidth space (U+3000)
// for normalization before heading match.
diff --git a/internal/parser/parser/text_toc_test.go b/internal/parser/parser/text_toc_test.go
index 01ceb8137b..cfb2907cfc 100644
--- a/internal/parser/parser/text_toc_test.go
+++ b/internal/parser/parser/text_toc_test.go
@@ -42,6 +42,22 @@ func TestRemoveContentsTable(t *testing.T) {
{"text": "正文开始"},
},
},
+ {
+ name: "multi-word Table of Contents heading + first TOC entry removed",
+ items: []map[string]any{
+ {"text": "Intro"},
+ {"text": "Table of Contents"},
+ {"text": "Chapter 1 Overview"},
+ {"text": "Chapter 2 Method"},
+ {"text": "Body starts"},
+ },
+ eng: true,
+ want: []map[string]any{
+ {"text": "Intro"},
+ {"text": "Chapter 2 Method"},
+ {"text": "Body starts"},
+ },
+ },
{
name: "English Contents heading + first TOC entry removed",
items: []map[string]any{