Skip to content

Commit 087dcd5

Browse files
committed
Add line numbers to footnote definitions
1 parent 8698805 commit 087dcd5

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

markdown_it/extensions/footnote/index.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,14 @@ def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
6565
return True
6666
pos += 1
6767

68-
if "footnotes" not in state.env:
69-
state.env["footnotes"] = {}
70-
if "refs" not in state.env["footnotes"]:
71-
# TODO here (as is expected) previously defined footnotes are ignored
72-
# we should though add a record of these (plus line numbers) to the env
73-
# so that renderers may report warnings for these ignored footnotes
74-
# rather than 'failing silently'
75-
state.env["footnotes"]["refs"] = {}
7668
label = state.src[start + 2 : pos - 2]
77-
state.env["footnotes"]["refs"][":" + label] = -1
69+
state.env.setdefault("footnotes", {}).setdefault("refs", {})[":" + label] = -1
7870

79-
token = Token("footnote_reference_open", "", 1)
80-
# TODO add record of line numbers to footnote definitions
81-
token.meta = {"label": label}
82-
token.level = state.level
71+
open_token = Token("footnote_reference_open", "", 1)
72+
open_token.meta = {"label": label}
73+
open_token.level = state.level
8374
state.level += 1
84-
state.tokens.append(token)
75+
state.tokens.append(open_token)
8576

8677
oldBMark = state.bMarks[startLine]
8778
oldTShift = state.tShift[startLine]
@@ -127,6 +118,8 @@ def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
127118
state.sCount[startLine] = oldSCount
128119
state.bMarks[startLine] = oldBMark
129120

121+
open_token.map = [startLine, state.line]
122+
130123
token = Token("footnote_reference_close", "", -1)
131124
state.level -= 1
132125
token.level = state.level
@@ -159,11 +152,8 @@ def footnote_inline(state: StateInline, silent: bool):
159152
# so all that's left to do is to call tokenizer.
160153
#
161154
if not silent:
162-
if "footnotes" not in state.env:
163-
state.env["footnotes"] = {}
164-
if "list" not in state.env["footnotes"]:
165-
state.env["footnotes"]["list"] = {}
166-
footnoteId = len(state.env["footnotes"]["list"])
155+
refs = state.env.setdefault("footnotes", {}).setdefault("list", {})
156+
footnoteId = len(refs)
167157

168158
tokens = []
169159
state.md.inline.parse(
@@ -173,10 +163,7 @@ def footnote_inline(state: StateInline, silent: bool):
173163
token = state.push("footnote_ref", "", 0)
174164
token.meta = {"id": footnoteId}
175165

176-
state.env["footnotes"]["list"][footnoteId] = {
177-
"content": state.src[labelStart:labelEnd],
178-
"tokens": tokens,
179-
}
166+
refs[footnoteId] = {"content": state.src[labelStart:labelEnd], "tokens": tokens}
180167

181168
state.pos = labelEnd + 1
182169
state.posMax = maximum

tests/test_footnote/test_footnote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_footnote_def():
2121
"tag": "",
2222
"nesting": 1,
2323
"attrs": None,
24-
"map": None,
24+
"map": [0, 1],
2525
"level": 0,
2626
"children": None,
2727
"content": "",

tests/test_port/test_fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_title(line, title, input, expected):
2222
read_fixture_file(FIXTURE_PATH.joinpath("commonmark_extras.md")),
2323
)
2424
def test_commonmark_extras(line, title, input, expected):
25-
if line in [54, 74, 88]:
25+
if line in [74, 88]:
2626
# TODO fix failing url escaping tests
2727
pytest.skip("url escaping")
2828
md = MarkdownIt("commonmark")

0 commit comments

Comments
 (0)