fix(docparser): preserve MinerU markdown and persist relative images
MinerU already returns markdown with embedded HTML blocks, but the current\nreader runs the whole document back through html-to-markdown. That\nsecond conversion escapes valid headings and image syntax, so chunk\nprofiling sees plain text instead of markdown structure and relative\nimage references stop matching the storage pipeline.\n\nKeep MinerU output in its original markdown form and only apply narrow\ncompatibility normalization for the specific over-escaped patterns we\nactually need to recover. The converter now matches image refs by the\npaths that are really present in markdown or embedded HTML instead of\nassuming a single images/<name> form.\n\nExtend ImageResolver so relative HTML <img src=...> references share the\nsame storage rewrite path as markdown images, deduplicate repeated saves,\nand keep the frontend sanitizer compatible with MinerU's details/summary\nblocks. Add focused docparser tests that cover escaped markdown repair,\nvariant image path matching, and relative HTML image persistence.
This commit is contained in:
@@ -80,7 +80,7 @@ func (r *ImageResolver) ResolveAndStore(
|
||||
fileSvc interfaces.FileService,
|
||||
tenantID uint64,
|
||||
) (updatedMarkdown string, images []StoredImage, err error) {
|
||||
markdown := UnwrapLinkedImages(result.MarkdownContent)
|
||||
markdown := UnwrapLinkedImages(normalizeMinerUMarkdown(result.MarkdownContent))
|
||||
md2, imgDataURIs, _ := r.ResolveDataURIImages(ctx, markdown, fileSvc, tenantID)
|
||||
markdown = md2
|
||||
images = append(images, imgDataURIs...)
|
||||
@@ -102,6 +102,7 @@ func (r *ImageResolver) ResolveAndStore(
|
||||
for _, ref := range result.ImageRefs {
|
||||
refMap[ref.OriginalRef] = ref
|
||||
}
|
||||
savedRefs := make(map[string]StoredImage)
|
||||
|
||||
// Process each image reference found in the markdown.
|
||||
// The URL group supports one level of balanced parentheses so that URLs
|
||||
@@ -123,51 +124,77 @@ func (r *ImageResolver) ResolveAndStore(
|
||||
}
|
||||
|
||||
// Find inline image bytes from the result
|
||||
ref, found := refMap[refPath]
|
||||
if !found || len(ref.ImageData) == 0 {
|
||||
stored, ok := r.saveReferencedImage(ctx, fileSvc, tenantID, refPath, refMap, savedRefs)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Filter out small icons and decorative images. Skip the filter
|
||||
// when the reference is the originally uploaded file itself, so
|
||||
// that a standalone image upload is never silently dropped even
|
||||
// if its dimensions are below the icon threshold.
|
||||
if !ref.IsOriginal && isIconImage(ref.ImageData) {
|
||||
// Remove the image reference from markdown entirely
|
||||
markdown = markdown[:m[0]] + markdown[m[1]:]
|
||||
continue
|
||||
}
|
||||
|
||||
// Determine extension
|
||||
ext := extFromMime(ref.MimeType)
|
||||
if ext == "" {
|
||||
ext = filepath.Ext(ref.Filename)
|
||||
}
|
||||
if ext == "" {
|
||||
ext = ".png"
|
||||
}
|
||||
|
||||
// Save via FileService — returns provider:// path
|
||||
fileName := uuid.New().String() + ext
|
||||
servingURL, saveErr := fileSvc.SaveBytes(ctx, ref.ImageData, tenantID, fileName, false)
|
||||
if saveErr != nil {
|
||||
log.Printf("WARN: failed to save image %s: %v", refPath, saveErr)
|
||||
continue
|
||||
}
|
||||
|
||||
images = append(images, StoredImage{
|
||||
OriginalRef: refPath,
|
||||
ServingURL: servingURL,
|
||||
MimeType: ref.MimeType,
|
||||
})
|
||||
images = appendStoredImage(images, stored)
|
||||
|
||||
// Replace in markdown
|
||||
markdown = markdown[:m[4]] + servingURL + markdown[m[5]:]
|
||||
markdown = markdown[:m[4]] + stored.ServingURL + markdown[m[5]:]
|
||||
}
|
||||
|
||||
md5, imgRelativeHTML, _ := r.ResolveRelativeHTMLImages(ctx, markdown, fileSvc, tenantID, refMap, savedRefs)
|
||||
markdown = md5
|
||||
images = append(images, imgRelativeHTML...)
|
||||
|
||||
return markdown, images, nil
|
||||
}
|
||||
|
||||
func appendStoredImage(images []StoredImage, stored StoredImage) []StoredImage {
|
||||
for _, existing := range images {
|
||||
if existing.OriginalRef == stored.OriginalRef && existing.ServingURL == stored.ServingURL {
|
||||
return images
|
||||
}
|
||||
}
|
||||
return append(images, stored)
|
||||
}
|
||||
|
||||
func (r *ImageResolver) saveReferencedImage(
|
||||
ctx context.Context,
|
||||
fileSvc interfaces.FileService,
|
||||
tenantID uint64,
|
||||
refPath string,
|
||||
refMap map[string]types.ImageRef,
|
||||
savedRefs map[string]StoredImage,
|
||||
) (StoredImage, bool) {
|
||||
if stored, ok := savedRefs[refPath]; ok {
|
||||
return stored, true
|
||||
}
|
||||
|
||||
ref, found := refMap[refPath]
|
||||
if !found || len(ref.ImageData) == 0 {
|
||||
return StoredImage{}, false
|
||||
}
|
||||
|
||||
if !ref.IsOriginal && isIconImage(ref.ImageData) {
|
||||
return StoredImage{}, false
|
||||
}
|
||||
|
||||
ext := extFromMime(ref.MimeType)
|
||||
if ext == "" {
|
||||
ext = filepath.Ext(ref.Filename)
|
||||
}
|
||||
if ext == "" {
|
||||
ext = ".png"
|
||||
}
|
||||
|
||||
fileName := uuid.New().String() + ext
|
||||
servingURL, saveErr := fileSvc.SaveBytes(ctx, ref.ImageData, tenantID, fileName, false)
|
||||
if saveErr != nil {
|
||||
log.Printf("WARN: failed to save image %s: %v", refPath, saveErr)
|
||||
return StoredImage{}, false
|
||||
}
|
||||
|
||||
stored := StoredImage{
|
||||
OriginalRef: refPath,
|
||||
ServingURL: servingURL,
|
||||
MimeType: ref.MimeType,
|
||||
}
|
||||
savedRefs[refPath] = stored
|
||||
return stored, true
|
||||
}
|
||||
|
||||
func extFromMime(mime string) string {
|
||||
switch mime {
|
||||
case "image/png":
|
||||
@@ -287,6 +314,10 @@ var imgHTMLDataURI = regexp.MustCompile(
|
||||
`(?i)<img\s[^>]*?src\s*=\s*["'](data:image/[^;]+;base64,[^"']+)["'][^>]*?/?\s*>`,
|
||||
)
|
||||
|
||||
var imgHTMLRelativeSrc = regexp.MustCompile(
|
||||
`(?i)<img\b([^>]*?)\bsrc\s*=\s*['"]([^'"]+)['"]([^>]*)>`,
|
||||
)
|
||||
|
||||
// ResolveHTMLDataURIImages finds <img src="data:image/*;base64,..."> tags in markdown,
|
||||
// decodes the images, stores them via fileSvc, and replaces each tag with a markdown
|
||||
// image reference using the storage URL.
|
||||
@@ -349,6 +380,41 @@ func (r *ImageResolver) ResolveHTMLDataURIImages(
|
||||
return markdown, images, nil
|
||||
}
|
||||
|
||||
// ResolveRelativeHTMLImages finds HTML <img> tags whose src points at a
|
||||
// relative document image reference, stores the corresponding bytes via
|
||||
// fileSvc, and replaces only the src attribute value with the storage URL.
|
||||
func (r *ImageResolver) ResolveRelativeHTMLImages(
|
||||
ctx context.Context,
|
||||
markdown string,
|
||||
fileSvc interfaces.FileService,
|
||||
tenantID uint64,
|
||||
refMap map[string]types.ImageRef,
|
||||
savedRefs map[string]StoredImage,
|
||||
) (updatedMarkdown string, images []StoredImage, err error) {
|
||||
matches := imgHTMLRelativeSrc.FindAllStringSubmatchIndex(markdown, -1)
|
||||
if len(matches) == 0 {
|
||||
return markdown, nil, nil
|
||||
}
|
||||
|
||||
for i := len(matches) - 1; i >= 0; i-- {
|
||||
m := matches[i]
|
||||
src := strings.TrimSpace(markdown[m[4]:m[5]])
|
||||
if src == "" || strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") ||
|
||||
isProviderScheme(src) || strings.HasPrefix(strings.ToLower(src), "data:image/") {
|
||||
continue
|
||||
}
|
||||
|
||||
stored, ok := r.saveReferencedImage(ctx, fileSvc, tenantID, src, refMap, savedRefs)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
images = appendStoredImage(images, stored)
|
||||
markdown = markdown[:m[4]] + stored.ServingURL + markdown[m[5]:]
|
||||
}
|
||||
|
||||
return markdown, images, nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Bare base64/data URI resolution (catch-all)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user