diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fe1b48a..4fd23d5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.8","3.9", "3.10", "3.11", "3.12"] + python-version: ["3.8","3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} diff --git a/README.md b/README.md index 94a5786..7fd7c29 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,12 @@ pylint $(git ls-files '*.py') && pytest -vv ## Changelog +## 0.9.0 + +**Features and Bug Fixes:** + +- Added the ability to use alias style links to anchors withing the current page, e.g.: `[[#my-anchor]]` + ## 0.8.1 **Bug Fix:** fixes a bug where annotations would break older versions of Python 3. Bug report: [#9](/~https://github.com/EddyLuten/mkdocs-alias-plugin/issues/9). diff --git a/alias/plugin.py b/alias/plugin.py index f82c947..869e237 100644 --- a/alias/plugin.py +++ b/alias/plugin.py @@ -91,13 +91,27 @@ def replace_tag( if match.group(2) is not None: tag_bits = str(match.group(2)).split('#') alias = aliases.get(tag_bits[0]) + + # if the alias is not found, log a warning and return the input string + # unless the alias is an anchor tag, then try to find the anchor tag + # and replace it with the anchor's title if alias is None: - log.warning( - "Alias '%s' not found in '%s'", - match.group(2), - page_file.src_path - ) - return match.group(0) # return the input string + if len(tag_bits) < 2: + log.warning( + "Alias '%s' not found in '%s'", + match.group(2), + page_file.src_path + ) + return match.group(0) # return the input string + anchor = tag_bits[1] + matched = match.group(2) + # using the [[#anchor]] syntax to link within the current page: + if str(matched).startswith('#'): + anchors = get_markdown_toc(page_file.content_string) + anchor_tag = find_anchor_by_id(anchors, anchor) + if anchor_tag is not None: + log.info(f"treating {matched} like an anchor to {anchor_tag['name']}") + return f"[{anchor_tag['name']}](#{anchor})" text = None anchor = tag_bits[1] if len(tag_bits) > 1 else None diff --git a/setup.py b/setup.py index 528ded7..3dd9910 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='mkdocs-alias-plugin', - version='0.8.1', + version='0.8.2', description= 'An MkDocs plugin allowing links to your pages using a custom alias', long_description=long_description, diff --git a/test_plugin.py b/test_plugin.py index 0431ba1..64a27a5 100644 --- a/test_plugin.py +++ b/test_plugin.py @@ -4,6 +4,7 @@ """ import logging import re + from mkdocs.structure.files import File from alias.plugin import get_alias_names, get_page_title, replace_tag, ALIAS_TAG_REGEX @@ -374,3 +375,22 @@ def test_plugin_should_use_custom_title_with_anchor_titles(): markdown ) assert result == 'Test: [Custom Title](../../../my-alias.md#nested)' + + +def test_plugin_should_link_to_anchor_on_current_page(): + """An alias with an anchor on the current page should link to it + without the page path""" + logger = logging.getLogger() + page_file = File( + 'foo/bar.md', + src_dir=None, + dest_dir='/path/to/site', + use_directory_urls=False, + ) + page_file.content_string = 'Test: [[#anchor]]\n\n## Anchor\n\nSome text' + result = re.sub( + ALIAS_TAG_REGEX, + lambda match: replace_tag(match, {}, logger, page_file), + page_file.content_string + ) + assert result == 'Test: [Anchor](#anchor)\n\n## Anchor\n\nSome text'