Alternative: render from_markdown via markdown-it-py#61
Open
MattFisher wants to merge 1 commit into
Open
Conversation
Replace the hand-rolled Markdown parser in from_markdown() with markdown-it-py plus the standard footnote plugin, and a small renderer (mdrender.py) that maps the syntax tree to Substack's node schema. Node construction is centralised in a new nodes.py module so the schema lives in one place. Footnotes (including multi-paragraph definitions) come from the footnote plugin. Adds end-to-end from_markdown feature tests covering every documented feature. Two intentional, CommonMark-correct behaviour changes vs the old parser: consecutive '>' lines are one paragraph (blank '>' lines split them), and unreferenced footnote definitions are dropped rather than appended.
f257c26 to
97ccc9e
Compare
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.
What this is
An alternative implementation of
Post.from_markdown()that delegates parsing to markdown-it-py (a CommonMark parser) plus the standard footnote plugin, with a small renderer (mdrender.py) that maps the syntax tree onto Substack's node schema. Node construction is centralised in a newnodes.pymodule so the (undocumented) schema lives in one place.Why I'm opening it
First off — this approach wasn't discussed beforehand, and I realise it's a bigger change than a typical PR, so I'm putting it up purely for your consideration; entirely your call on whether it's a direction you want to take.
I started out adding footnote support to the existing hand-rolled parser (#56). As that grew — footnotes, fenced/inline-code edge cases, multi-paragraph definitions — it started to feel like we were re-implementing a Markdown parser. So I prototyped this alternative to see how it compared, and it turned out significantly simpler:
from_markdown()drops from a ~270-line hand-rolled parser to a few lines delegating to the renderer (netpost.py≈ −215 lines).Trade-offs (flagging honestly)
markdown-it-pyandmdit-py-plugins(both widely used and well maintained). This is the main thing to weigh.>lines are one paragraph; blank>lines split paragraphs (standard CommonMark). The old parser made one paragraph per line.parse_inline()/tokens_to_text_nodes()remain as public helpers (still used by the manualfootnote()builder), butfrom_markdown()no longer relies on them.Tests
from_markdown/parse_inlinetests pass (the two semantic-difference tests above were updated).test_from_markdown_features.pywith end-to-end coverage of every feature listed in thefrom_markdown()docstring (headings 1–6, bold/italic/bold+italic/inline-code/strikethrough, links, images, linked images, code blocks with/without language, blockquotes, bullet/ordered lists, horizontal rules, paragraphs).Relationship to #56
This is an alternative to #56 — if you'd prefer this approach, #56 can be closed in its favour; if not, no harm done and #56 stands on its own.