Skip to content

Commit

Permalink
Support validating anchors containing emojis (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthagen authored Jul 18, 2023
1 parent ece53af commit 633f551
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ test/integration/site

# PyCharm
.idea

# Project specfic
tests/integration/site
13 changes: 12 additions & 1 deletion htmlproofer/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
ATTRLIST_ANCHOR_PATTERN = re.compile(r'\{.*?\#([^\s\}]*).*?\}')
ATTRLIST_PATTERN = re.compile(r'\{.*?\}')

# Example emojis:
# :banana:
# :smiley_cat:
# :octicons-apps-16:
# :material-star:
EMOJI_PATTERN = re.compile(r'\:[a-z0-9_-]+\:')

urllib3.disable_warnings()


Expand Down Expand Up @@ -225,7 +232,7 @@ def contains_anchor(markdown: str, anchor: str) -> bool:
# # Heading {: #testanchor .testclass }
# # Heading {.testclass #testanchor}
# # Heading {.testclass}
# these can override the headings anchor id, or alternativly just provide additional class etc.
# these can override the headings anchor id, or alternatively just provide additional class etc.
attr_list_anchor_match = ATTRLIST_ANCHOR_PATTERN.match(heading)
if attr_list_anchor_match is not None:
attr_list_anchor = heading_match.groups()[1]
Expand All @@ -239,6 +246,10 @@ def contains_anchor(markdown: str, anchor: str) -> bool:
# But these images are not included in the generated anchor, so remove them.
heading = re.sub(IMAGE_PATTERN, '', heading)

# Headings are allowed to have emojis in them under certain Mkdocs themes.
# https://squidfunk.github.io/mkdocs-material/setup/extensions/python-markdown-extensions/#emoji
heading = re.sub(EMOJI_PATTERN, '', heading)

anchor_slug = slugify(heading, '-')
if anchor == anchor_slug:
return True
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/docs/nested/page1.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ or to a nested page
[Invalid Anchor](./page2.md#BAD_ANCHOR).

But allows valid anchors such as
[Main Page](../index.md#mkdocs-htmlproofer-plugin) and
[Table of Contents](../index.md#table-of-contents).
[Main Page](../index.md#mkdocs-htmlproofer-plugin),
[Table of Contents](../index.md#table-of-contents), and
[Emoji Anchor](./page2.md#title-with-emojis).

## Image Link absolute/relative

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/docs/nested/page2.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Second Nested Test Page

## :smile_cat: Title with Emojis :material-star: :octicons-apps-16:

0 comments on commit 633f551

Please sign in to comment.