Skip to content

Commit cff5ec6

Browse files
Merge pull request #591 from mhils/alerts
Add `Alerts` extra
2 parents c35e704 + cfae0ac commit cff5ec6

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

lib/markdown2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,41 @@ def run(self, text):
27252725
return self.admonitions_re.sub(self.sub, text)
27262726

27272727

2728+
class Alerts(Extra):
2729+
'''
2730+
Markdown Alerts as per
2731+
https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
2732+
'''
2733+
2734+
name = 'alerts'
2735+
order = (), (Stage.BLOCK_QUOTES, )
2736+
2737+
alert_re = re.compile(r'''
2738+
<blockquote>\s*
2739+
<p>
2740+
\[!(?P<type>NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]
2741+
(?P<closing_tag></p>[ \t]*\n?)?
2742+
(?P<contents>[\s\S]+?)
2743+
</blockquote>
2744+
''', re.X
2745+
)
2746+
2747+
def test(self, text):
2748+
return "<blockquote>" in text
2749+
2750+
def sub(self, match: re.Match) -> str:
2751+
typ = match["type"].lower()
2752+
heading = f"<em>{match['type'].title()}</em>"
2753+
contents = match["contents"].strip()
2754+
if match["closing_tag"]:
2755+
return f'<div class="alert {typ}">\n{heading}\n{contents}\n</div>'
2756+
else:
2757+
return f'<div class="alert {typ}">\n{heading}\n<p>{contents}\n</div>'
2758+
2759+
def run(self, text):
2760+
return self.alert_re.sub(self.sub, text)
2761+
2762+
27282763
class _BreaksExtraOpts(TypedDict, total=False):
27292764
'''Options for the `Breaks` extra'''
27302765
on_backslash: bool
@@ -3501,6 +3536,7 @@ def test(self, text):
35013536

35023537
# Register extras
35033538
Admonitions.register()
3539+
Alerts.register()
35043540
Breaks.register()
35053541
CodeFriendly.register()
35063542
FencedCodeBlocks.register()

test/tm-cases/alerts.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<div class="alert note">
2+
<em>Note</em>
3+
<p>Useful information that users should know, even when skimming content.</p>
4+
</div>
5+
6+
<div class="alert tip">
7+
<em>Tip</em>
8+
<p>Helpful advice for doing things better or more easily.</p>
9+
</div>
10+
11+
<div class="alert important">
12+
<em>Important</em>
13+
<p>Key information users need to know to achieve their goal.</p>
14+
</div>
15+
16+
<div class="alert warning">
17+
<em>Warning</em>
18+
<p>Urgent info that needs immediate user attention to avoid problems.</p>
19+
</div>
20+
21+
<div class="alert caution">
22+
<em>Caution</em>
23+
<p>Advises about risks or negative outcomes of certain actions.</p>
24+
</div>

test/tm-cases/alerts.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["alerts"]}

test/tm-cases/alerts.text

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
> [!NOTE]
2+
> Useful information that users should know, even when skimming content.
3+
4+
> [!TIP]
5+
> Helpful advice for doing things better or more easily.
6+
7+
> [!IMPORTANT]
8+
> Key information users need to know to achieve their goal.
9+
10+
> [!WARNING]
11+
> Urgent info that needs immediate user attention to avoid problems.
12+
13+
> [!CAUTION]
14+
>
15+
> Advises about risks or negative outcomes of certain actions.

0 commit comments

Comments
 (0)