Skip to content

Commit 6346ebd

Browse files
authored
🐛 FIX: container plugin \w no newline (#43)
Fixed an `IndexError` when there is no newline on the closing tag line.
1 parent a4159f0 commit 6346ebd

File tree

5 files changed

+145
-2
lines changed

5 files changed

+145
-2
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ exclude docstring.fmt.mustache
1212
exclude .flake8
1313
exclude .circleci
1414
exclude .circleci/config.yml
15+
exclude codecov.yml
1516

1617
include LICENSE
1718
include LICENSE.markdown-it

codecov.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 95%
6+
threshold: 0.2%
7+
patch:
8+
default:
9+
target: 80%
10+
threshold: 0.2%

markdown_it/extensions/container/index.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool
4242
# Check out the rest of the marker string
4343
pos = start + 1
4444
while pos <= maximum:
45-
if marker_str[(pos - start) % marker_len] != state.src[pos]:
45+
try:
46+
character = state.src[pos]
47+
except IndexError:
48+
break
49+
if marker_str[(pos - start) % marker_len] != character:
4650
break
4751
pos += 1
4852

@@ -88,7 +92,11 @@ def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool
8892

8993
pos = start + 1
9094
while pos <= maximum:
91-
if marker_str[(pos - start) % marker_len] != state.src[pos]:
95+
try:
96+
character = state.src[pos]
97+
except IndexError:
98+
break
99+
if marker_str[(pos - start) % marker_len] != character:
92100
break
93101
pos += 1
94102

tests/test_plugins/test_container.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ def test_plugin_parse(data_regression):
2222
data_regression.check([t.as_dict() for t in tokens])
2323

2424

25+
def test_no_new_line_issue(data_regression):
26+
"""Fixed an IndexError when no newline on final line."""
27+
md = MarkdownIt().use(container_plugin, "name")
28+
tokens = md.parse(
29+
dedent(
30+
"""\
31+
::: name
32+
*content*
33+
:::"""
34+
)
35+
)
36+
data_regression.check([t.as_dict() for t in tokens])
37+
38+
2539
FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures", "container.md")
2640

2741

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
- attrs: null
2+
block: true
3+
children: null
4+
content: ''
5+
hidden: false
6+
info: ' name'
7+
level: 0
8+
map:
9+
- 0
10+
- 2
11+
markup: ':::'
12+
meta: {}
13+
nesting: 1
14+
tag: div
15+
type: container_name_open
16+
- attrs: null
17+
block: true
18+
children: null
19+
content: ''
20+
hidden: false
21+
info: ''
22+
level: 1
23+
map:
24+
- 1
25+
- 2
26+
markup: ''
27+
meta: {}
28+
nesting: 1
29+
tag: p
30+
type: paragraph_open
31+
- attrs: null
32+
block: true
33+
children:
34+
- attrs: null
35+
block: false
36+
children: null
37+
content: ''
38+
hidden: false
39+
info: ''
40+
level: 0
41+
map: null
42+
markup: '*'
43+
meta: {}
44+
nesting: 1
45+
tag: em
46+
type: em_open
47+
- attrs: null
48+
block: false
49+
children: null
50+
content: content
51+
hidden: false
52+
info: ''
53+
level: 1
54+
map: null
55+
markup: ''
56+
meta: {}
57+
nesting: 0
58+
tag: ''
59+
type: text
60+
- attrs: null
61+
block: false
62+
children: null
63+
content: ''
64+
hidden: false
65+
info: ''
66+
level: 0
67+
map: null
68+
markup: '*'
69+
meta: {}
70+
nesting: -1
71+
tag: em
72+
type: em_close
73+
content: '*content*'
74+
hidden: false
75+
info: ''
76+
level: 2
77+
map:
78+
- 1
79+
- 2
80+
markup: ''
81+
meta: {}
82+
nesting: 0
83+
tag: ''
84+
type: inline
85+
- attrs: null
86+
block: true
87+
children: null
88+
content: ''
89+
hidden: false
90+
info: ''
91+
level: 1
92+
map: null
93+
markup: ''
94+
meta: {}
95+
nesting: -1
96+
tag: p
97+
type: paragraph_close
98+
- attrs: null
99+
block: true
100+
children: null
101+
content: ''
102+
hidden: false
103+
info: ''
104+
level: 0
105+
map: null
106+
markup: ':::'
107+
meta: {}
108+
nesting: -1
109+
tag: div
110+
type: container_name_close

0 commit comments

Comments
 (0)