Repository navigation
Commit 0198992
Browse files
user01010111
and
jf nz
authored
📚 DOCS: Document renderer constructor differences (
#421 )
Document that `MarkdownIt` passes itself to `renderer_cls`, unlike the
argument-free JavaScript renderer constructor. Clarify the custom
constructor contract and that `RendererHTML` accepts but ignores an
optional parser argument.
Closes
#127 .
Validation: 981 tests passed; all pre-commit checks passed; strict docs
build passed on Python 3.10 with Sphinx 8.1.3. Sphinx 9 fails in the
unchanged API-doc configuration on both upstream and this branch.
Co-authored-by: jf nz <talon_usable_9k@icloud.com>
1 parent d6ccfdd commit 0198992
3 files changed
Lines changed: 16 additions & 0 deletions
File tree
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
@@ -1,5 +1,9 @@
|
1 | 1 | # Change Log
|
2 | 2 |
|
| 3 | +## Unreleased
|
| 4 | +
|
| 5 | +* 📚 Document the Python renderer constructor contract.
|
| 6 | +
|
3 | 7 | ## 4.2.0 - 2026-05-07
|
4 | 8 |
|
5 | 9 | * ✨ Add `make_fence_rule()` factory for configurable fence markers in [#394](https://github.com/executablebooks/markdown-it-py/pull/394)
|
|
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
@@ -333,6 +333,12 @@ md = MarkdownIt("commonmark", renderer_cls=MyRenderer)
|
333 | 333 | md.render("*a*")
|
334 | 334 | ```
|
335 | 335 |
|
| 336 | +`MarkdownIt` creates the renderer by calling `renderer_cls(self)`, passing the parser instance as a positional argument.
|
| 337 | +This differs from JavaScript, where the renderer is constructed with no arguments.
|
| 338 | +If you override `__init__` in a custom renderer, it must accept this parser argument; when subclassing `RendererHTML`, call `super().__init__(parser)` to initialise the render rules.
|
| 339 | +`RendererHTML` itself accepts an optional `parser=None` argument and does not use or store it, so `RendererHTML()` is also valid when rendering tokens directly.
|
| 340 | +The example above inherits this constructor unchanged.
|
| 341 | +
|
336 | 342 | Plugins can support multiple render types, using the `__output__` attribute (this is currently a Python only feature).
|
337 | 343 |
|
338 | 344 | ```{jupyter-execute}
|
|
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
@@ -35,6 +35,12 @@
|
35 | 35 | use `MarkdownIt("commonmark", {"html": False})` instead of `MarkdownIt({"html": False})`
|
36 | 36 | - The default configuration preset for `MarkdownIt` is "commonmark" not "default"
|
37 | 37 | - Allow custom renderer to be passed to `MarkdownIt`
|
| 38 | + - |
|
| 39 | + `MarkdownIt` constructs its renderer with `renderer_cls(self)`, passing the
|
| 40 | + parser instance as a positional argument; JavaScript constructs its renderer
|
| 41 | + with no arguments. Custom renderer constructors must accept this argument.
|
| 42 | + `RendererHTML` accepts an optional `parser=None` argument but does not use it,
|
| 43 | + so it can also be instantiated directly without a parser.
|
38 | 44 | - |
|
39 | 45 | change render method signatures
|
40 | 46 | `func(tokens, idx, options, env, slf)` to
|
|