Alternative: render from_markdown via markdown-it-py#61
Open
MattFisher wants to merge 4 commits into
Open
Conversation
f257c26 to
97ccc9e
Compare
Owner
|
Hi, thanks for this. I merged #56 first because it was the lower-risk incremental path for footnotes and kept the existing I like the direction here. Moving Markdown rendering onto
If you want to keep going with this PR, I think the next step is to rebase/merge main now that #56 is merged, regenerate the lockfile, and make the renderer preserve the current draft node shapes while still using |
Contributor
Author
|
Awesome I’ll do that
…On Sat, 27 Jun 2026 at 7:23 pm, Paolo Mazza ***@***.***> wrote:
*ma2za* left a comment (ma2za/python-substack#61)
<#61 (comment)>
Hi, thanks for this. I merged #56
<#56> first because it was
the lower-risk incremental path for footnotes and kept the existing
from_markdown output shape intact.
I like the direction here. Moving Markdown rendering onto markdown-it-py
is the cleaner long-term approach, but I don’t want to merge this PR as-is
yet because it changes a couple of things beyond the parser swap:
- poetry install / poetry check currently fails because the new
Markdown dependencies were added to pyproject.toml but the lockfile is
not in sync.
- Image rendering changes shape from the existing nested captionedImage
-> image2.attrs structure to a flattened captionedImage node. That may
be valid, but it is a compatibility change from the current library
behavior, so I’d prefer this PR preserve the existing image schema unless
we have verified the new one against Substack.
If you want to keep going with this PR, I think the next step is to
rebase/merge main now that #56
<#56> is merged, regenerate
the lockfile, and make the renderer preserve the current draft node shapes
while still using markdown-it-py for parsing.
—
Reply to this email directly, view it on GitHub
<#61?email_source=notifications&email_token=AAARHQUG3RZP6NVMHMGQXDD5B6G7ZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIOBRGYZTMNJZGM32M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-4816365937>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAARHQXARSPWDVUG7JV4Q6L5B6G7ZAVCNFSNUABFKJSXA33TNF2G64TZHM2TANZXGA4DGMBUHNEXG43VMU5TINZUHA4DEOJYG4YKC5QC>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
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.
Restore the schema used by post.py's captioned_image() method: captionedImage wraps an image2 node whose attrs dict carries src, alt, href and the remaining layout fields. Update mdrender tests to match. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
97ccc9e to
ce772b1
Compare
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
5538ee5 to
0ec3bd7
Compare
Strip the PROTOTYPE: prefix and "not wired for production" disclaimer from nodes.py and mdrender.py now that the direction is approved. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.