Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(feishu): keep nested text in native table cells (#4400)

davienb373374bf38a
diff --git a/internal/datasource/connector/feishu/core/markdown.go b/internal/datasource/connector/feishu/core/markdown.go
index 822824997..333313ab2 100644
--- a/internal/datasource/connector/feishu/core/markdown.go
+++ b/internal/datasource/connector/feishu/core/markdown.go
@@ -773,18 +773,50 @@ func textBearingField(b DocxBlock) *BlockText {
// cell itself, so we concatenate the text of each child block; the first child
// carrying an explicit style alignment (2 center / 3 right) wins.
func cellTextAndAlign(cell DocxBlock, byID map[string]DocxBlock) (string, int) {
- var parts []string
align := 0
for _, childID := range cell.Children {
child := byID[childID]
- if t := plainText(textBearingField(child)); t != "" {
- parts = append(parts, t)
- }
if align == 0 && child.Text != nil && child.Text.Style != nil && child.Text.Style.Align != 0 {
align = child.Text.Style.Align
}
}
- return strings.Join(parts, " "), align
+ return cellSubtreeText(cell, byID), align
+}
+
+// cellSubtreeText joins the text of every block under a cell in document
+// order. tableDescendants consumes the whole subtree (lists and callouts
+// nested in the cell included), so text below the direct children would
+// otherwise be dropped entirely. Media blocks and their subtrees are skipped
+// exactly as tableDescendants leaves them to the flat loop, and each block is
+// visited at most once so repeated or cyclic children neither duplicate text
+// nor loop.
+func cellSubtreeText(cell DocxBlock, byID map[string]DocxBlock) string {
+ var parts []string
+ seen := map[string]bool{cell.BlockID: true}
+ var pending []string
+ push := func(children []string) {
+ for i := len(children) - 1; i >= 0; i-- {
+ pending = append(pending, children[i])
+ }
+ }
+ push(cell.Children)
+ for len(pending) > 0 {
+ id := pending[len(pending)-1]
+ pending = pending[:len(pending)-1]
+ block, ok := byID[id]
+ if !ok || seen[id] {
+ continue
+ }
+ seen[id] = true
+ if block.BlockType == BlockTypeFile || block.BlockType == BlockTypeImage || block.BlockType == BlockTypeBoard {
+ continue
+ }
+ if t := plainText(textBearingField(block)); t != "" {
+ parts = append(parts, t)
+ }
+ push(block.Children)
+ }
+ return strings.Join(parts, " ")
}
// renderNativeTable renders a native docx table block into a Markdown table.
diff --git a/internal/datasource/connector/feishu/core/markdown_cell_nested_test.go b/internal/datasource/connector/feishu/core/markdown_cell_nested_test.go
new file mode 100644
index 000000000..54075df7d
--- /dev/null
+++ b/internal/datasource/connector/feishu/core/markdown_cell_nested_test.go
@@ -0,0 +1,72 @@
+package core
+
+import (
+ "context"
+ "strings"
+ "testing"
+)
+
+func textBlk(id, text string, children ...string) DocxBlock {
+ return DocxBlock{BlockID: id, BlockType: BlockTypeText, Text: txt(text), Children: children}
+}
+
+// A table cell is a container: list or callout blocks nested in it are
+// consumed by tableDescendants, so the cell must render their text.
+func TestNativeTablePreservesNestedCellTextOnce(t *testing.T) {
+ blocks := []DocxBlock{
+ tableBlk("table", 1, "cell"),
+ {BlockID: "cell", BlockType: BlockTypeTableCell, Children: []string{"callout", "after"}},
+ {BlockID: "callout", BlockType: BlockTypeCallout, Children: []string{"item"}},
+ {BlockID: "item", BlockType: BlockTypeBullet, Bullet: txt("初始化参数"), Children: []string{"nested"}},
+ textBlk("nested", "appKey 必填"),
+ textBlk("after", "最后启动"),
+ }
+ md, _, _, _, err := blocksToMarkdown(context.Background(), nil, blocks, "", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(md), "| 初始化参数 appKey 必填 最后启动 |") {
+ t.Fatalf("nested cell text missing or reordered:\n%s", md)
+ }
+ for _, text := range []string{"初始化参数", "appKey 必填", "最后启动"} {
+ if strings.Count(string(md), text) != 1 {
+ t.Fatalf("cell text %q duplicated:\n%s", text, md)
+ }
+ }
+}
+
+func TestNativeTableNestedCellTextHandlesRepeatedAndCyclicReferences(t *testing.T) {
+ blocks := []DocxBlock{
+ tableBlk("table", 1, "cell"),
+ {BlockID: "cell", BlockType: BlockTypeTableCell, Children: []string{"container", "leaf", "missing"}},
+ {BlockID: "container", BlockType: BlockTypeCallout, Children: []string{"leaf", "cell", "container"}},
+ textBlk("leaf", "保留一次"),
+ }
+ md, _, _, _, err := blocksToMarkdown(context.Background(), nil, blocks, "", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Count(string(md), "保留一次") != 1 {
+ t.Fatalf("repeated or cyclic reference duplicated text:\n%s", md)
+ }
+}
+
+// Media keeps its own handling in the flat loop, and so does text nested
+// under it; the cell must not render it a second time.
+func TestNativeTableNestedCellTextSkipsMediaSubtrees(t *testing.T) {
+ for _, kind := range []int{BlockTypeFile, BlockTypeImage, BlockTypeBoard} {
+ blocks := []DocxBlock{
+ tableBlk("table", 1, "cell"),
+ {BlockID: "cell", BlockType: BlockTypeTableCell, Children: []string{"media"}},
+ {BlockID: "media", BlockType: kind, Children: []string{"caption"}},
+ textBlk("caption", "媒体说明"),
+ }
+ md, _, _, _, err := blocksToMarkdown(context.Background(), nil, blocks, "", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(md), "| 媒体说明 |") {
+ t.Fatalf("media subtree rendered inside the cell for block type %d:\n%s", kind, md)
+ }
+ }
+}