Skip to content

Commit

Permalink
Adds support for linking to anchors within the same page, see #14
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyLuten committed Feb 21, 2025
1 parent 33546df commit 02ba4fb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 20 additions & 6 deletions alias/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'

0 comments on commit 02ba4fb

Please sign in to comment.