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
👌 IMPROVE: MarkdownIt config and documentation (#136)
Add additional configuration presets,
allow for options to be overridden in the `MarkdownIt` initialisation,
Add convenience methods to `SyntaxTreeNode`.
The `linkify` component requires that [linkify-it-py](https://github.com/tsutsu3/linkify-it-py) be installed (e.g. *via*`pip install markdown-it-py[linkify]`).
152
+
This allows URI autolinks to be identified, without the need for enclosing in `<>` brackets:
153
+
154
+
```{code-cell}
155
+
md = MarkdownIt("commonmark", {"linkify": True})
156
+
md.enable(["linkify"])
157
+
md.render("github.com")
158
+
```
159
+
100
160
### Plugins load
101
161
102
162
Plugins load collections of additional syntax rules and render methods into the parser
@@ -130,7 +190,6 @@ md.render(text)
130
190
131
191
## The Token Stream
132
192
133
-
134
193
+++
135
194
136
195
Before rendering, the text is parsed to a flat token stream of block level syntax elements, with nesting defined by opening (1) and closing (-1) attributes:
@@ -183,20 +242,42 @@ This dictionary can also be deserialized:
183
242
Token.from_dict(tokens[1].as_dict())
184
243
```
185
244
186
-
In some use cases `nest_tokens` may be useful, to collapse the opening/closing tokens into single tokens:
245
+
### Creating a syntax tree
246
+
247
+
```{versionchanged} 0.7.0
248
+
`nest_tokens` and `NestedTokens` are deprecated and replaced by `SyntaxTreeNode`.
249
+
```
250
+
251
+
In some use cases it may be useful to convert the token stream into a syntax tree,
252
+
with opening/closing tokens collapsed into a single token that contains children.
187
253
188
254
```{code-cell}
189
-
from markdown_it.token import nest_tokens
190
-
nested_tokens = nest_tokens(tokens)
191
-
[t.type for t in nested_tokens]
255
+
from markdown_it.tree import SyntaxTreeNode
256
+
257
+
md = MarkdownIt("commonmark")
258
+
tokens = md.parse("""
259
+
# Header
260
+
261
+
Here's some text and an image 
262
+
263
+
1. a **list**
264
+
265
+
> a *quote*
266
+
""")
267
+
268
+
node = SyntaxTreeNode.from_tokens(tokens)
269
+
print(node.pretty(indent=2, show_text=True))
192
270
```
193
271
194
-
This introduces a single additional class `NestedTokens`,
195
-
containing an `opening`, `closing` and `children`, which can be a list of mixed
0 commit comments