Skip to content

Make MarkdownSplitter an immutable class and remove the outline - #41

Merged
Gal-Gilor merged 2 commits into
mainfrom
refactor/markdown-splitter-class
Oct 18, 2026
Merged

Gal-Gilor merged 2 commits into
mainfrom
refactor/markdown-splitter-class

Conversation

@Gal-Gilor

@Gal-Gilor Gal-Gilor commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

What changed

  • MarkdownSplitter is a frozen, keyword-only dataclass with one field, max_level (1-6, default 6). MarkdownSplitter(3) raises TypeError, and setting max_level after creation raises FrozenInstanceError.
  • split_text and split_file are methods. split_text builds the sections in one pass with a stack of the enclosing sections.
  • Removed get_document_outline, OutlineNode and _create_sections_from_outline. The package exports MarkdownSplitter, Section and SectionMetadata.
  • Tests use a module-level splitter = MarkdownSplitter(). Removed the outline-only tests; the section tests cover the same behavior.
  • Updated the README, the CHANGELOG 0.3.0 entry (compared with 0.2.0) and the CI wheel check.

Why

  • One class with immutable options. One instance can be reused safely.
  • One representation of the tree: sections with parent_id, instead of both sections and an outline.
  • One pass over the headers, instead of building a tree and then flattening it.

Verification

  • Equivalence with main: 508 distinct inputs (every text main's test run passed to the splitter, every string literal in main's tests/test_splitter.py and conftest.py, README.md and CHANGELOG.md, the README examples, and 194 generated documents with preamble, BOM, CRLF, front matter and code fences). Each was compared with the default and with max_level 1-6, 3556 comparisons in all. [s.model_dump() for s in ...] was equal in every case.
  • ruff check and ruff format --check pass. 94 tests pass, splitter.py coverage is 98%, the same as main.
  • Doctests pass. The built wheel passes the CI import check. The tests pass with pydantic 2.0.2 on Python 3.12.
  • The README Python examples run and give the outputs shown.

MarkdownSplitter takes a keyword-only max_level in __init__, checks
that it is between 1 and 6, and has split_text, split_file and
get_document_outline methods that call the private module-level
helpers with self.max_level. The module-level functions are removed,
so the package has one API. The tests, the CI wheel check, the README
and the changelog use the class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FHW7nLqPiFJhQm6M1ARLi6
MarkdownSplitter is now a frozen, keyword-only dataclass with one field,
max_level. split_text builds the sections in one pass with a stack of the
enclosing sections, instead of building an outline and flattening it.
Results are unchanged.

Remove get_document_outline and OutlineNode. Sections with parent_id
already describe the tree, so the library keeps one representation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018jNL9rsodv5SMpDXVNMEia
@Gal-Gilor Gal-Gilor changed the title Bring back the MarkdownSplitter class as the public API Make MarkdownSplitter an immutable class and remove the outline Oct 18, 2026
@Gal-Gilor
Gal-Gilor merged commit 06ac8b8 into main Oct 18, 2026
5 checks passed
@Gal-Gilor
Gal-Gilor deleted the refactor/markdown-splitter-class branch October 18, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants