Skip to content

Commit

Permalink
fix(#2): enforce newline in alert
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed May 19, 2024
1 parent d5c6c1f commit 3f140c4
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 152 deletions.
2 changes: 1 addition & 1 deletion mdformat_gfm_alerts/factories/_gfm_blockquote_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def parse_possible_blockquote_admon(
# Exit if no match for any pattern
text = state.src[start:]
regexes = [
re.compile(rf"{pat}(?P<inline_content>(?: |<br>)[^\n]+)?", re.IGNORECASE)
re.compile(rf"{pat}(?: |<br>)?(?P<inline_content>[^\n]+)?", re.IGNORECASE)
for pat in patterns
]
match = next((_m for rx in regexes if (_m := rx.match(text))), None)
Expand Down
7 changes: 1 addition & 6 deletions mdformat_gfm_alerts/mdit_plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
from ._gfm_alerts import (
GFM_ALERTS_PREFIX,
INLINE_SEP,
format_gfm_alerts_markup,
gfm_alerts_plugin,
)
from ._gfm_alerts import GFM_ALERTS_PREFIX, format_gfm_alerts_markup, gfm_alerts_plugin
10 changes: 7 additions & 3 deletions mdformat_gfm_alerts/mdit_plugins/_gfm_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ def format_gfm_alerts_markup(
admonition: AlertData,
) -> None:
"""Format markup."""
title_line = (
f"[!{admonition.meta_text.upper()}]{INLINE_SEP}{admonition.inline_content}"
)
# To fix #2, when the admonition and text are on the same line, push the content
# to the next line. While this is equivalent in the markdown spec, this is
# required for Github
newline = "\n"

meta = f"[!{admonition.meta_text.upper()}]"
title_line = f"{meta}{newline}{admonition.inline_content}".rstrip()

with new_token(state, GFM_ALERTS_PREFIX, "div") as token:
token.attrs = {
Expand Down
4 changes: 2 additions & 2 deletions mdformat_gfm_alerts/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Render

from .mdit_plugins import GFM_ALERTS_PREFIX, INLINE_SEP, gfm_alerts_plugin
from .mdit_plugins import GFM_ALERTS_PREFIX, gfm_alerts_plugin


def update_mdit(mdit: MarkdownIt) -> None:
Expand All @@ -18,7 +18,7 @@ def update_mdit(mdit: MarkdownIt) -> None:

def _render_gfm_alerts(node: RenderTreeNode, context: RenderContext) -> str:
"""Render a `RenderTreeNode`."""
title_line = node.markup.replace(INLINE_SEP, "")
title_line = node.markup
elements = [render for child in node.children if (render := child.render(context))]
# Do not separate the title line from the first row
return "\n".join([title_line, "\n\n".join(elements)]).rstrip()
Expand Down
3 changes: 2 additions & 1 deletion tests/format/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Fixes formatting issues introduced without this extension
- List A
- List Nested
> [!NOTE] Useful information that users should know, even when skimming content.
> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.
Expand Down
Loading

0 comments on commit 3f140c4

Please sign in to comment.