Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(parser): handle angle-bracket image paths in Markdown (#4533)

ShaoZegangByte0b3b407c9df6
diff --git a/openviking/parse/parsers/markdown.py b/openviking/parse/parsers/markdown.py
index 562cb7e60..8f269e57b 100644
--- a/openviking/parse/parsers/markdown.py
+++ b/openviking/parse/parsers/markdown.py
@@ -949,7 +949,7 @@ class MarkdownParser(BaseParser):
allowed root, otherwise None
"""
try:
- path = Path(path_str)
+ path = Path(self._unwrap_link_destination(path_str))
# Reject absolute paths: they can point anywhere on the host
if path.is_absolute():
@@ -1024,7 +1024,15 @@ class MarkdownParser(BaseParser):
return is_valid_image(image_bytes, source_path)
@staticmethod
- def _is_remote_uri(path: str) -> bool:
+ def _unwrap_link_destination(path: str) -> str:
+ """Return the path represented by a Markdown ``<destination>``."""
+ path = path.strip()
+ if len(path) >= 2 and path.startswith("<") and path.endswith(">"):
+ return path[1:-1]
+ return path
+
+ @classmethod
+ def _is_remote_uri(cls, path: str) -> bool:
"""
Check if a path is a remote URI.
@@ -1035,7 +1043,7 @@ class MarkdownParser(BaseParser):
True if path starts with http://, https://, viking://, data:, or ftp://
"""
remote_prefixes = ("http://", "https://", "viking://", "data:", "ftp://")
- return path.startswith(remote_prefixes)
+ return cls._unwrap_link_destination(path).startswith(remote_prefixes)
@staticmethod
def _deduplicate_filename(filename: str, used_names: set[str]) -> str:
diff --git a/tests/parse/test_markdown_link_rewrite.py b/tests/parse/test_markdown_link_rewrite.py
index f878a2f1b..e8d80c785 100644
--- a/tests/parse/test_markdown_link_rewrite.py
+++ b/tests/parse/test_markdown_link_rewrite.py
@@ -553,6 +553,27 @@ class TestImageLinkSplit:
assert any(u.endswith("/photo.png") for u in fake.files), fake.files
assert any(u.endswith(".image_mappings.json") for u in fake.files), fake.files
+ async def test_angle_bracket_image_destination_with_spaces_is_ingested(self, tmp_path: Path):
+ kb = tmp_path / "kb"
+ image_dir = kb / "resource name" / "images"
+ image_dir.mkdir(parents=True)
+ _write_valid_png(image_dir / "photo.png")
+ src = kb / "page.md"
+ image_ref = "<resource name/images/photo.png>"
+ src.write_text(f"![p]({image_ref})", encoding="utf-8")
+
+ fake = FakeVikingFS()
+ with patch.object(BaseParser, "_get_viking_fs", return_value=fake):
+ await MarkdownParser().parse(
+ str(src), enable_link_rewrite=True, link_rewrite_root=str(kb)
+ )
+
+ md = [_decode(c) for u, c in fake.files.items() if u.endswith(".md")]
+ assert md and f"![p]({image_ref})" in md[0], fake.files
+ assert any(u.endswith("/photo.png") for u in fake.files), fake.files
+ mapping = [_decode(c) for u, c in fake.files.items() if u.endswith(".image_mappings.json")]
+ assert mapping and image_ref in mapping[0], fake.files
+
async def test_image_outside_base_dir_depth_adjusted(self, tmp_path: Path):
# The md lives in kb/sub; the image lives in kb/img — outside base_dir
# (= the md's own directory) but inside the import root. #2429 cannot