Skip to content

Commit 35b1f39

Browse files
committed
‼️ BREAKING: Add attrs argument to highligh callback
The highligh callback should not have the signature: `highlight(code, lang, attrs)`. Implements: markdown-it/markdown-it@866fba3
1 parent f0c96ae commit 35b1f39

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

markdown_it/renderer.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,19 @@ def fence(self, tokens: Sequence[Token], idx: int, options, env) -> str:
219219
token = tokens[idx]
220220
info = unescapeAll(token.info).strip() if token.info else ""
221221
langName = ""
222+
langAttrs = ""
222223

223224
if info:
224225
langName = info.split()[0]
226+
arr = info.split(maxsplit=1)
227+
langName = arr[0]
228+
if len(arr) == 2:
229+
langAttrs = arr[1]
225230

226231
if options.highlight:
227-
highlighted = options.highlight(token.content, langName) or escapeHtml(
228-
token.content
229-
)
232+
highlighted = options.highlight(
233+
token.content, langName, langAttrs
234+
) or escapeHtml(token.content)
230235
else:
231236
highlighted = escapeHtml(token.content)
232237

tests/test_port/test_misc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from markdown_it import MarkdownIt
2+
from markdown_it import presets
3+
4+
5+
def test_highlight_arguments():
6+
def highlight_func(str_, lang, attrs):
7+
assert lang == "a"
8+
assert attrs == "b c d"
9+
return "<pre><code>==" + str_ + "==</code></pre>"
10+
11+
conf = presets.commonmark.make()
12+
conf["options"]["highlight"] = highlight_func
13+
md = MarkdownIt(config=conf)
14+
assert md.render("``` a b c d \nhl\n```") == "<pre><code>==hl\n==</code></pre>\n"

0 commit comments

Comments
 (0)