fix(docparser): handle nested linked images and url parens

This commit is contained in:
wizardchen
2026-03-17 23:25:54 +08:00
committed by lyingbug
parent 3450ed5e41
commit f3a3c72a4f
3 changed files with 47 additions and 8 deletions
+6 -1
View File
@@ -7,6 +7,8 @@ import (
"regexp"
"strings"
"unicode/utf8"
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
)
// Chunk represents a piece of split text with position tracking.
@@ -451,9 +453,12 @@ func SplitTextParentChild(text string, parentCfg, childCfg SplitterConfig) Paren
}
// ExtractImageRefs extracts markdown image references from text.
var imageRefPattern = regexp.MustCompile(`!\[([^\]]*)\]\(([^)]+)\)`)
// The URL group supports one level of balanced parentheses so that URLs
// like https://example.com/item_(abc)/123 are captured in full.
var imageRefPattern = regexp.MustCompile(`!\[([^\]]*)\]\(([^()\s]*(?:\([^)]*\)[^()\s]*)*)\)`)
func ExtractImageRefs(text string) []ImageRef {
text = docparser.UnwrapLinkedImages(text)
matches := imageRefPattern.FindAllStringSubmatchIndex(text, -1)
var refs []ImageRef
for _, m := range matches {