Skip to content

Commit

Permalink
👌 Throw an error if 3rd party plugin doesn't increment line or `pos…
Browse files Browse the repository at this point in the history
…` counters (previously infinite loop would likely occur)
  • Loading branch information
hukkin committed Nov 2, 2024
1 parent 26c1653 commit ddf1634
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion markdown_it/parser_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self) -> None:

def tokenize(self, state: StateBlock, startLine: int, endLine: int) -> None:
"""Generate tokens for input range."""
ok = False
rules = self.ruler.getRules("")
line = startLine
maxNesting = state.md.options.maxNesting
Expand All @@ -82,10 +83,19 @@ def tokenize(self, state: StateBlock, startLine: int, endLine: int) -> None:
# - update `state.line`
# - update `state.tokens`
# - return True
prevLine = state.line

for rule in rules:
if rule(state, line, endLine, False):
ok = rule(state, line, endLine, False)
if ok:
if prevLine >= state.line:
raise Exception("block rule didn't increment state.line")
break

# this can only happen if user disables paragraph rule
if not ok:
raise Exception("none of the block rules matched")

# set state.tight if we had an empty line before current tag
# i.e. latest empty line should not count
state.tight = not hasEmptyLines
Expand Down
5 changes: 5 additions & 0 deletions markdown_it/parser_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def skipToken(self, state: StateInline) -> None:
ok = rule(state, True)
state.level -= 1
if ok:
if pos >= state.pos:
raise Exception("inline rule didn't increment state.pos")
break
else:
# Too much nesting, just skip until the end of the paragraph.
Expand Down Expand Up @@ -117,11 +119,14 @@ def tokenize(self, state: StateInline) -> None:
# - update `state.pos`
# - update `state.tokens`
# - return true
prevPos = state.pos

if state.level < maxNesting:
for rule in rules:
ok = rule(state, False)
if ok:
if prevPos >= state.pos:
raise Exception("inline rule didn't increment state.pos")
break

if ok:
Expand Down
4 changes: 2 additions & 2 deletions markdown_it/port.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- package: markdown-it/markdown-it
version: 13.0.1
commit: e843acc9edad115cbf8cf85e676443f01658be08
date: May 3, 2022
commit: 49ca65bbef067c7dba63468a48c4aee3048607dc
date: Sep 26, 2023
notes:
- Rename variables that use python built-in names, e.g.
- `max` -> `maximum`
Expand Down

0 comments on commit ddf1634

Please sign in to comment.