Guides

Chunk Markdown for RAG with Headings and Source Lines

Split Markdown into section records with heading paths and source-line ranges, and keep fenced code intact in a reproducible retrieval fixture.

Key takeaway

Create heading-aware JSONL records with source-line ranges and preserve fenced code.

On this page
  1. Create section records
  2. Put the heading in the searchable text
  3. Handle long sections and revisions

If two sections both say “exports,” a retrieved paragraph needs enough context to identify the plan it belongs to. Store the heading path and source line range with the paragraph. A parser can distinguish a real Markdown heading from a line beginning with a hash inside a code fence. markdown-it-py exposes block tokens and source maps for this purpose. markdown-it-py usage

Create section records

Save the Markdown fixture and section exporter. The fixture contains two plans with different retention limits, a nested exception, and a shell example.

python -m pip install markdown-it-py==4.3.0
python markdown-section-records.py markdown-section-fixture.md > sections.jsonl

On November 11, 2026, the script produced three records with body content:

Heading path after “Export handbook” Source lines Evidence
Standard plan 3 to 10 Seven days, plus the complete code fence
Enterprise plan 12 to 14 30 days
Enterprise plan / Exceptions 16 to 18 Administrator can shorten the period

The title remains in each heading path. It has no body of its own, so the exporter omits a title-only record. Line ranges are one-based and inclusive, so a person can inspect the exact passage in an editor.

Put the heading in the searchable text

For a retrieval index, prepend the heading path to the section text you send to the embedding or keyword index. Keep the path and line numbers as separate metadata too. Search for “Enterprise export retention” and verify that the retrieved evidence is tied to Enterprise rather than the similarly worded Standard section. For a question about exceptions, check that the nested heading still names the correct parent plan.

Keep a small labeled set of questions and expected source sections. A result that retrieves the wrong plan is an evidence selection problem even if the final answer is fluent. SQLite retrieval baseline

Handle long sections and revisions

The exporter gives one record per section body and preserves the source text, including fenced blocks. If a section is too long for the target model, split at paragraph or block boundaries and carry the heading path into each smaller record. Keep code and table blocks intact when the task depends on them. Measure token size with your target model’s tokenizer before choosing a fixed limit.

Store the source revision with each line range when you index multiple versions. Inserting a paragraph near the top of a file moves later lines. A citation is useful only when its line range refers to the same revision as the indexed text.

Sources & references

Written by Modelrovia Editorial. Our standards distinguish official claims from observed results.

How we research and test →

Tools in this article