Skip to content

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

fix(parse): keep content before the first top-level Markdown heading (#5427)

Mohammad Hijjawif596fa0ab5f2
diff --git a/openviking/parse/parsers/markdown.py b/openviking/parse/parsers/markdown.py
index e78b17e14..ecc2b968e 100644
--- a/openviking/parse/parsers/markdown.py
+++ b/openviking/parse/parsers/markdown.py
@@ -1449,9 +1449,11 @@ class MarkdownParser(BaseParser):
ops.append(_LayoutOp("write", f"{root_dir}/{doc_name}_{part_idx}.md", part))
return
- # Build virtual section list (pre-heading content as first virtual section)
+ # Build virtual section list (content before the first top-level heading,
+ # including any lower-level headings there, is the first virtual section)
sections = []
- first_heading_start = headings[0][0]
+ min_level = min(h[3] for h in headings)
+ first_heading_start = next(h[0] for h in headings if h[3] == min_level)
if first_heading_start > 0:
pre_content = content[:first_heading_start].strip()
if pre_content:
@@ -1467,7 +1469,6 @@ class MarkdownParser(BaseParser):
)
# Add real sections (top-level only for this pass)
- min_level = min(h[3] for h in headings)
i = 0
while i < len(headings):
if headings[i][3] == min_level:
diff --git a/tests/parse/test_markdown_no_split.py b/tests/parse/test_markdown_no_split.py
index bc48b696f..875cf89b7 100644
--- a/tests/parse/test_markdown_no_split.py
+++ b/tests/parse/test_markdown_no_split.py
@@ -113,6 +113,19 @@ async def test_long_markdown_default_mode_still_splits() -> None:
assert len(writes) > 1
+async def test_split_keeps_lower_level_headings_before_first_top_level_heading() -> None:
+ body = "\n\n".join(f"paragraph {index} " + "x" * 500 for index in range(10))
+ content = f"## Preface\n\nPREFACE {body}\n\n# Chapter 1\n\n{body}\n\n# Chapter 2\n\n{body}"
+ parser = MarkdownParser(config=ParserConfig(max_section_size=512, max_section_chars=2048))
+
+ layout = await parser._compute_layout(content, "viking://temp/test", resource_name="book")
+
+ written = "".join(op.content for op in layout.ops if op.kind == "write")
+ assert "## Preface" in written
+ assert "PREFACE" in written
+
+
@pytest.mark.asyncio
async def test_pdf_no_split_converts_to_one_complete_markdown(
monkeypatch: pytest.MonkeyPatch,