Skip to main content

markdown-parser-py

Parse a Markdown document into a heading tree, edit its sections, and write it back to Markdown. Unedited documents round-trip exactly as Python strings, including their preamble, heading syntax, blank lines, and line endings.

The package supports Python 3.10 and later. Its own block scanner identifies section headings and their surrounding Markdown.

Installation

pip install markdown-parser-py

For development:

git clone https://github.com/VarunGumma/markdown-parser-py
cd markdown-parser-py
pip install -e .

Parse and edit a document

from markdown_parser import MarkdownTree

tree = MarkdownTree()
tree.parse("""Introduction before the first heading.

# Guide
A short guide.

## Installation
Run `pip install example`.
""")

installation = tree.find_node_by_path("Guide.Installation")
assert installation is not None
print(installation.content)

tree.add_section("Guide", "Usage", "Import and use the package.")
print(tree.dump())
tree.visualize()

parse() replaces the tree's previous contents. The synthetic root has level 0; headings have levels 1–6. Every node has a title, level, content list, children list, and parent. A heading belongs to the nearest preceding heading with a smaller level, even if levels are skipped. Text before the first heading belongs to the root.

A node's title is Markdown source, including inline formatting, rather than rendered plain text. ATX opening and closing markers and Setext underlines are excluded. Titles can be empty. Section content excludes child sections. Parsed content entries omit surrounding blank lines for convenient access, while unchanged source is retained separately for serialization. add_content() appends the supplied text as a new content entry; entries are joined with a newline when edited content is written.

Find sections

Dot paths select titles from the root downward. A missing path returns None:

node = tree.find_node_by_path("Guide.Installation")

Use a sequence for a title containing a literal dot, or for a top-level section named ROOT:

node = tree.find_node_by_path(["Version 1.0", "Installation"])

find_nodes_by_path(path) returns all complete matches in document order. find_node_by_path(path) and mutators taking a path choose the first complete match. To edit a particular duplicate, pass the returned node directly:

matches = tree.find_nodes_by_path(["Repeated heading"])
if len(matches) > 1:
    tree.add_section(matches[1], "Child", "Text")
    tree.remove_section(matches[0])

An empty string or empty sequence refers to the root. For add_section() and attach_subtree() only, the string "ROOT" is also a root shortcut; use ["ROOT"] for a heading with that title. remove_section() returns a boolean, and add_section() returns the new node or None when the parent is missing.

Copy sections between documents

base = MarkdownTree()
base.parse("# Guide\nGuide text.\n")

appendix = MarkdownTree()
appendix.parse("# Extra\nMore text.\n\n## Details\nDetails here.\n")

# Copy every top-level section below Guide.
attached = base.attach_subtree("Guide", appendix)

# Or copy just one section, selected by path or node handle.
# attached = base.attach_subtree("Guide", appendix, ["Extra", "Details"])

Copies are independent of the source. Each copied root is placed one heading level below the target; differences in level within its subtree are preserved. Copying all sections copies the root's children, not source preamble text. Pass that text explicitly if the composed document needs it. Attaching a tree to itself is supported.

Markdown has only six heading levels. If a section cannot fit at the destination, add_section() or attach_subtree() raises ValueError rather than silently flattening it. Attachment validates all requested sections before changing the destination. max_level can set a stricter upper bound from 1 to 6. A multiline Setext title cannot in general be relevelled without changing its inline meaning; such an attachment raises ValueError.

At the lower level, MarkdownNode.add_child() moves a node, updates its parent, and rejects cycles and invalid heading levels; it does not relevel the node. remove_child() detaches it. copy_with_level_delta() rejects levels outside its allowed range instead of clamping them. Directly editing children, parent, or level can bypass these checks.

Markdown behavior and validation

The parser recognizes top-level ATX and Setext headings. Heading-like text inside fenced or indented code, HTML blocks, block quotes, and lists remains section content. Markdown inside a container is not represented as a separate section tree. The scanner aims to follow CommonMark block rules when identifying headings; it does not render Markdown or enable GitHub-specific extensions such as tables. It is not certified as a complete CommonMark implementation, and unusual combinations of nested blocks may still expose edge cases.

A complete initial block starting with --- and ending with --- or ... is treated as front matter and kept in the root content. This convention is ambiguous with ordinary Markdown. To disable this convention and scan the document without special front matter handling, use:

tree.parse(markdown_text, front_matter=False)

dump() preserves original source for unchanged headings and content, and regenerates only edited parts. File decoding and newline handling are the caller's responsibility: for example, read with open(path, encoding="utf-8", newline="") if preserving file line endings matters.

By default, dump() validates the tree and, after edits, reparses the result to check that its section titles and levels still match the tree. This catches cases such as adding a heading after an unclosed code fence or adding heading syntax inside content. It raises ValueError if the hierarchy cannot be represented. dump(validate=False) skips this check when the caller explicitly prefers to handle that risk. Validation uses this same scanner, so it cannot detect a mistake shared by parsing and validation. It checks heading structure, not every aspect of rendered Markdown: moving content can change the meaning of reference links, HTML, or other context-dependent constructs.

Metadata

Release files for markdown-parser-py 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markdown-parser-py 2.0.0
File Size Uploaded Provenance Actions
markdown_parser_py-2.0.0.tar.gz 18.0 kB With provenance Details for markdown_parser_py-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for markdown-parser-py 2.0.0
File Interpreter ABI Platform Actions
markdown_parser_py-2.0.0-py3-none-any.whl Python 3 none any Details for markdown_parser_py-2.0.0-py3-none-any.whl

Total release size: 34.8 kB

Release files / markdown_parser_py-2.0.0.tar.gz

Download URL markdown_parser_py-2.0.0.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
37bbfb9c7ed28e9009ba84b000cf19692eadb0d2fa1b07be5e5bca4d79fc2b84
BLAKE2b-256 checksum
How to use checksums
36b5503be42719aeef99879a1db3b03bbbb6bc192d7f09144d74aa8aeee2088a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Release files / markdown_parser_py-2.0.0-py3-none-any.whl

Download URL markdown_parser_py-2.0.0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24537b2345e9196607d2395fe2ec31d1656f84b5d1039c678b6234d27c90ebfb
BLAKE2b-256 checksum
How to use checksums
59f17d0ab7325ee4646d005156e93664d213147a77d524081a1afa4e59a307aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Security for markdown-parser-py 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for markdown_parser_py-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page