Skip to content

Commit 633f551

Browse files
authored
Support validating anchors containing emojis (#67)
1 parent ece53af commit 633f551

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ test/integration/site
4343

4444
# PyCharm
4545
.idea
46+
47+
# Project specfic
48+
tests/integration/site

htmlproofer/plugin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
ATTRLIST_ANCHOR_PATTERN = re.compile(r'\{.*?\#([^\s\}]*).*?\}')
3434
ATTRLIST_PATTERN = re.compile(r'\{.*?\}')
3535

36+
# Example emojis:
37+
# :banana:
38+
# :smiley_cat:
39+
# :octicons-apps-16:
40+
# :material-star:
41+
EMOJI_PATTERN = re.compile(r'\:[a-z0-9_-]+\:')
42+
3643
urllib3.disable_warnings()
3744

3845

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

249+
# Headings are allowed to have emojis in them under certain Mkdocs themes.
250+
# https://squidfunk.github.io/mkdocs-material/setup/extensions/python-markdown-extensions/#emoji
251+
heading = re.sub(EMOJI_PATTERN, '', heading)
252+
242253
anchor_slug = slugify(heading, '-')
243254
if anchor == anchor_slug:
244255
return True

tests/integration/docs/nested/page1.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ or to a nested page
2121
[Invalid Anchor](./page2.md#BAD_ANCHOR).
2222

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

2728
## Image Link absolute/relative
2829

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# Second Nested Test Page
2+
3+
## :smile_cat: Title with Emojis :material-star: :octicons-apps-16:

0 commit comments

Comments
 (0)