Skip to content

Commit 99cde43

Browse files
authored
✨ NEW: Add inline_definitions option (#187)
This option allows for `definition` token to be inserted into the token stream, at the point where the definition is located in the source text (in addition to the standard extraction to `env`). It is not currently part of the Markdown-It JS implementation, but is useful for cases where one wishes to capture a "loseless" syntax tree of the parsed Markdown (in conjunction with the `store_labels` option).
1 parent 448ea83 commit 99cde43

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

markdown_it/rules_block/reference.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ def reference(state: StateBlock, startLine, _endLine, silent):
187187

188188
state.line = startLine + lines + 1
189189

190+
# note, this is not part of markdown-it JS, but is useful for renderers
191+
if state.md.options.get("inline_definitions", False):
192+
token = state.push("definition", "", 0)
193+
token.meta = {
194+
"id": label,
195+
"title": title,
196+
"url": href,
197+
"label": string[1:labelEnd],
198+
}
199+
token.map = [startLine, state.line]
200+
190201
if label not in state.env["references"]:
191202
state.env["references"][label] = {
192203
"title": title,

tests/test_port/test_references.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ def test_store_labels(data_regression):
4343
src = "[a]\n\n![a]\n\n[a]: ijk"
4444
tokens = md.parse(src)
4545
data_regression.check([token.as_dict() for token in tokens])
46+
47+
48+
def test_inline_definitions(data_regression):
49+
md = MarkdownIt()
50+
md.options["inline_definitions"] = True
51+
src = '[a]: url "title"\n- [a]: url "title"'
52+
tokens = md.parse(src)
53+
data_regression.check([token.as_dict() for token in tokens])
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
- attrs: null
2+
block: true
3+
children: null
4+
content: ''
5+
hidden: false
6+
info: ''
7+
level: 0
8+
map:
9+
- 0
10+
- 1
11+
markup: ''
12+
meta:
13+
id: A
14+
label: a
15+
title: title
16+
url: url
17+
nesting: 0
18+
tag: ''
19+
type: definition
20+
- attrs: null
21+
block: true
22+
children: null
23+
content: ''
24+
hidden: false
25+
info: ''
26+
level: 0
27+
map:
28+
- 1
29+
- 2
30+
markup: '-'
31+
meta: {}
32+
nesting: 1
33+
tag: ul
34+
type: bullet_list_open
35+
- attrs: null
36+
block: true
37+
children: null
38+
content: ''
39+
hidden: false
40+
info: ''
41+
level: 1
42+
map:
43+
- 1
44+
- 2
45+
markup: '-'
46+
meta: {}
47+
nesting: 1
48+
tag: li
49+
type: list_item_open
50+
- attrs: null
51+
block: true
52+
children: null
53+
content: ''
54+
hidden: false
55+
info: ''
56+
level: 2
57+
map:
58+
- 1
59+
- 2
60+
markup: ''
61+
meta:
62+
id: A
63+
label: a
64+
title: title
65+
url: url
66+
nesting: 0
67+
tag: ''
68+
type: definition
69+
- attrs: null
70+
block: true
71+
children: null
72+
content: ''
73+
hidden: false
74+
info: ''
75+
level: 1
76+
map: null
77+
markup: '-'
78+
meta: {}
79+
nesting: -1
80+
tag: li
81+
type: list_item_close
82+
- attrs: null
83+
block: true
84+
children: null
85+
content: ''
86+
hidden: false
87+
info: ''
88+
level: 0
89+
map: null
90+
markup: '-'
91+
meta: {}
92+
nesting: -1
93+
tag: ul
94+
type: bullet_list_close

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ usedevelop = true
1313
extras =
1414
linkify
1515
testing
16-
commands = pytest tests/ {posargs}
16+
commands = pytest {posargs:tests/}
1717

1818
[testenv:py{36,37,38,39,310}-plugins]
1919
extras = testing

0 commit comments

Comments
 (0)