Skip to content

Commit

Permalink
update tag regex
Browse files Browse the repository at this point in the history
do not use negative lookahead on backquotes
  • Loading branch information
ndy2 committed Mar 28, 2024
1 parent 13e724f commit c3152aa
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 23 deletions.
61 changes: 54 additions & 7 deletions docs/features/admonition/callout.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,57 @@ See [Demo](demo) for more examples
> [!faq]- Are callouts foldable?
> Yes! In a foldable callout, the contents are hidden when the callout is collapsed.
```

## Nested

### obsidian callout

```tabs
---tab obsidian markdown
~~~
> [!note] some note
> some content before nested notes
>
> > [!note] nested note with no content
>
> > [!note] another nested note with some another content
> > some another content
>
> some content after nested notes
~~~
---tab obsidian rendered
![[images/callout_3.png]]
```

### mkdocs-material admonition

```tabs
---tab mkdocs-material markdown
~~~
!!! note "some note"
some content before nested notes
!!! note "nested note with no content"
!!! note "another nested note with some another content"
some another content
some content after nested notes
~~~
---tab mkdocs-material rendered
> [!note] some note
> some content before nested notes
>
> > [!note] nested note with no content
>
> > [!note] another nested note with some another content
> > some another content
>
> some content after nested notes
```

## 💡 Notes

common types that `obsidian callout` and `mkdocs-material admonition` support are
Expand All @@ -96,16 +147,12 @@ common types that `obsidian callout`, `mkdocs-material admonition` and even `Git
## Implementation details and Warning

> [!warning] implementation limitation
> 1. Nested callout or admonition is not suppoerted
>
> 2. Unlike actual obsidian callout, It requires more precise syntax. <br>
> there sholud be only zero or one space before and after first `>` character <br>
> and no space before the rest of `>` characters and one space after it.
> Unlike actual obsidian callout, It requires more precise syntax. <br>
> there sholud be only zero or one space before and after first `>` character <br>
> and no space before the rest of `>` characters and one space after it.
>
> recommended format is as below
> ```text
> > [!info]
> > copy me
> ```
6 changes: 3 additions & 3 deletions obsidian_support/conversion/abstract_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def obsidian_regex_groups(self):
return list(self.obsidian_regex_pattern.groupindex.keys())

@abstractmethod
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
pass

"""
A template method that applies conversion for every regex matches
"""

def markdown_convert(self, markdown: str, page: Page) -> str:
def markdown_convert(self, markdown: str, page: Page, depth: int = 0) -> str:
converted_markdown = ""
index = 0
excluded_indices = get_exclude_indices(markdown)
Expand All @@ -56,7 +56,7 @@ def markdown_convert(self, markdown: str, page: Page) -> str:

syntax_groups = list(map(lambda group: obsidian_syntax.group(group), self.obsidian_regex_groups))

mkdocs_syntax = self.convert(syntax_groups, page)
mkdocs_syntax = self.convert(syntax_groups, page, depth)
converted_markdown += markdown[index:start]
converted_markdown += mkdocs_syntax
index = end + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def obsidian_regex_pattern(self):
""", flags=re.VERBOSE)

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._create_admonition(*syntax_groups)

def _create_admonition(self, place, ad_type: str, title: str, collapse: str, contents: str) -> str:
Expand Down
22 changes: 18 additions & 4 deletions obsidian_support/conversion/admonition/admonition_callout.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ def obsidian_regex_pattern(self):
""", flags=re.VERBOSE)

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
return self._create_admonition(*syntax_groups)
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._create_admonition(page, depth, *syntax_groups)

def _create_admonition(self, place, ad_type: str, collapse: str, title: str, contents: str) -> str:
def _create_admonition(self, page: Page, depth: int, place: str, ad_type: str, collapse: str, title: str,
contents: str) -> str:
contents = contents.replace("\n> ", "\n ")
contents = contents.replace("\n > ", "\n ")
contents = contents.replace("\n>", "\n ")
Expand All @@ -60,5 +61,18 @@ def _create_admonition(self, place, ad_type: str, collapse: str, title: str, con
else:
collapse = "!!! "

admonition = place + collapse + ad_type + title + "\n" + contents
de_indented_contents = self._de_indent(contents)
contents = self.markdown_convert(de_indented_contents, page, depth)
re_indented_contents = self._indent(depth + 1, contents)

admonition = place + collapse + ad_type + title + "\n" + re_indented_contents
return admonition

def _de_indent(self, contents: str) -> str:
contents = contents.replace("\n ", "\n")
return contents

def _indent(self, depth: int, contents: str) -> str:
indent = " " * 4 * depth
contents = contents.replace("\n", "\n" + indent)
return contents
2 changes: 1 addition & 1 deletion obsidian_support/conversion/comment/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def obsidian_regex_pattern(self):
return re.compile(r"%%(?P<comment>[\S\s]*?)%%")

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._convert_comment(*syntax_groups)

def _convert_comment(self, comment):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def obsidian_regex_pattern(self):
return re.compile(r"!\[\[(?P<image_path>[^|^\]]+)(?P<tags>|.+)?]]")

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._convert_image_internal_link(*syntax_groups)

def _convert_image_internal_link(self, image_path: str, tags: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion obsidian_support/conversion/image_link/image_web_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def obsidian_regex_pattern(self):
return re.compile(r"!\[(?P<tags>(?!\\).*)]\((?P<image_path>https?://.*)\)")

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._convert_image_link(*syntax_groups)

def _convert_image_link(self, tags: str, image_path: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion obsidian_support/conversion/pdf/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def obsidian_regex_pattern(self):
return re.compile(r"!\[\[(?P<pdf_path>[^|^\]]+\.pdf)(?P<tags>#height=\d+)?]]")

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
base_path = page.canonical_url[:-len(page.url)]
return self._convert_tags(base_path, *syntax_groups)

Expand Down
2 changes: 1 addition & 1 deletion obsidian_support/conversion/tabs/tabs_backquotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def obsidian_regex_pattern(self):
""", flags=re.VERBOSE)

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._create_content_tabs(*syntax_groups)

def _create_content_tabs(self, place, tabs) -> str:
Expand Down
2 changes: 1 addition & 1 deletion obsidian_support/conversion/tabs/tabs_tilde_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def obsidian_regex_pattern(self):
""", flags=re.VERBOSE)

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._create_content_tabs(*syntax_groups)

def _create_content_tabs(self, place, tabs) -> str:
Expand Down
2 changes: 1 addition & 1 deletion obsidian_support/conversion/tags/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def obsidian_regex_pattern(self):
return re.compile(r"(?<!\\)#(?P<tags>[\w\-_\/]+)(?![^\[\(]*[\]\)])")

@override
def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str:
def convert(self, syntax_groups: SyntaxGroup, page: Page, depth: int) -> str:
return self._convert_tags(*syntax_groups)

def _convert_tags(self, tags: str) -> str:
Expand Down
37 changes: 36 additions & 1 deletion test/conversion/admonition/test_admonition_callout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from obsidian_support.conversion.admonition.admonition_callout import AdmonitionCalloutConversion


def test_admonition_backquotes_conversion():
def test_admonition_backquotes_conversion_1():
# given
conversion = AdmonitionCalloutConversion()
markdown = cleandoc("""
Expand All @@ -22,3 +22,38 @@ def test_admonition_backquotes_conversion():
some content
"""))


def test_admonition_backquotes_conversion_2():
# given
conversion = AdmonitionCalloutConversion()
markdown = cleandoc("""
> [!note] some note
> some content before nested notes
>
> > [!note] nested note with no content
>
> > [!note] another nested note with some another content
> > some another content
>
> some content after nested notes
""")

# when
converted = conversion.markdown_convert(markdown, None)

# then
assert_that(converted).is_equal_to(cleandoc("""
!!! note "some note"
some content before nested notes
!!! note "nested note with no content"
!!! note "another nested note with some another content"
some another content
some content after nested notes
"""))

0 comments on commit c3152aa

Please sign in to comment.