Skip to content

Fix compatiblity of admonition with Python-Markdown #93 #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions mdit_py_plugins/admon/index.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Process admonitions and pass to cb.
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Callable, Sequence

from markdown_it import MarkdownIt
Expand All @@ -14,20 +15,23 @@
from markdown_it.utils import EnvType, OptionsDict


def _get_tag(params: str) -> tuple[str, str]:
def _get_tag(params: str) -> tuple[List[str], str]:
"""Separate the tag name from the admonition title."""
if not params.strip():
return "", ""

tag, *_title = params.strip().split(" ")
joined = " ".join(_title)
params = params.strip()

title = ""
if not joined:
title = tag.title()
elif joined != '""':
title = joined
return tag.lower(), title
if not params:
return [""], ""

match = re.match('^\s*([^"]+)\s+"(.*)"\S*$', params)
if match:
tokens = match.group(1)
tags = tokens.strip().split(" ")
title = match.group(2)
return [tag.lower() for tag in tags], title
else:
tags = params.split(" ")
return [tag.lower() for tag in tags], tags[0].title()


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

tag, title = _get_tag(params)
tags, title = _get_tag(params)
tag = tags[0]

token = state.push("admonition_open", "div", 1)
token.markup = markup
token.block = True
token.attrs = {"class": " ".join(["admonition", tag, *_extra_classes(markup)])}
token.attrs = {"class": " ".join(["admonition", *tags, *_extra_classes(markup)])}
token.meta = {"tag": tag}
token.content = title
token.info = params
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/admon.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Could contain block elements too

Shows custom title
.
!!! note Custom title
!!! note "Custom title"

Some text

Expand Down Expand Up @@ -249,7 +249,7 @@ zzz</p>

Renders unknown admonition type
.
!!! unknown title
!!! unknown "title"
content
.
<div class="admonition unknown">
Expand Down