Skip to content

Commit 98b8fa3

Browse files
committed
feat: Add check link script
1 parent 7d3d72c commit 98b8fa3

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎tools/check-links.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,40 @@
4242

4343

4444
def slugify(heading: str) -> str:
45-
"""GitHub-style anchor slug for a markdown heading.
45+
"""GitHub-faithful anchor slug for a markdown heading.
4646
47-
GitHub keeps underscores and hyphens in anchors, lowercases everything,
48-
strips the remaining punctuation, and replaces spaces with hyphens.
47+
Mirrors github/html-pipeline's TableOfContentsFilter: lowercase, drop
48+
everything that is not a word character (letters, digits, underscore),
49+
hyphen, or space, then replace *each* space with a hyphen (consecutive
50+
spaces yield consecutive hyphens, e.g. `a b` -> `a--b`).
4951
"""
5052
s: str = heading.strip().lower()
51-
s = re.sub(r"[`*~]", "", s) # drop markdown emphasis / backticks
52-
s = re.sub(r"[^\w\s\-]", "", s) # drop remaining punctuation
53-
s = re.sub(r"\s+", "-", s) # spaces -> hyphens
53+
s = re.sub(r"^#{1,6}\s+", "", s) # drop leading markdown markers
54+
s = re.sub(r"[^\w\- ]", "", s) # remove punctuation
55+
s = s.strip() # drop space left by markers
56+
s = s.replace(" ", "-") # each space -> hyphen
5457
return s
5558

5659

5760
def headings_slugs(path: Path) -> List[str]:
58-
"""Return the GitHub anchor slugs of all headings in a markdown file."""
61+
"""Return the GitHub anchor slugs of all headings in a markdown file.
62+
63+
Duplicate headings get GitHub's `-1`, `-2`, ... suffixes (the first
64+
occurrence keeps the bare slug).
65+
"""
5966
slugs: List[str] = []
67+
seen: Dict[str, int] = {}
6068
try:
6169
text: str = path.read_text(encoding="utf-8", errors="replace")
6270
except OSError:
6371
return slugs
6472
for line in text.splitlines():
6573
m = re.match(r"^#{1,6}\s+(.+?)\s*#*\s*$", line)
6674
if m:
67-
slugs.append(slugify(m.group(1)))
75+
slug: str = slugify(m.group(1))
76+
n: int = seen.get(slug, 0)
77+
seen[slug] = n + 1
78+
slugs.append(slug if n == 0 else f"{slug}-{n}")
6879
return slugs
6980

7081

0 commit comments

Comments
 (0)