Repository navigation
fix: keep non-mapping frontmatter blocks as content - #156
Closed
rahul05ranjan wants to merge 1 commit into
Closed
rahul05ranjan wants to merge 1 commit into
rahul05ranjan wants to merge 1 commit into
Conversation
A frontmatter block that parses to a non-mapping value (a plain string, a list) was silently discarded: loads() dropped both the parsed value and the source text between the delimiters, so a loads->dumps round-trip deleted part of the file. Most often this hits Markdown files that use --- thematic breaks near the top. Treat the whole file as content in that case, the same as the no-frontmatter path. Empty frontmatter (which parses to None) keeps its existing behavior. Fixes eyeseast#155 Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
Author
|
Saw this was closed — thanks for looking. If the behavior change itself was the concern (I read the empty-None case as intentional), I'm happy to drop this. If a different approach to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #155
What's wrong
parse()only used the frontmatter block when it parsed to a dict:Anything else — a plain string, a list, a number — was thrown away, along with the source text it came from. Nothing warned, so a
loads()→dumps()round-trip silently deleted part of the file:>>> frontmatter.loads("---\nsome markdown text\n---\nbody\n").content 'body'The most realistic case is a Markdown file using
---thematic breaks near the top: everything between the first two rules parsed as a YAML string, wasn't a dict, and vanished. A list-valued block (---\n- a\n- b\n---) is valid YAML with the same fate.The fix
Treat the whole file as content when the block parses to a non-mapping value — the same thing the no-frontmatter path already does. Empty frontmatter (which parses to
None) keeps its existing behavior, socheck()and the empty-frontmatter tests are unchanged.parse()'s docstring is updated to document this; tests are intests/unit_test.pyas suggested in CONTRIBUTING.Verification
test_non_mapping_frontmatter_is_content,test_list_frontmatter_is_content,test_thematic_break_file_roundtrip_loses_no_text) fail onmain, pass with this change.pytest . --doctest-modules --doctest-glob "README.md": 61 passed, 9 failed with the fix vs 58 passed, 9 failed onmain— the failures are all the pre-existing TOML-suite ones (this host has notomlpackage installed) and are identical on both runs; the +3 delta is exactly the new tests.black --check .clean;mypycouldn't run locally (host lacks the toolchain), happy to fix anything CI flags.AI tools
Yes — this issue (#155) and this PR were prepared with the help of an AI coding assistant (GLM, via OpenRouter): finding the repro, drafting the fix, tests and this write-up. The verification commands above were run directly against this branch and against
main, and their output is quoted as it was produced.