Skip to content

Commit 4f8c8e0

Browse files
committed
Improve reference capture
1 parent 1e8c790 commit 4f8c8e0

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

markdown_it/port.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
in favour of using them directly through:
2626
2727
from markdown_it.common.normalize_url import normalizeLinkText
28-
28+
- |
29+
In markdown_it/rules_block/reference.py,
30+
record line range in state.env["references"] and add state.env["duplicate_refs"]
31+
This is to allow renderers to report on issues regarding references
2932
- Allow custom renderer to be passed to `MarkdownIt`
3033
- |
3134
change render method signatures

markdown_it/rules_block/reference.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,24 @@ def reference(state: StateBlock, startLine, _endLine, silent):
187187
if "references" not in state.env:
188188
state.env["references"] = {}
189189

190+
state.line = startLine + lines + 1
191+
190192
if label not in state.env["references"]:
191-
# TODO here (as is expected) references that have been previously defined
192-
# are ignored. But we should add a record of these (plus line numbers) to the
193-
# env, so that renderers may report warnings for these ignored references
194-
# rather than 'failing silently'
195-
state.env["references"][label] = AttrDict({"title": title, "href": href})
193+
state.env["references"][label] = AttrDict(
194+
{"title": title, "href": href, "map": [startLine, state.line]}
195+
)
196+
else:
197+
state.env.setdefault("duplicate_refs", []).append(
198+
AttrDict(
199+
{
200+
"title": title,
201+
"href": href,
202+
"label": label,
203+
"map": [startLine, state.line],
204+
}
205+
)
206+
)
196207

197208
state.parentType = oldParentType
198209

199-
state.line = startLine + lines + 1
200210
return True

tests/test_port/test_references.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
def test_ref_definitions():
77

88
md = MarkdownIt()
9-
src = "[a]: abc\n\n[b]: xyz"
9+
src = "[a]: abc\n\n[b]: xyz\n\n[b]: ijk"
1010
env = AttrDict()
1111
tokens = md.parse(src, env)
1212
assert tokens == []
1313
assert env == {
1414
"references": {
15-
"A": {"title": "", "href": "abc"},
16-
"B": {"title": "", "href": "xyz"},
17-
}
15+
"A": {"title": "", "href": "abc", "map": [0, 1]},
16+
"B": {"title": "", "href": "xyz", "map": [2, 3]},
17+
},
18+
"duplicate_refs": [{"href": "ijk", "label": "B", "map": [4, 5], "title": ""}],
1819
}
1920

2021

@@ -24,8 +25,8 @@ def test_use_existing_env():
2425
env = AttrDict(
2526
{
2627
"references": {
27-
"A": {"title": "", "href": "abc"},
28-
"B": {"title": "", "href": "xyz"},
28+
"A": {"title": "", "href": "abc", "map": [0, 1]},
29+
"B": {"title": "", "href": "xyz", "map": [2, 3]},
2930
}
3031
}
3132
)
@@ -126,8 +127,8 @@ def test_use_existing_env():
126127
]
127128
assert env == {
128129
"references": {
129-
"A": {"title": "", "href": "abc"},
130-
"B": {"title": "", "href": "xyz"},
131-
"C": {"title": "", "href": "ijk"},
130+
"A": {"title": "", "href": "abc", "map": [0, 1]},
131+
"B": {"title": "", "href": "xyz", "map": [2, 3]},
132+
"C": {"title": "", "href": "ijk", "map": [2, 3]},
132133
}
133134
}

0 commit comments

Comments
 (0)