From cc82e75cb5ca5ac0fc66386917810519054870f2 Mon Sep 17 00:00:00 2001 From: Lvcy_cd <757104454@qq.com> Date: Mon, 21 Sep 2026 14:25:39 +0800 Subject: [PATCH] fix(parser): keep PDFs on builtin instead of defaulting to anydoc anydoc has no document model for PDF: Convert force-disables asset extraction, and AnydocReader falls back to builtin only when the whole file yields no text. A born-digital PDF with figures and tables that still yields a few characters is therefore reported as parsed, with its images, tables and layout silently dropped. 35275d56 widened the type-level preference from ppt/pptx to every type anydoc claims, and pdf is on that list. The predicate asks whether anydoc can convert a type, not whether it is the better parser for it. 60207997, one commit earlier, recorded the original intent: "Types without a fallback (pdf, docx, ...) never consult this". The builtin PDF parser classifies pages individually and routes scanned ones through OCR/VLM, so it stays the default for PDF. Every other anydoc format is unchanged, and an explicit pdf -> anydoc engine rule still wins. --- .../docparser/engine_registry_test.go | 26 ++++++++++++++++--- internal/infrastructure/docparser/engines.go | 13 ++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/internal/infrastructure/docparser/engine_registry_test.go b/internal/infrastructure/docparser/engine_registry_test.go index 26ba84e7..4bd82424 100644 --- a/internal/infrastructure/docparser/engine_registry_test.go +++ b/internal/infrastructure/docparser/engine_registry_test.go @@ -33,7 +33,7 @@ func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) { } func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) { - cases := []string{"pptx", "ppt", "pdf", "docx"} + cases := []string{"pptx", "ppt", "docx"} if anydoc.Available() { for _, ft := range cases { if got := types.DefaultParserEngine(ft); got != AnydocEngineName { @@ -43,15 +43,33 @@ func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) { if got := types.DefaultParserEngine("csv"); got != "" { t.Errorf("DefaultParserEngine(csv) = %q, want empty so the Go simple reader stays default", got) } + // anydoc claims pdf but cannot model one, so linking the binding must + // not move PDFs off builtin. This branch is where that is observable. + for _, ft := range []string{"pdf", ".PDF"} { + if got := types.DefaultParserEngine(ft); got != "" { + t.Errorf("DefaultParserEngine(%q) = %q, want empty so PDFs stay on builtin", ft, got) + } + } return } if got := types.DefaultParserEngine("pptx"); got != "markitdown" { t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got) } - if got := types.DefaultParserEngine("pdf"); got != "" { - t.Fatalf("DefaultParserEngine(pdf) = %q, want empty when anydoc is unavailable", got) - } if got := types.DefaultParserEngine("docx"); got != "" { t.Fatalf("DefaultParserEngine(docx) = %q, want empty when anydoc is unavailable", got) } } + +// The PDF exclusion only matters because anydoc still claims the type. If a +// future anydoc drops pdf, the exclusion becomes dead code and should go with +// it — this guard runs in every build, tagged or not. +func TestPreferAnydocSkipsPDF(t *testing.T) { + if !anydoc.Supports("pdf", "") { + t.Fatal("anydoc no longer claims pdf; drop the explicit exclusion in preferAnydocWhenAvailable") + } + for _, ft := range []string{"pdf", ".PDF", " Pdf "} { + if got := preferAnydocWhenAvailable(ft); got != "" { + t.Errorf("preferAnydocWhenAvailable(%q) = %q, want empty so PDFs stay on builtin", ft, got) + } + } +} diff --git a/internal/infrastructure/docparser/engines.go b/internal/infrastructure/docparser/engines.go index 0dafa648..7883094f 100644 --- a/internal/infrastructure/docparser/engines.go +++ b/internal/infrastructure/docparser/engines.go @@ -44,11 +44,20 @@ func init() { // preferAnydocWhenAvailable is the type-level default override: when the // anydoc binding is linked and converts this file type, use it instead of // builtin or the markitdown fallback. Simple formats stay on the Go reader. +// +// PDF is excluded. anydoc has no document model for it, so Convert force- +// disables asset extraction, and AnydocReader only falls back to builtin +// when the whole file yields no text at all. A born-digital PDF that yields +// a few characters is therefore reported as parsed while its figures, +// tables and layout are gone. The builtin parser classifies pages +// individually and routes scanned ones through OCR/VLM, so it is the better +// default for the one type anydoc cannot model. func preferAnydocWhenAvailable(fileType string) string { - if IsSimpleFormat(fileType) { + ft := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(fileType), ".")) + if IsSimpleFormat(ft) || ft == "pdf" { return "" } - if anydoc.Available() && anydoc.Supports(fileType, "") { + if anydoc.Available() && anydoc.Supports(ft, "") { return AnydocEngineName } return ""