Skip to content

Commit c58b879

Browse files
author
Gabriel Corona
committed
Fix compatiblity of admonition with Python-Markdown #93
1 parent ba0c31e commit c58b879

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

mdit_py_plugins/admon/index.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Process admonitions and pass to cb.
22
from __future__ import annotations
33

4+
import re
45
from typing import TYPE_CHECKING, Callable, Sequence
56

67
from markdown_it import MarkdownIt
@@ -14,20 +15,23 @@
1415
from markdown_it.utils import EnvType, OptionsDict
1516

1617

17-
def _get_tag(params: str) -> tuple[str, str]:
18+
def _get_tag(params: str) -> tuple[List[str], str]:
1819
"""Separate the tag name from the admonition title."""
19-
if not params.strip():
20-
return "", ""
2120

22-
tag, *_title = params.strip().split(" ")
23-
joined = " ".join(_title)
21+
params = params.strip()
2422

25-
title = ""
26-
if not joined:
27-
title = tag.title()
28-
elif joined != '""':
29-
title = joined
30-
return tag.lower(), title
23+
if not params:
24+
return [""], ""
25+
26+
match = re.match('^\s*([^"]+)\s+"(.*)"\S*$', params)
27+
if match:
28+
tokens = match.group(1)
29+
tags = tokens.strip().split(" ")
30+
title = match.group(2)
31+
return [tag.lower() for tag in tags], title
32+
else:
33+
tags = params.split(" ")
34+
return [tag.lower() for tag in tags], tags[0].title()
3135

3236

3337
def _validate(params: str) -> bool:
@@ -125,12 +129,13 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
125129
# this will prevent lazy continuations from ever going past our end marker
126130
state.lineMax = next_line
127131

128-
tag, title = _get_tag(params)
132+
tags, title = _get_tag(params)
133+
tag = tags[0]
129134

130135
token = state.push("admonition_open", "div", 1)
131136
token.markup = markup
132137
token.block = True
133-
token.attrs = {"class": " ".join(["admonition", tag, *_extra_classes(markup)])}
138+
token.attrs = {"class": " ".join(["admonition", *tags, *_extra_classes(markup)])}
134139
token.meta = {"tag": tag}
135140
token.content = title
136141
token.info = params

tests/fixtures/admon.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Could contain block elements too
2929

3030
Shows custom title
3131
.
32-
!!! note Custom title
32+
!!! note "Custom title"
3333

3434
Some text
3535

0 commit comments

Comments
 (0)