Skip to content
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

Add Alerts extra #591

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
36 changes: 36 additions & 0 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,41 @@ def run(self, text):
return self.admonitions_re.sub(self.sub, text)


class Alerts(Extra):
'''
Markdown Alerts as per
https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
'''

name = 'alerts'
order = (), (Stage.BLOCK_QUOTES, )

alert_re = re.compile(r'''
<blockquote>\s*
<p>
\[!(?P<type>NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]
(?P<closing_tag></p>[ \t]*\n?)?
(?P<contents>[\s\S]+?)
</blockquote>
''', re.X
)

def test(self, text):
return "<blockquote>" in text

def sub(self, match: re.Match) -> str:
typ = match["type"].lower()
heading = f"<em>{match['type'].title()}</em>"
contents = match["contents"].strip()
if match["closing_tag"]:
return f'<div class="alert {typ}">\n{heading}\n{contents}\n</div>'
else:
return f'<div class="alert {typ}">\n{heading}\n<p>{contents}\n</div>'

def run(self, text):
return self.alert_re.sub(self.sub, text)


class _BreaksExtraOpts(TypedDict, total=False):
'''Options for the `Breaks` extra'''
on_backslash: bool
Expand Down Expand Up @@ -3490,6 +3525,7 @@ def test(self, text):

# Register extras
Admonitions.register()
Alerts.register()
Breaks.register()
CodeFriendly.register()
FencedCodeBlocks.register()
Expand Down
24 changes: 24 additions & 0 deletions test/tm-cases/alerts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="alert note">
<em>Note</em>
<p>Useful information that users should know, even when skimming content.</p>
</div>

<div class="alert tip">
<em>Tip</em>
<p>Helpful advice for doing things better or more easily.</p>
</div>

<div class="alert important">
<em>Important</em>
<p>Key information users need to know to achieve their goal.</p>
</div>

<div class="alert warning">
<em>Warning</em>
<p>Urgent info that needs immediate user attention to avoid problems.</p>
</div>

<div class="alert caution">
<em>Caution</em>
<p>Advises about risks or negative outcomes of certain actions.</p>
</div>
1 change: 1 addition & 0 deletions test/tm-cases/alerts.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["alerts"]}
15 changes: 15 additions & 0 deletions test/tm-cases/alerts.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
> [!NOTE]
> Useful information that users should know, even when skimming content.

> [!TIP]
> Helpful advice for doing things better or more easily.

> [!IMPORTANT]
> Key information users need to know to achieve their goal.

> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.

> [!CAUTION]
>
nicholasserra marked this conversation as resolved.
Show resolved Hide resolved
> Advises about risks or negative outcomes of certain actions.
Loading