Skip to content

Commit 3d6cc62

Browse files
authored
🐛 FIX: Exception when doc ends in heading/blockquote marker (#84)
1 parent 3a625ac commit 3d6cc62

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

markdown_it/rules_block/blockquote.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
2323

2424
# check the block quote marker
2525
if state.srcCharCode[pos] != 0x3E: # /* > */
26-
pos += 1
2726
return False
2827
pos += 1
2928

@@ -39,16 +38,21 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
3938
- (state.bMarks[startLine] + state.tShift[startLine])
4039
)
4140

41+
try:
42+
second_char_code = state.srcCharCode[pos]
43+
except IndexError:
44+
second_char_code = None
45+
4246
# skip one optional space after '>'
43-
if state.srcCharCode[pos] == 0x20: # /* space */
47+
if second_char_code == 0x20: # /* space */
4448
# ' > test '
4549
# ^ -- position start of line here:
4650
pos += 1
4751
initial += 1
4852
offset += 1
4953
adjustTab = False
5054
spaceAfterMarker = True
51-
elif state.srcCharCode[pos] == 0x09: # /* tab */
55+
elif second_char_code == 0x09: # /* tab */
5256
spaceAfterMarker = True
5357

5458
if (state.bsCount[startLine] + offset) % 4 == 3:

markdown_it/rules_block/heading.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
2727
# count heading level
2828
level = 1
2929
pos += 1
30-
ch = state.srcCharCode[pos]
30+
try:
31+
ch = state.srcCharCode[pos]
32+
except IndexError:
33+
ch = None
3134
# /* # */
3235
while ch == 0x23 and pos < maximum and level <= 6:
3336
level += 1
3437
pos += 1
35-
ch = state.srcCharCode[pos]
38+
try:
39+
ch = state.srcCharCode[pos]
40+
except IndexError:
41+
ch = None
3642

3743
if level > 6 or (pos < maximum and not isSpace(ch)):
3844
return False
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
3+
from markdown_it import MarkdownIt
4+
5+
6+
@pytest.mark.parametrize(
7+
"input,expected",
8+
[
9+
("#", "<h1></h1>\n"),
10+
("###", "<h3></h3>\n"),
11+
("` `", "<p><code> </code></p>\n"),
12+
("``````", "<pre><code></code></pre>\n"),
13+
("-", "<ul>\n<li></li>\n</ul>\n"),
14+
("1.", "<ol>\n<li></li>\n</ol>\n"),
15+
(">", "<blockquote></blockquote>\n"),
16+
("---", "<hr />\n"),
17+
("<h1></h1>", "<h1></h1>"),
18+
("p", "<p>p</p>\n"),
19+
("[reference]: /url", ""),
20+
],
21+
)
22+
def test_no_end_newline(input, expected):
23+
md = MarkdownIt()
24+
text = md.render(input)
25+
assert text == expected

0 commit comments

Comments
 (0)