Skip to content

Commit

Permalink
Restrict only to GitHub callout keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Dec 19, 2023
1 parent a679a9f commit 71311e7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
7 changes: 5 additions & 2 deletions markdown_callouts/github_callouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Based on https://github.com/Python-Markdown/markdown/blob/4acb949256adc535d6e6cd84c4fb47db8dda2f46/markdown/blockprocessors.py#L277
class _GitHubCalloutsBlockProcessor(BlockQuoteProcessor):
REGEX = re.compile(
r"((?:^|\n) *(?:[^>].*)?(?:^|\n)) {0,3}> *\[!([A-Za-z]{3,})\] *\n(?: *> *\n)*() *(?:> *[^\s\n]|[^\s\n>])"
r"((?:^|\n) *(?:[^>].*)?(?:^|\n)) {0,3}> *\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\] *\n(?: *> *\n)*() *(?:> *[^\s\n]|[^\s\n>])",flags=re.IGNORECASE
)

def test(self, parent, block):
Expand All @@ -31,7 +31,10 @@ def run(self, parent: etree.Element, blocks: list[str]) -> None:
self.parser.parseBlocks(parent, [before])
kind = m[2]

admon = etree.SubElement(parent, "div", {"class": "admonition " + kind.lower()})
css_class = kind.lower()
if css_class == 'caution':
css_class = 'danger'
admon = etree.SubElement(parent, "div", {"class": "admonition " + css_class})
title = etree.SubElement(admon, "p", {"class": "admonition-title"})
title.text = kind.title()

Expand Down
17 changes: 17 additions & 0 deletions tests/github/classes-nope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
input: |
> [!INFO]
> foo
> [!TIPSY]
> foo
output: |-
<blockquote>
<p>
[!INFO]
foo
</p>
<p>
[!TIPSY]
foo
</p>
</blockquote>
56 changes: 56 additions & 0 deletions tests/github/classes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
input: |
> [!NOTE]
> foo
> [!TIP]
> foo
> [!IMPORTANT]
> foo
> [!WARNING]
> foo
> [!CAUTION]
> foo
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
foo
</p>
</div>
<div class="admonition tip">
<p class="admonition-title">
Tip
</p>
<p>
foo
</p>
</div>
<div class="admonition important">
<p class="admonition-title">
Important
</p>
<p>
foo
</p>
</div>
<div class="admonition warning">
<p class="admonition-title">
Warning
</p>
<p>
foo
</p>
</div>
<div class="admonition danger">
<p class="admonition-title">
Caution
</p>
<p>
foo
</p>
</div>
6 changes: 3 additions & 3 deletions tests/github/varied-case.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
input: |
> [!info]
> [!important]
> Foo
> [!TIP]
Expand All @@ -8,9 +8,9 @@ input: |
> [!NoTe]
> Foo
output: |-
<div class="admonition info">
<div class="admonition important">
<p class="admonition-title">
Info
Important
</p>
<p>
Foo
Expand Down

0 comments on commit 71311e7

Please sign in to comment.