Repository navigation
fix(rag): remove inline images and keep the text between them - #45202
TINGyu123644 wants to merge 2 commits into
Conversation
remove_images only knew the wiki form `![[name]]`, so a standard `` survived even with remove_images on, and its greedy `(.*)` deleted everything between two wiki images. remove_hyperlinks also matched the `[alt](url)` part of an image, so with image removal off a link sweep left a stray `!` and destroyed the image. Match both image forms with non-greedy character classes, and skip images when unwrapping links. Adds unit tests for both reports. Fixes langgenius#45183 Fixes langgenius#45184
GYJ99
left a comment
There was a problem hiding this comment.
Thanks for consolidating #45183 and #45184 and adding regressions for both image syntaxes. I found one valid Markdown URL shape that the new inline-image regex does not consume completely: destinations containing balanced parentheses.
With the exact new expression re.sub(r"!\\[[^\\]]*\\]\\([^)]*\\)", "", text), an isolated Python check gives:
input: before .png) after
actual: before .png) after
The first ) in (v2) terminates the regex before the actual closing parenthesis, so remove_images=True leaves a broken filename fragment in the indexed content. Another example, /render.png), leaves /render.png). Both forms are legitimate Markdown destinations.
Could you add a regression for a parenthesized image destination with meaningful surrounding prose and use balanced-destination handling (or a Markdown-aware parser) rather than [^)]*? The new image-removal behavior should not leave URL debris. I reproduced only the regex behavior with Python, not the full Dify extractor tests.
Also, #45185 and #45186 overlap these same helper functions; coordination before merging would avoid competing fixes. AI-assisted code review with ChatGPT.
Review of this PR found that the inline-image matcher stopped at the first ")" in the destination, so a destination like .png) left a ".png)" fragment in the indexed text. Link destinations may contain one level of balanced parentheses (and backslash escapes), which a [^)]* class cannot express. Match a destination as a sequence of plain characters, escapes, or one balanced parenthesized group - in both remove_images and remove_hyperlinks - and add regressions for parenthesized destinations with surrounding prose.
|
Replying to @GYJ99's review (balanced parentheses in image destinations): Thanks for the careful review - you found a real gap. The inline-image matcher stopped at the first Fixed in 5006de6 with balanced-destination handling instead of Regressions added (all fail on the previous commit, pass now - verified locally with the shimmed extractor test suite, 17 -> 20 passed):
On coordination: #45185 and #45186 (Zhi-Xiang-Guo, opened earlier the same day) do overlap these helpers - this PR intentionally consolidates both halves (#45183 + #45184) plus the balanced-destination handling. Happy to close this one if maintainers prefer landing #45185/#45186; just say the word. |
What this fixes
Both reports point at the same two helpers in
api/core/rag/extractor/markdown_extractor.py:remove_images=True, a standardwas left in the text, while![[one.png]] useful text ![[two.png]]lost the words between the images.remove_imagesonly knew the wiki form![[...]]and matched it with a greedy(.*).remove_hyperlinks=Trueand image removal off,became!diagram: the link pattern also matched the[alt](url)half of the image.How
remove_imagesnow matches both forms with non-greedy character classes, so only the image markup is dropped and the surrounding text stays:remove_hyperlinksskips anything preceded by!, so an image is never half-unwrapped:The replacement for a link is still its text, as before.
Tests
Four cases added to
api/tests/unit_tests/core/rag/extractor/test_markdown_extractor.py, covering both issues at helper level and throughextract()with a real temporary file.Only that unit module was run locally (
pytest, API deps installed viauv); the rest of the suite is left to CI.The change and the tests were drafted with an AI assistant and reviewed by me before opening this PR.