From e5856bf6e3e9691afcfa8024adf6f7962edacbbf Mon Sep 17 00:00:00 2001 From: ndy2 Date: Sat, 9 Mar 2024 19:50:00 +0900 Subject: [PATCH] update tag regex do not use negative lookahead on backquotes --- obsidian_support/conversion/tags/tags.py | 2 +- obsidian_support/conversion/util.py | 3 +- setup.py | 2 +- test/conversion/tabs/test_tabs_backquotes.py | 2 +- test/conversion/tags/__init__.py | 0 test/conversion/tags/test_tags.py | 49 ++++++++++++++++++++ test/conversion/test_util.py | 20 ++++++++ 7 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 test/conversion/tags/__init__.py create mode 100644 test/conversion/tags/test_tags.py diff --git a/obsidian_support/conversion/tags/tags.py b/obsidian_support/conversion/tags/tags.py index 9b75f9d..b77f608 100644 --- a/obsidian_support/conversion/tags/tags.py +++ b/obsidian_support/conversion/tags/tags.py @@ -17,7 +17,7 @@ class TagsConversion(AbstractConversion): @override def obsidian_regex_pattern(self): # OBSIDIAN_TAGS_REGEX - return re.compile(r"(?[\w\-_\/]+)(?![^\[\(`]*[\]\)`])") + return re.compile(r"(?[\w\-_\/]+)(?![^\[\(]*[\]\)])") @override def convert(self, syntax_groups: SyntaxGroup, page: Page) -> str: diff --git a/obsidian_support/conversion/util.py b/obsidian_support/conversion/util.py index dc16a63..fa3d6c8 100644 --- a/obsidian_support/conversion/util.py +++ b/obsidian_support/conversion/util.py @@ -59,7 +59,8 @@ def get_exclude_indices(markdown: str) -> List[Tuple[int, int]]: nested_code_block_syntax = code_block_syntax elif nested_code_block_syntax is not None and \ code_block_syntax.code_block_type == nested_code_block_syntax.code_block_type: - if not nested_code_block_syntax.language.startswith("ad-") and not nested_code_block_syntax.language == "tabs": + if not nested_code_block_syntax.language.startswith("ad-") and \ + not nested_code_block_syntax.language == "tabs": exclude_indices.append((nested_code_block_syntax.start, code_block_syntax.end)) nested_code_block_syntax = None diff --git a/setup.py b/setup.py index 46b0515..a603771 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages -VERSION_NUMBER = '1.3.1' +VERSION_NUMBER = '1.3.2' def read_file(fname): diff --git a/test/conversion/tabs/test_tabs_backquotes.py b/test/conversion/tabs/test_tabs_backquotes.py index d7ccc93..bddd934 100644 --- a/test/conversion/tabs/test_tabs_backquotes.py +++ b/test/conversion/tabs/test_tabs_backquotes.py @@ -5,7 +5,7 @@ from obsidian_support.conversion.tabs.tabs_backquotes import TabsBackquotesConversion -def test_tag_conversion(): +def test_tabs_conversion(): # given conversion = TabsBackquotesConversion() markdown = cleandoc(""" diff --git a/test/conversion/tags/__init__.py b/test/conversion/tags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/conversion/tags/test_tags.py b/test/conversion/tags/test_tags.py new file mode 100644 index 0000000..6f6d1bf --- /dev/null +++ b/test/conversion/tags/test_tags.py @@ -0,0 +1,49 @@ +from inspect import cleandoc + +from assertpy import assert_that + +from obsidian_support.conversion.tags.tags import TagsConversion + + +def test_tags_conversion_1(): + # given + conversion = TagsConversion() + markdown = "#tag" + + # when + converted = conversion.markdown_convert(markdown, None) + + # then + assert_that(converted).is_equal_to("**tag**{: .hash}") + + +def test_tags_conversion_2(): + # given + conversion = TagsConversion() + markdown = cleandoc(""" + #tag + ```tabs + ---tab obsidian markdown + ~~~ + This is just an #obsidian tag. Click to go to the search bar + ~~~ + ---tab mkdocs-material rendered + This is just an #obsidian tag. Click to go to the search bar + ``` + """) + + # when + converted = conversion.markdown_convert(markdown, None) + + # then + assert_that(converted).is_equal_to(cleandoc(""" + **tag**{: .hash} + ```tabs + ---tab obsidian markdown + ~~~ + This is just an #obsidian tag. Click to go to the search bar + ~~~ + ---tab mkdocs-material rendered + This is just an **obsidian**{: .hash} tag. Click to go to the search bar + ``` + """)) diff --git a/test/conversion/test_util.py b/test/conversion/test_util.py index 1cb8d76..338bd3a 100644 --- a/test/conversion/test_util.py +++ b/test/conversion/test_util.py @@ -202,3 +202,23 @@ def test_get_exclude_indices_5(): # then print(exclude_indices) + + +def test_get_exclude_indices_6(): + # given + markdown = cleandoc(""" + ```tabs + ---tab obsidian markdown + ~~~ + This is just an #obsidian tag. Click to go to the search bar + ~~~ + ---tab mkdocs-material rendered + This is just an #obsidian tag. Click to go to the search bar + ``` + """) + + # when + exclude_indices = get_exclude_indices(markdown) + + # then + print(exclude_indices)