Skip to content

Commit 618fca8

Browse files
committed
👌 IMPROVE: Link parsing performance
Don't try to parse link title if link wasn't found (implements: markdown-it/markdown-it@537ab89) Allow no more than 32 levels of nesting in `[]( (((((....))))) )` (implements: markdown-it/markdown-it@1910a3c)
1 parent 8b28f3d commit 618fca8

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

markdown_it/helpers/parse_link_destination.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result:
6464

6565
if code == 0x28: # /* ( */)
6666
level += 1
67+
if level > 32:
68+
return result
6769

6870
if code == 0x29: # /* ) */)
6971
if level == 0:

markdown_it/rules_inline/link.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
def link(state: StateInline, silent: bool):
88

99
href = ""
10+
title = ""
1011
label = None
1112
oldPos = state.pos
1213
maximum = state.posMax
@@ -56,31 +57,29 @@ def link(state: StateInline, silent: bool):
5657
else:
5758
href = ""
5859

59-
# [link]( <href> "title" )
60-
# ^^ skipping these spaces
61-
start = pos
62-
while pos < maximum:
63-
code = state.srcCharCode[pos]
64-
if not isSpace(code) and code != 0x0A:
65-
break
66-
pos += 1
67-
68-
# [link]( <href> "title" )
69-
# ^^^^^^^ parsing link title
70-
res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax)
71-
if pos < maximum and start != pos and res.ok:
72-
title = res.str
73-
pos = res.pos
74-
7560
# [link]( <href> "title" )
76-
# ^^ skipping these spaces
61+
# ^^ skipping these spaces
62+
start = pos
7763
while pos < maximum:
7864
code = state.srcCharCode[pos]
7965
if not isSpace(code) and code != 0x0A:
8066
break
8167
pos += 1
82-
else:
83-
title = ""
68+
69+
# [link]( <href> "title" )
70+
# ^^^^^^^ parsing link title
71+
res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax)
72+
if pos < maximum and start != pos and res.ok:
73+
title = res.str
74+
pos = res.pos
75+
76+
# [link]( <href> "title" )
77+
# ^^ skipping these spaces
78+
while pos < maximum:
79+
code = state.srcCharCode[pos]
80+
if not isSpace(code) and code != 0x0A:
81+
break
82+
pos += 1
8483

8584
if pos >= maximum or state.srcCharCode[pos] != 0x29: # /* ) */
8685
# parsing a valid shortcut link failed, fallback to reference

0 commit comments

Comments
 (0)