Skip to content

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

fix(parse): handle nested markdown frontmatter (#5734)

Lusen Dong80a5a768806d
diff --git a/openviking/parse/parsers/markdown.py b/openviking/parse/parsers/markdown.py
index ecc2b968e..c4c98f4af 100644
--- a/openviking/parse/parsers/markdown.py
+++ b/openviking/parse/parsers/markdown.py
@@ -26,6 +26,8 @@ from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
+import yaml
+
from openviking.parse.accessors.mime_types import IANA_MEDIA_TYPE_TO_EXTENSION
from openviking.parse.base import NodeType, ParseResult, ResourceNode, create_parse_result
from openviking.parse.image_validation import is_valid_image
@@ -417,13 +419,12 @@ class MarkdownParser(BaseParser):
# Preserve the original uploaded filename when available instead of the temp
# upload name (e.g. upload_<uuid>.txt).
- doc_title = meta.get("frontmatter", {}).get(
- "title",
- explicit_name
- if explicit_name
- else _smart_stem(source_path)
- if source_path
- else "Document",
+ fallback_title = explicit_name or (_smart_stem(source_path) if source_path else "Document")
+ frontmatter_title = meta.get("frontmatter", {}).get("title")
+ doc_title = (
+ frontmatter_title
+ if isinstance(frontmatter_title, str) and frontmatter_title.strip()
+ else fallback_title
)
doc_name = self._sanitize_for_path(doc_title)
# Preserve code source filenames as the temp document directory.
@@ -604,13 +605,16 @@ class MarkdownParser(BaseParser):
frontmatter_text = match.group(1)
content_without_frontmatter = content[match.end() :]
- # Parse YAML (simple key: value parsing)
- frontmatter = {}
- for line in frontmatter_text.split("\n"):
- line = line.strip()
- if ":" in line:
- key, value = line.split(":", 1)
- frontmatter[key.strip()] = value.strip()
+ try:
+ # Keep scalar values as strings, matching the previous parser and keeping
+ # ParseResult metadata JSON-serializable, while honoring YAML nesting.
+ frontmatter = yaml.load(frontmatter_text, Loader=yaml.BaseLoader)
+ except yaml.YAMLError:
+ logger.warning("Ignoring malformed YAML frontmatter")
+ return content_without_frontmatter, None
+
+ if not isinstance(frontmatter, dict):
+ return content_without_frontmatter, None
return content_without_frontmatter, frontmatter
diff --git a/tests/parse/test_markdown_frontmatter.py b/tests/parse/test_markdown_frontmatter.py
index 847d1ced6..5a8542ed1 100644
--- a/tests/parse/test_markdown_frontmatter.py
+++ b/tests/parse/test_markdown_frontmatter.py
@@ -82,3 +82,25 @@ async def test_frontmatter_title_still_names_the_document_when_kept() -> None:
assert layout.doc_title == "Release Notes"
assert _written(layout) == [document]
+
+
+async def test_nested_frontmatter_title_does_not_rename_document() -> None:
+ document = """---
+date: 2026-10-08
+sources:
+ - id: cited
+ title: "Cited document"
+---
+# Topic
+"""
+
+ layout = await MarkdownParser()._compute_layout(
+ document, "viking://temp/test", source_path="/tmp/topic.md"
+ )
+
+ assert layout.doc_title == "topic"
+ assert layout.meta["frontmatter"] == {
+ "date": "2026-10-08",
+ "sources": [{"id": "cited", "title": "Cited document"}],
+ }