RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Upgrade office_oxide to v0.1.16 and drop temporary caption/table fixes (#21978)
xugangqianga72dd5fc2bfc
diff --git a/go.mod b/go.modindex 2fdf58b5ca..eba5b875e8 100644--- a/go.mod+++ b/go.mod@@ -71,7 +71,7 @@ require (github.com/spf13/viper v1.18.2github.com/ucloud/ucloud-sandbox-sdk-go v0.0.0-20260807065450-08464aef9ed5github.com/xuri/excelize/v2 v2.11.1-0.20261003002531-6258dcebc4e2- github.com/yfedoseev/office_oxide/go v0.1.12+ github.com/yfedoseev/office_oxide/go v0.1.16github.com/yfedoseev/pdf_oxide/go v0.3.73github.com/yuin/goldmark v1.7.1github.com/zeebo/xxh3 v1.0.2diff --git a/go.sum b/go.sumindex 3dba24fa76..5d81f9380a 100644--- a/go.sum+++ b/go.sum@@ -609,8 +609,8 @@ github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLgithub.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=github.com/yargevad/filepathx v1.0.0 h1:SYcT+N3tYGi+NvazubCNlvgIPbzAk7i7y2dwg3I5FYc=github.com/yargevad/filepathx v1.0.0/go.mod h1:BprfX/gpYNJHJfc35GjRRpVcwWXS89gGulUIU5tK3tA=-github.com/yfedoseev/office_oxide/go v0.1.12 h1:QLsiB8Aj7LB42D9Vwu1rslRuZg/vSXc4IkVXCDKQv6o=-github.com/yfedoseev/office_oxide/go v0.1.12/go.mod h1:YLtMlKUkRCp/Q96wsy7D6yoBKDeJnP66UH+c9Bb+E+M=+github.com/yfedoseev/office_oxide/go v0.1.16 h1:ZZ3/Z5vlwOunUZ5ZY7XItWg3W8Spcw+C2VJIeSdLlJM=+github.com/yfedoseev/office_oxide/go v0.1.16/go.mod h1:YLtMlKUkRCp/Q96wsy7D6yoBKDeJnP66UH+c9Bb+E+M=github.com/yfedoseev/pdf_oxide/go v0.3.73 h1:hRad+X4W0ILW3bCQeCvdUKcoy8yMj2TFS6/XtoMk55I=github.com/yfedoseev/pdf_oxide/go v0.3.73/go.mod h1:QbJ/nLbez0al2EnqEdEPIlGflFprWmiuUM4mo9rNNOI=github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=diff --git a/internal/parser/parser/docx_caption_cgo_test.go b/internal/parser/parser/docx_caption_cgo_test.goindex 1987dd1e94..b42f083f71 100644--- a/internal/parser/parser/docx_caption_cgo_test.go+++ b/internal/parser/parser/docx_caption_cgo_test.go@@ -134,11 +134,12 @@ func TestDOCXParser_TableCaptionFromHeading_EndToEnd(t *testing.T) {}}-// TestDOCXParser_CaptionStyledParagraphBecomesTableCaption_EndToEnd pins the-// Go-side temporary stopgap (docx_caption_scan.go): a "Caption"-styled-// paragraph immediately above a table is attached as the table caption and is-// NOT emitted as its own text section. (office_oxide itself never surfaces the-// paragraph style; the Go parser recovers it by re-parsing document.xml.)+// TestDOCXParser_CaptionStyledParagraphBecomesTableCaption_EndToEnd exercises+// the real extraction path: office_oxide 0.1.16 surfaces the Word paragraph+// style name in the IR "style" field (yfedoseev/office_oxide#455), and the Go+// parser reads it directly. A "Caption"-styled paragraph immediately above a+// table is attached as the table caption and is NOT emitted as its own text+// section.func TestDOCXParser_CaptionStyledParagraphBecomesTableCaption_EndToEnd(t *testing.T) {res := docxCaptionParse(t, docxCaptionPara("Caption", "设备流转明细")+docxCaptionTable())td := docxCaptionFindTable(t, res)diff --git a/internal/parser/parser/docx_caption_scan.go b/internal/parser/parser/docx_caption_scan.godeleted file mode 100644index d95d7280e9..0000000000--- a/internal/parser/parser/docx_caption_scan.go+++ /dev/null@@ -1,171 +0,0 @@-// Package-level note: this file is a TEMPORARY stopgap, not a permanent fix.-//-// Problem it works around: office_oxide's IR JSON (the structured output the-// Go DOCX parser builds sections from) does NOT expose paragraph style names-// for non-heading paragraphs. It resolves Word heading styles into-// type "heading" + level, but a paragraph styled with the built-in "Caption"-// style (or the Chinese "题注") is emitted as a plain {"type":"paragraph"}-// with no style field. As a result the Go parser cannot tell a caption-// paragraph from ordinary body text, so a table's visible caption is lost-// (see the user-reported case "设备流转明细").-//-// The correct, robust fix lives upstream: teach office_oxide to emit the-// paragraph style name so this detection is unnecessary. That is tracked in-// yfedoseev/office_oxide#455 ("Expose paragraph style name (w:pStyle) in the-// IR JSON for non-heading paragraphs").-//-// Until that lands and office_oxide is upgraded in this repo, we recover the-// caption the cheap way: re-parse word/document.xml ourselves and pair a-// caption-styled paragraph with the table that immediately follows it. This-// MUST be removed once office_oxide emits paragraph styles — do not extend it,-// do not rely on it from new code. The position-based pairing (document-body-// table order vs office_oxide IR table order) is approximate and documented at-// buildDOCXJSONSectionsCore.-package parser--import (- "archive/zip"- "bytes"- "encoding/xml"- "fmt"- "io"- "strings"-)--// docxCaptionStyleNames are the Word paragraph style identifiers that mark a-// paragraph as a table caption for the temporary detection above. The built-in-// caption style's styleId is "Caption" in every locale; "题注" covers documents-// where a caption style was created with the localized display name.-var docxCaptionStyleNames = map[string]bool{- "caption": true,- "题注": true,-}--func isDOCXCaptionStyle(styleID string) bool {- return docxCaptionStyleNames[strings.ToLower(strings.TrimSpace(styleID))]-}--// extractDOCXTableCaptions re-parses word/document.xml to recover table-// captions. It returns one entry per table in document-body reading order: the-// trimmed text of the paragraph immediately above the table when that-// paragraph carries a caption style, otherwise "".-//-// It is intentionally limited to the common case: paragraphs and tables that-// are direct children of w:body, in order. Nested tables (e.g. inside a text-// box) are ignored, matching how office_oxide flattens the typical document.-// Errors are returned to the caller, which falls back to no captions.-//-// TEMPORARY: see the package note at the top of this file.-func extractDOCXTableCaptions(docx []byte) ([]string, error) {- zr, err := zip.NewReader(bytes.NewReader(docx), int64(len(docx)))- if err != nil {- return nil, fmt.Errorf("docx caption scan: open zip: %w", err)- }- var bodyXML []byte- for _, f := range zr.File {- if f.Name == "word/document.xml" {- rc, err := f.Open()- if err != nil {- return nil, fmt.Errorf("docx caption scan: open document.xml: %w", err)- }- bodyXML, err = io.ReadAll(rc)- rc.Close()- if err != nil {- return nil, fmt.Errorf("docx caption scan: read document.xml: %w", err)- }- break- }- }- if bodyXML == nil {- return nil, fmt.Errorf("docx caption scan: word/document.xml not found")- }-- dec := xml.NewDecoder(bytes.NewReader(bodyXML))- var captions []string- var pendingCaption string-- var inBody bool- var bodyDepth int // depth relative to <w:body>; 1 == direct child of body- var inPara bool- var paraIsCaption bool- var inText bool- var paraText strings.Builder-- flushPara := func() {- if paraIsCaption {- pendingCaption = strings.TrimSpace(paraText.String())- } else {- // Any non-caption paragraph clears a caption that was pending- // for the next table, so only an *immediately* preceding caption- // is attached.- pendingCaption = ""- }- inPara = false- paraIsCaption = false- inText = false- paraText.Reset()- }-- for {- tok, err := dec.Token()- if err == io.EOF {- break- }- if err != nil {- return nil, fmt.Errorf("docx caption scan: xml: %w", err)- }- switch t := tok.(type) {- case xml.StartElement:- local := t.Name.Local- if !inBody {- if local == "body" {- inBody = true- bodyDepth = 0- }- continue- }- bodyDepth++- switch {- case bodyDepth == 1 && local == "p":- inPara = true- paraIsCaption = false- inText = false- paraText.Reset()- case bodyDepth == 1 && local == "tbl":- captions = append(captions, pendingCaption)- pendingCaption = ""- case inPara && local == "pStyle":- for _, a := range t.Attr {- if a.Name.Local == "val" {- paraIsCaption = isDOCXCaptionStyle(a.Value)- }- }- case inPara && local == "t":- inText = true- }- case xml.CharData:- if inPara && inText {- paraText.Write(t)- }- case xml.EndElement:- local := t.Name.Local- if !inBody {- continue- }- switch {- case bodyDepth == 1 && local == "p" && inPara:- flushPara()- case inPara && local == "t":- inText = false- case inPara && local == "pStyle":- // nothing to reset; paraIsCaption stays until paragraph end- }- bodyDepth--- if local == "body" {- inBody = false- }- }- }- // A trailing caption paragraph with no following table is discarded.- return captions, nil-}diff --git a/internal/parser/parser/docx_caption_scan_test.go b/internal/parser/parser/docx_caption_scan_test.godeleted file mode 100644index 9107069c6c..0000000000--- a/internal/parser/parser/docx_caption_scan_test.go+++ /dev/null@@ -1,141 +0,0 @@-package parser--import (- "archive/zip"- "bytes"- "testing"-)--// buildSampleDOCX constructs a minimal but valid .docx (ZIP/OOXML) in memory-// from the given body children of <w:body>. The caller supplies the inner XML-// of <w:body> (paragraphs and tables). A default styles.xml is included so the-// document is well-formed; style names are referenced by w:pStyle w:val.-func buildSampleDOCX(t *testing.T, bodyInner string) []byte {- t.Helper()- document := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +- `<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">` +- `<w:body>` + bodyInner + `</w:body></w:document>`- styles := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +- `<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">` +- `<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/></w:style>` +- `<w:style w:type="paragraph" w:styleId="Heading1"><w:name w:val="heading 1"/></w:style>` +- `<w:style w:type="paragraph" w:styleId="Caption"><w:name w:val="caption"/></w:style>` +- `<w:style w:type="paragraph" w:styleId="题注"><w:name w:val="caption"/></w:style>` +- `</w:styles>`- contentTypes := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +- `<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">` +- `<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>` +- `<Default Extension="xml" ContentType="application/xml"/>` +- `<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>` +- `<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>` +- `</Types>`- rels := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +- `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` +- `<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>` +- `</Relationships>`- docRels := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +- `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` +- `<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>` +- `</Relationships>`-- var buf bytes.Buffer- zw := zip.NewWriter(&buf)- files := map[string]string{- "[Content_Types].xml": contentTypes,- "_rels/.rels": rels,- "word/document.xml": document,- "word/styles.xml": styles,- "word/_rels/document.xml.rels": docRels,- }- for name, content := range files {- w, err := zw.Create(name)- if err != nil {- t.Fatalf("zip create %s: %v", name, err)- }- if _, err := w.Write([]byte(content)); err != nil {- t.Fatalf("zip write %s: %v", name, err)- }- }- if err := zw.Close(); err != nil {- t.Fatalf("zip close: %v", err)- }- return buf.Bytes()-}--func docxScanPara(styleID, text string) string {- return `<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">` +- `<w:pPr><w:pStyle w:val="` + styleID + `"/></w:pPr>` +- `<w:r><w:t>` + text + `</w:t></w:r></w:p>`-}--func plainPara(text string) string {- return `<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">` +- `<w:r><w:t>` + text + `</w:t></w:r></w:p>`-}--func docxScanTable() string {- return `<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">` +- `<w:tr><w:tc><w:p><w:r><w:t>A1</w:t></w:r></w:p></w:tc>` +- `<w:tc><w:p><w:r><w:t>B1</w:t></w:r></w:p></w:tc></w:tr></w:tbl>`-}--// A Caption-styled paragraph immediately above a table is recovered.-func TestExtractDOCXTableCaptions_CaptionBeforeTable(t *testing.T) {- doc := buildSampleDOCX(t,- docxScanPara("Heading1", "设备台账")+- docxScanPara("Caption", "设备流转明细")+- docxScanTable()+- plainPara("普通正文"))- caps, err := extractDOCXTableCaptions(doc)- if err != nil {- t.Fatalf("extractDOCXTableCaptions: %v", err)- }- if len(caps) != 1 {- t.Fatalf("want 1 table caption, got %d: %v", len(caps), caps)- }- if caps[0] != "设备流转明细" {- t.Errorf("caption = %q, want %q", caps[0], "设备流转明细")- }-}--// The Chinese caption style "题注" is detected the same way.-func TestExtractDOCXTableCaptions_ChineseCaption(t *testing.T) {- doc := buildSampleDOCX(t, docxScanPara("题注", "设备明细表")+docxScanTable())- caps, err := extractDOCXTableCaptions(doc)- if err != nil {- t.Fatalf("extractDOCXTableCaptions: %v", err)- }- if len(caps) != 1 || caps[0] != "设备明细表" {- t.Fatalf("caption = %v, want [设备明细表]", caps)- }-}--// A non-caption paragraph between the caption and the table detaches it.-func TestExtractDOCXTableCaptions_CaptionNotImmediate(t *testing.T) {- doc := buildSampleDOCX(t,- docxScanPara("Caption", "设备流转明细")+- plainPara("中间说明文字")+- docxScanTable())- caps, err := extractDOCXTableCaptions(doc)- if err != nil {- t.Fatalf("extractDOCXTableCaptions: %v", err)- }- if len(caps) != 1 || caps[0] != "" {- t.Fatalf("caption = %v, want [\"\"]", caps)- }-}--// Multiple tables get the caption of their own preceding caption paragraph.-func TestExtractDOCXTableCaptions_MultipleTables(t *testing.T) {- doc := buildSampleDOCX(t,- docxScanPara("Caption", "第一表")+docxScanTable()+- plainPara("分隔")+- docxScanPara("Caption", "第二表")+docxScanTable())- caps, err := extractDOCXTableCaptions(doc)- if err != nil {- t.Fatalf("extractDOCXTableCaptions: %v", err)- }- if len(caps) != 2 || caps[0] != "第一表" || caps[1] != "第二表" {- t.Fatalf("captions = %v, want [第一表 第二表]", caps)- }-}diff --git a/internal/parser/parser/docx_caption_test.go b/internal/parser/parser/docx_caption_test.goindex 74f75f46f4..6206b9a063 100644--- a/internal/parser/parser/docx_caption_test.go+++ b/internal/parser/parser/docx_caption_test.go@@ -152,38 +152,3 @@ func TestBuildDOCXJSONSections_TableCaption_Empty(t *testing.T) {t.Errorf("empty IR should yield no sections, got %d", len(sections))}}--// TEMPORARY Caption-style detection (docx_caption_scan.go): an explicit-// caption supplied for the table overrides the synthesized heading-location-// caption.-func TestBuildDOCXJSONSections_TableCaption_ExplicitPreferredOverLocation(t *testing.T) {- ir := docxCaptionTableIR(- `{"type":"heading","level":1,"content":[{"type":"text","text":"设备台账"}]}`,- docxCaptionTableBody,- )- sections := buildDOCXJSONSectionsWithCaptions(ir, nil, []string{"设备流转明细"})- if got := docxCaptionFindTableCaption(t, sections); got != "设备流转明细" {- t.Errorf("explicit caption = %q, want %q", got, "设备流转明细")- }-}--// An explicit caption is attached even when the table sits under no heading.-func TestBuildDOCXJSONSections_TableCaption_ExplicitWithNoHeading(t *testing.T) {- ir := docxCaptionTableIR(docxCaptionTableBody)- sections := buildDOCXJSONSectionsWithCaptions(ir, nil, []string{"设备流转明细"})- if got := docxCaptionFindTableCaption(t, sections); got != "设备流转明细" {- t.Errorf("explicit caption = %q, want %q", got, "设备流转明细")- }-}--// A blank entry never overrides a valid heading-location caption.-func TestBuildDOCXJSONSections_TableCaption_BlankDoesNotOverrideLocation(t *testing.T) {- ir := docxCaptionTableIR(- `{"type":"heading","level":1,"content":[{"type":"text","text":"设备台账"}]}`,- docxCaptionTableBody,- )- sections := buildDOCXJSONSectionsWithCaptions(ir, nil, []string{""})- if got := docxCaptionFindTableCaption(t, sections); got != "Table Location: 设备台账" {- t.Errorf("caption = %q, want %q", got, "Table Location: 设备台账")- }-}diff --git a/internal/parser/parser/docx_ir.go b/internal/parser/parser/docx_ir.goindex 9bc9bd144d..f7bf8113fd 100644--- a/internal/parser/parser/docx_ir.go+++ b/internal/parser/parser/docx_ir.go@@ -357,41 +357,32 @@ func docxHeadingLocationCaption(headings []docxHeading) string {// heading styles into element type "heading" plus a level, so the outline// is tracked from those elements alone.//-// This is the caption-only (heading hierarchy) entry point. Use-// buildDOCXJSONSectionsWithCaptions to also attach Caption-styled paragraph-// text (a temporary stopgap; see docx_caption_scan.go).+// buildDOCXJSONSections builds sections from an office_oxide IR. The heading+// hierarchy supplies each table's caption; a "Caption"-styled paragraph that+// immediately precedes a table overrides that with its own text. (Paragraph-+// style recovery used to be a temporary stopgap that re-parsed+// word/document.xml; office_oxide now emits the style directly — see+// yfedoseev/office_oxide#455.)//// A nil sink means a text-only parse: embedded images are skipped and no// media cache is required. Normal DOCX/DOC parsing always passes a sink.func buildDOCXJSONSections(irJSON string, sink *officeMediaSink) []map[string]any {- return buildDOCXJSONSectionsCore(irJSON, sink, nil)+ return buildDOCXJSONSectionsCore(irJSON, sink)}-// buildDOCXJSONSectionsWithCaptions is buildDOCXJSONSections plus the-// TEMPORARY Caption-styled-paragraph detection. tableCaptions must be aligned-// by position with the tables in the IR (see docx_caption_scan.go).-//-// TEMPORARY: remove once office_oxide emits paragraph styles-// (yfedoseev/office_oxide#455).-func buildDOCXJSONSectionsWithCaptions(irJSON string, sink *officeMediaSink, tableCaptions []string) []map[string]any {- return buildDOCXJSONSectionsCore(irJSON, sink, tableCaptions)-}--// buildDOCXJSONSectionsCore is the shared implementation of the two entry-// points above. tableCaptions, when non-nil, supplies a descriptive caption-// for each table indexed by reading order; an explicit caption is preferred-// over the synthesized heading-location caption. The position alignment-// between tableCaptions and the IR tables is approximate (document-body order-// vs office_oxide IR order) and is a known limitation of the temporary-// Caption-style stopgap in docx_caption_scan.go.-func buildDOCXJSONSectionsCore(irJSON string, sink *officeMediaSink, tableCaptions []string) []map[string]any {+// buildDOCXJSONSectionsCore builds sections from an office_oxide IR. The+// heading hierarchy supplies each table's caption; a "Caption"-styled+// paragraph that immediately precedes a table overrides that with its own+// text. (Paragraph-style recovery used to be a temporary stopgap that+// re-parsed word/document.xml; office_oxide now emits the style directly.)+func buildDOCXJSONSectionsCore(irJSON string, sink *officeMediaSink) []map[string]any {var ir docxIRDocumentif err := json.Unmarshal([]byte(irJSON), &ir); err != nil {return nil}var sections []map[string]anytableSequence := 0- captionSequence := 0+ var pendingCaption stringvar headings []docxHeadingvar emitElements func([]docxIRElement, bool)emitElements = func(elements []docxIRElement, topLevel bool) {@@ -405,57 +396,6 @@ func buildDOCXJSONSectionsCore(irJSON string, sink *officeMediaSink, tableCaptioreturn}el := elements[ei]- // TEMPORARY: this merge is a stopgap. Revisit it once docxIRRow- // carries a three-state header field and office_oxide populates- // `TableRow.is_header` for .doc tables (upstream issue- // yfedoseev/office_oxide#456) — at that point prefer rejoining via- // the real header flag and drop the structural proxy below.- //- // office_oxide splits a .doc table whose first row is a styled- // header/title row into a 1-row "header" table immediately- // followed by the body table (the legacy binary .doc format- // stores such a table as two separate Table objects with a blank- // paragraph between them; walk_paragraphs flushes a new Element::Table- // at every non-fInTable paragraph, and emit_prose drops the blank- // one, so the two tables end up adjacent in the IR). Rejoin a- // 1-row table fragment with the adjacent following table when their- // first rows carry the same column count, so one logical table is- // emitted as a single item instead of a header chunk + body chunk- // (each would otherwise carry the same caption).- //- // Why a structural proxy instead of the row's `is_header` flag:- // office_oxide's IR DOES carry `TableRow.is_header`, but the .doc- // converter (convert_doc.rs) never populates it — every .doc row- // arrives with is_header=false, verified on the real document that- // triggered this bug. Only the .docx converter sets it, and .docx- // never splits. So `is_header` is a dead signal for .doc and cannot- // be used to confirm the leading fragment is a header or that the- // following table lacks one. The 1-row gate is the strongest- // available proxy: a 1-row table adjacent to a same-width table is- // overwhelmingly a header fragment, because a genuinely standalone- // 1-row table immediately followed by another table (with only a- // blank paragraph between) is rare.- //- // Regression guards baked into the merge condition:- // - leading fragment is exactly 1 row: two genuinely separate- // MULTI-row tables (same width, blank paragraph between) are- // never merged.- // - same first-row column count: differently-shaped tables stay- // separate.- // - adjacency (no element between): any heading/paragraph/image- // in between keeps the two tables distinct.- if topLevel && el.Type == "table" && len(el.Rows) == 1 && ei+1 < len(elements) {- if nxt := elements[ei+1]; nxt.Type == "table" &&- docxIRTableCols(el) > 0 && docxIRTableCols(el) == docxIRTableCols(nxt) {- merged := nxt- rows := make([]docxIRRow, 0, len(nxt.Rows)+len(el.Rows))- rows = append(rows, el.Rows...)- rows = append(rows, nxt.Rows...)- merged.Rows = rows- el = merged- ei++ // fold the following table into this one; skip it- }- }switch el.Type {case "paragraph", "heading":if el.Type == "heading" {@@ -465,17 +405,13 @@ func buildDOCXJSONSectionsCore(irJSON string, sink *officeMediaSink, tableCaptio}headings = pushDOCXHeading(headings, level, docxElementText(el, "\n"))}- // TEMPORARY (docx_caption_scan.go): when a paragraph's text- // exactly matches the Caption recovered for the next table,- // suppress it as a standalone text section — it is attached- // to that table as the caption instead. The text match keeps- // this safe if the document scan and the IR element order- // ever desync: a non-matching paragraph is never dropped.- if topLevel && captionSequence < len(tableCaptions) {- if explicit := strings.TrimSpace(tableCaptions[captionSequence]); explicit != "" &&- strings.TrimSpace(docxElementText(el, " ")) == explicit {- continue- }+ // A "Caption"-styled paragraph (office_oxide 0.1.16 exposes the+ // Word style name via the IR "style" field, yfedoseev/office_oxide#455)+ // is attached to the next table as its caption and is not emitted as a+ // standalone text section. It is consumed by the next top-level table.+ if topLevel && strings.EqualFold(strings.TrimSpace(el.Style), "Caption") {+ pendingCaption = strings.TrimSpace(docxElementText(el, " "))+ continue}sections = appendDOCXParagraphSections(sections, el, el.Type == "heading", sink)@@ -484,20 +420,15 @@ func buildDOCXJSONSectionsCore(irJSON string, sink *officeMediaSink, tableCaptiocase "table":tableSequence++- if topLevel {- captionSequence++- }if len(el.Rows) == 0 {continue}- // Prefer an explicit Caption-styled paragraph (temporary- // stopgap, see docx_caption_scan.go) over the synthesized- // heading-location caption.+ // A "Caption"-styled paragraph that preceded this table (office_oxide#455)+ // overrides the synthesized heading-location caption.caption := docxHeadingLocationCaption(headings)- if idx := captionSequence - 1; topLevel && idx >= 0 && idx < len(tableCaptions) {- if explicit := strings.TrimSpace(tableCaptions[idx]); explicit != "" {- caption = explicit- }+ if topLevel && pendingCaption != "" {+ caption = pendingCaption+ pendingCaption = ""}sections = appendOfficeTableSections(sections, el, caption, fmt.Sprintf("docx-table-%d", tableSequence), sink)@@ -516,6 +447,17 @@ func buildDOCXJSONSectionsCore(irJSON string, sink *officeMediaSink, tableCaptiofor _, sec := range ir.Sections {emitElements(sec.Elements, true)}++ // An orphan Caption-styled paragraph (no following table) is emitted as a+ // normal text section so it is never silently dropped.+ if pendingCaption != "" {+ sections = append(sections, map[string]any{+ "text": pendingCaption,+ "image": nil,+ "doc_type_kwd": "text",+ })+ }+return sections}diff --git a/internal/parser/parser/docx_ir_caption_style_test.go b/internal/parser/parser/docx_ir_caption_style_test.gonew file mode 100644index 0000000000..9f3a783f91--- /dev/null+++ b/internal/parser/parser/docx_ir_caption_style_test.go@@ -0,0 +1,140 @@+package parser++import (+ "testing"++ "ragflow/internal/entity"+)++// docxCountTables returns the number of top-level table sections.+func docxCountTables(sections []map[string]any) int {+ n := 0+ for _, s := range sections {+ if dt, _ := s["doc_type_kwd"].(string); dt == "table" {+ n+++ }+ }+ return n+}++// docxFirstTableCaption returns the Caption of the first table section.+func docxFirstTableCaption(t *testing.T, sections []map[string]any) string {+ t.Helper()+ for _, s := range sections {+ if dt, _ := s["doc_type_kwd"].(string); dt == "table" {+ td, ok := s["table"].(*entity.TableData)+ if !ok || td == nil {+ t.Fatalf("table section missing *entity.TableData")+ }+ return td.Caption+ }+ }+ t.Fatal("no table section found")+ return ""+}++// docxHasText reports whether a standalone text section carries exactly want.+func docxHasText(sections []map[string]any, want string) bool {+ for _, s := range sections {+ if dt, _ := s["doc_type_kwd"].(string); dt == "text" {+ if txt, _ := s["text"].(string); txt == want {+ return true+ }+ }+ }+ return false+}++// A Caption-styled paragraph (office_oxide 0.1.16 emits "style":"Caption")+// immediately above a table must be attached as the table caption and must+// NOT be emitted as its own text section. Before office_oxide exposed the+// style field this required re-parsing word/document.xml (docx_caption_scan.go);+// now the IR carries it directly.+func TestBuildDOCXJSONSections_TableCaption_FromStyle(t *testing.T) {+ ir := `{"sections":[{"elements":[+ {"type":"paragraph","style":"Caption","content":[{"type":"text","text":"设备流转明细"}]},+ {"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A1"}]}]},{"content":[{"type":"paragraph","content":[{"type":"text","text":"B1"}]}]}]}]}+ ]}]}`+ sections := buildDOCXJSONSections(ir, newOfficeMediaSink(t.Context(), officeMediaFormatDOCX))+ if got := docxFirstTableCaption(t, sections); got != "设备流转明细" {+ t.Errorf("caption = %q, want %q", got, "设备流转明细")+ }+ if docxHasText(sections, "设备流转明细") {+ t.Error("Caption-styled paragraph must be consumed as caption, not left as a text section")+ }+}++// An explicit Caption-styled paragraph wins over the synthesized+// heading-location caption when both precede the table.+func TestBuildDOCXJSONSections_TableCaption_StylePreferredOverHeading(t *testing.T) {+ ir := `{"sections":[{"elements":[+ {"type":"heading","level":1,"content":[{"type":"text","text":"设备台账"}]},+ {"type":"paragraph","style":"Caption","content":[{"type":"text","text":"设备流转明细"}]},+ {"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A1"}]}]},{"content":[{"type":"paragraph","content":[{"type":"text","text":"B1"}]}]}]}]}+ ]}]}`+ sections := buildDOCXJSONSections(ir, newOfficeMediaSink(t.Context(), officeMediaFormatDOCX))+ if got := docxFirstTableCaption(t, sections); got != "设备流转明细" {+ t.Errorf("caption = %q, want explicit caption %q", got, "设备流转明细")+ }+}++// office_oxide reports built-in Caption as the capitalized name; the match+// must be case-insensitive so "caption" (or any casing) is still recognized.+func TestBuildDOCXJSONSections_TableCaption_StyleCaseInsensitive(t *testing.T) {+ ir := `{"sections":[{"elements":[+ {"type":"paragraph","style":"caption","content":[{"type":"text","text":"设备流转明细"}]},+ {"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A1"}]}]}]}]}+ ]}]}`+ sections := buildDOCXJSONSections(ir, newOfficeMediaSink(t.Context(), officeMediaFormatDOCX))+ if got := docxFirstTableCaption(t, sections); got != "设备流转明细" {+ t.Errorf("caption = %q, want %q", got, "设备流转明细")+ }+}++// A Caption-styled paragraph with NO following table is not a caption; it+// must be emitted as an ordinary text section (never silently dropped).+func TestBuildDOCXJSONSections_TableCaption_StyleWithoutTableEmittedAsText(t *testing.T) {+ ir := `{"sections":[{"elements":[+ {"type":"paragraph","style":"Caption","content":[{"type":"text","text":"孤立题注"}]}+ ]}]}`+ sections := buildDOCXJSONSections(ir, newOfficeMediaSink(t.Context(), officeMediaFormatDOCX))+ if docxCountTables(sections) != 0 {+ t.Errorf("expected no table sections, got %d", docxCountTables(sections))+ }+ if !docxHasText(sections, "孤立题注") {+ t.Error("orphan Caption-styled paragraph must be emitted as a text section")+ }+}++// A plain (non-Caption) paragraph above a table must never become a caption;+// only the heading hierarchy is used for it.+func TestBuildDOCXJSONSections_TableCaption_NonCaptionParagraphIgnored(t *testing.T) {+ ir := `{"sections":[{"elements":[+ {"type":"paragraph","content":[{"type":"text","text":"设备流转明细"}]},+ {"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A1"}]}]}]}]}+ ]}]}`+ sections := buildDOCXJSONSections(ir, newOfficeMediaSink(t.Context(), officeMediaFormatDOCX))+ if got := docxFirstTableCaption(t, sections); got == "设备流转明细" {+ t.Errorf("plain paragraph must not become a caption, got %q", got)+ }+}++// Regression for the old .doc split workaround (office_oxide#456): a 1-row+// table immediately followed by a same-width multi-row table must NOT be+// merged into one section. office_oxide 0.1.16 no longer splits .doc tables,+// so the structural proxy in buildDOCXJSONSectionsCore is gone and two+// adjacent tables stay distinct.+func TestBuildDOCXJSONSections_AdjacentTablesNotMerged(t *testing.T) {+ ir := `{"sections":[{"elements":[+ {"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"H"}]}]}]}]},+ {"type":"table","rows":[+ {"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A1"}]}]}]},+ {"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A2"}]}]}]},+ {"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"A3"}]}]}]}+ ]}+ ]}]}`+ sections := buildDOCXJSONSections(ir, newOfficeMediaSink(t.Context(), officeMediaFormatDOCX))+ if got := docxCountTables(sections); got != 2 {+ t.Errorf("adjacent tables must stay distinct, got %d tables (want 2)", got)+ }+}diff --git a/internal/parser/parser/docx_ir_test.go b/internal/parser/parser/docx_ir_test.goindex 6ef50aaea8..1d1576b0a6 100644--- a/internal/parser/parser/docx_ir_test.go+++ b/internal/parser/parser/docx_ir_test.go@@ -54,44 +54,6 @@ func TestBuildDOCXJSONSections_EmptyTableSkipped(t *testing.T) {}}-// TestBuildDOCXJSONSections_MergesSplitDocTable pins the fix for the-// office_oxide .doc artifact where a table whose first row is a styled-// header is emitted as a 1-row "header" table immediately followed by the-// body table. The two fragments share the same column count and sit under-// the same heading, so they must be rejoined into a single table item-// (otherwise the chunker emits a header chunk + body chunk, each carrying-// the same caption).-func TestBuildDOCXJSONSections_MergesSplitDocTable(t *testing.T) {- const cell = `{"content":[{"type":"paragraph","content":[{"type":"text","text":"x"}]}]}`- // header: 1 row x 3 cols; body: 2 rows x 3 cols — same width.- irJSON := fmt.Sprintf(`{"sections":[{"title":"","elements":[- {"type":"heading","level":1,"content":[{"type":"text","text":"H"}]},- {"type":"table","rows":[{"cells":[%s,%s,%s]}]},- {"type":"table","rows":[{"cells":[%s,%s,%s]},{"cells":[%s,%s,%s]}]}- ]}]}`, cell, cell, cell, cell, cell, cell, cell, cell, cell)-- got := buildDOCXJSONSections(irJSON, nil)- var tables []map[string]any- for _, it := range got {- if dt, _ := it["doc_type_kwd"].(string); dt == "table" {- tables = append(tables, it)- }- }- if len(tables) != 1 {- t.Fatalf("expected 1 merged table item, got %d: %#v", len(tables), got)- }- td, ok := tables[0]["table"].(*entity.TableData)- if !ok || td == nil {- t.Fatalf("merged table has no TableData: %#v", tables[0])- }- if len(td.Grid) != 3 {- t.Errorf("merged table should have 3 rows (1 header + 2 body), got %d: %#v", len(td.Grid), td.Grid)- }- if td.Caption != "Table Location: H" {- t.Errorf("merged table caption = %q, want %q", td.Caption, "Table Location: H")- }-}-// TestBuildDOCXJSONSections_DifferentColumnTablesStaySeparate pins that// two adjacent tables of different column counts are NOT merged: they are// genuinely separate tables, not fragments of one.diff --git a/internal/parser/parser/docx_parser.go b/internal/parser/parser/docx_parser.goindex 780f1d7371..0267dfed21 100644--- a/internal/parser/parser/docx_parser.go+++ b/internal/parser/parser/docx_parser.go@@ -86,14 +86,13 @@ func (p *DOCXParser) ParseWithResult(ctx context.Context, filename string, dataif irJSON == "" {return ParseResult{Err: fmt.Errorf("docx extract: empty IR, cannot build JSON output")}}- // TEMPORARY Caption-styled-paragraph detection (office_oxide does- // not yet expose paragraph styles; yfedoseev/office_oxide#455).- // Remove once office_oxide is upgraded to emit them.- tableCaptions, capErr := extractDOCXTableCaptions(data)- if capErr != nil {- tableCaptions = nil- }- sections := buildDOCXJSONSectionsWithCaptions(irJSON, sink, tableCaptions)+ // office_oxide 0.1.16 exposes the Word paragraph style name (e.g.+ // "Caption") in the IR "style" field (yfedoseev/office_oxide#455); a+ // Caption-styled paragraph is attached to the table that follows it and is+ // not emitted as a standalone text section. This replaces the temporary+ // word/document.xml re-parse that the Go parser used before the field+ // existed.+ sections := buildDOCXJSONSections(irJSON, sink)if sink != nil && sink.err != nil {return ParseResult{Err: sink.err}}diff --git a/internal/parser/parser/table_regression_test.go b/internal/parser/parser/table_regression_test.goindex 216d2435b1..ebbe76e2e4 100644--- a/internal/parser/parser/table_regression_test.go+++ b/internal/parser/parser/table_regression_test.go@@ -26,9 +26,9 @@ func TestTableRegressionMarkdownMixedBackticksKeepTrailingCells(t *testing.T) {}func TestTableRegressionDOCXCaptionAfterNestedTable(t *testing.T) {- ir := `{"sections":[{"elements":[{"type":"text_box","content":[{"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"Nested"}]}]}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Outer Caption"}]},{"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"Outer"}]}]}]}]}]}]}`+ ir := `{"sections":[{"elements":[{"type":"text_box","content":[{"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"Nested"}]}]}]}]}]},{"type":"paragraph","style":"Caption","content":[{"type":"text","text":"Outer Caption"}]},{"type":"table","rows":[{"cells":[{"content":[{"type":"paragraph","content":[{"type":"text","text":"Outer"}]}]}]}]}]}]}`- after := buildDOCXJSONSectionsWithCaptions(ir, nil, []string{"Outer Caption"})+ after := buildDOCXJSONSections(ir, nil)if len(after) != 2 {t.Fatalf("expected two tables without duplicate caption text: %#v", after)}@@ -37,7 +37,7 @@ func TestTableRegressionDOCXCaptionAfterNestedTable(t *testing.T) {if tableRowsContain(td, "Nested") && td.Caption != "" {t.Error("nested table stole the outer caption")}- if tableRowsContain(td, "Outer") && td.Caption != "Outer Caption" {+ if tableRowsContain(td, "Outer") && td.Caption != "Outer Caption" { // caption from preceding Caption-styled paragrapht.Error("outer table lost its caption")}}