🔧 Clarify that add_render_rule's name must match a token type - #432
Merged
chrisjsewell merged 2 commits intoSep 17, 2026
Merged
chrisjsewell merged 2 commits into
chrisjsewell merged 2 commits into
Conversation
Fixes executablebooks#308. The docstring gave no indication of what `name` should be, which led to confusion (see the issue): a rule silently never fires if `name` doesn't match a token's `.type`, since the renderer just looks up `self.rules.get(token.type, ...)`. A runtime warning was considered (as suggested in the issue), but `self.renderer.rules` only contains methods for token types that already have a dedicated renderer method (e.g. `fence`, `code_inline`). Most token types (`paragraph_open`, `heading_open`, ...) fall back to the generic `renderToken` and are simply not keys in that dict, so "name not in self.renderer.rules" would produce a false warning for the single most common use of this method: adding a custom renderer for a standard token type that didn't previously have one. There is no other registry of "valid" token type names to check against, since plugins are free to introduce new token types. Documenting the actual contract is the fix that doesn't add noise. Co-Authored-By: Claude Sonnet 5 <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.
Summary
Fixes #308.
MarkdownIt.add_render_rule's docstring gave no indication of whatnameshould be, which led to confusion (as described in the issue): a rule silently never fires ifnamedoesn't match a token's.type, sinceRendererHTML.render/renderInlinejust doself.rules.get(token.type, ...).Why not the suggested runtime warning?
The issue suggests logging a warning when
nameisn't a "preset rule". I implemented and tested that first, butself.renderer.rules(populated inRendererHTML.__init__viainspect.getmembers) only contains methods for token types that already have a dedicated renderer method (e.g.fence,code_inline,image). Most token types —paragraph_open,heading_open, etc. — have no such method and fall back to the genericrenderToken, so they're simply not keys in that dict.That means a
name not in self.renderer.rulescheck produces a false warning for the single most common use of this method: adding a custom renderer for a standard token type that didn't previously have a dedicated one (verified with a quick test:md.add_render_rule("paragraph_open", ...)would warn even though"paragraph_open"is a perfectly valid token type). There's also no other registry of "valid" token type names to check against, since plugins are free to introduce their own.Given that, documenting the actual contract (
namemust equal a token's.type) is the fix that resolves the reported confusion without adding noisy false positives. Happy to revisit a runtime check if maintainers have a different validation approach in mind.Test plan
pytest tests/test_api/test_main.pybefore and after: 20 passed both times (2 pre-existing unrelated failures due to the optionallinkify-it-pydependency not being installed in this sandbox).🤖 Generated with Claude Code