Skip to content

Commit

Permalink
feat: fallback to alias as a title when using aliases
Browse files Browse the repository at this point in the history
feat: fallback to alias as a title when using aliases
  • Loading branch information
sondregronas authored Dec 11, 2024
2 parents 1e3809a + 4659cd4 commit 8bff974
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "mkdocs-callouts"
version = "1.14.1"
version = "1.15.0"
keywords = ["mkdocs", "mkdocs-plugin", "markdown", "callouts", "admonitions", "obsidian"]
description = "A simple plugin that converts Obsidian style callouts and converts them into mkdocs supported 'admonitions' (a.k.a. callouts)."
readme = "README.md"
Expand Down
7 changes: 7 additions & 0 deletions src/mkdocs_callouts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def _parse_block_syntax(self, block) -> str:

# Group 3: Callout block type (note, warning, info, etc.) + inline block syntax
c_type = block.group(3).lower()
# Get a clean version of the callout type for the title, if it exists in order
# to use it as a fallback if the title is empty & we are using an alias
clean_c_type = c_type.split("|")[0].strip()
c_type = re.sub(r" *\| *(inline|left) *$", " inline", c_type)
c_type = re.sub(r" *\| *(inline end|right) *$", " inline end", c_type)
c_type = re.sub(r" *\|.*", "", c_type)
Expand All @@ -100,6 +103,10 @@ def _parse_block_syntax(self, block) -> str:

# Group 5: Title, add leading whitespace and quotation marks, if it exists
title = block.group(5).strip()
# If we are using an alias without a title, use the alias
# We use startswith to avoid issues with the inline block syntax
if not title and not c_type.lower().startswith(clean_c_type.lower()):
title = clean_c_type.capitalize()
title = f' "{title}"' if title else ""

# Construct the new callout syntax ({indent}!!! note "Title")
Expand Down
36 changes: 34 additions & 2 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ def test_aliases_enabled():

# Test alias conversion
mkdown = '> [!HINT]\n> Text'
result = '!!! tip\n\tText'
result = '!!! tip "Hint"\n\tText'
assert (parser.parse(mkdown) == result)

# Test alias conversion with inline block syntax
mkdown = '> [!HINT | inline]\n> Text'
result = '!!! tip inline\n\tText'
result = '!!! tip inline "Hint"\n\tText'
assert (parser.parse(mkdown) == result)

# Test alias conversion where alias is a substring of another alias
Expand Down Expand Up @@ -403,3 +403,35 @@ def test_nested_codefences():
> ```
```"""
assert (convert(mkdown) == mkdown)


def test_alias_titles():
# Test alias titles
mkdown = "> [!cAuTiON]"
result = "!!! warning \"Caution\""

assert (convert(mkdown) == result)

# Test normal titles
mkdown = "> [!WARNING]"
result = "!!! warning"

assert (convert(mkdown) == result)

# Test with inline block syntax
mkdown = "> [!WaRnINg | inline]"
result = "!!! warning inline"

assert (convert(mkdown) == result)

# Test alias titles with inline block syntax
mkdown = "> [!cAuTiON | inline]"
result = "!!! warning inline \"Caution\""

assert (convert(mkdown) == result)

# Test alias with a title
mkdown = "> [!cAuTiON] Title"
result = "!!! warning \"Title\""

assert (convert(mkdown) == result)

0 comments on commit 8bff974

Please sign in to comment.