You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Using_the_api.md
+112-1Lines changed: 112 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -202,5 +202,116 @@ nested_tokens[0]
202
202
203
203
## Renderers
204
204
205
+
<!-- #region -->
206
+
After the token stream is generated, it's passed to a [renderer](https://github.com/ExecutableBookProject/markdown-it-py/tree/master/markdown_it/renderer.py).
207
+
It then plays all the tokens, passing each to a rule with the same name as token type.
205
208
206
-
Todo ...
209
+
Renderer rules are located in `md.renderer.rules` and are simple functions
210
+
with the same signature:
211
+
212
+
```python
213
+
deffunction(renderer, tokens, idx, options, env):
214
+
return htmlResult
215
+
```
216
+
<!-- #endregion -->
217
+
218
+
You can inject render methods into the instantiated render class.
This is a slight change to the JS version, where the renderer argument is at the end.
231
+
Also `add_render_rule` method is specific to Python, rather than adding directly to the `md.renderer.rules`, this ensures the method is bound to the renderer.
232
+
233
+
234
+
You can also subclass a render and add the method there:
-[Live demo](https://markdown-it.github.io/) - type your text and click `debug` tab.
74
74
75
75
@@ -95,90 +95,67 @@ and tried to do something yourself. We never reject with help to real developers
95
95
96
96
## Renderer
97
97
98
-
After token stream is generated, it's passed to a [renderer](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js).
98
+
After the token stream is generated, it's passed to a [renderer](https://github.com/ExecutableBookProject/markdown-it-py/tree/master/markdown_it/renderer.py).
99
99
It then plays all the tokens, passing each to a rule with the same name as token type.
100
100
101
101
Renderer rules are located in `md.renderer.rules[name]` and are simple functions
102
102
with the same signature:
103
103
104
-
```js
105
-
function (tokens, idx, options, env, renderer) {
106
-
//...
107
-
return htmlResult;
108
-
}
104
+
```python
105
+
deffunction(renderer, tokens, idx, options, env):
106
+
return htmlResult
109
107
```
110
108
111
109
In many cases that allows easy output change even without parser intrusion.
112
110
For example, let's replace images with vimeo links to player's iframe:
Also you can change output directly in [renderer](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js) for many simple cases.
179
+
Also you can change output directly in [renderer](https://github.com/ExecutableBookProject/markdown-it-py/tree/master/markdown_it/renderer.py) for many simple cases.
0 commit comments