Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

tools: md_links skips inline code, ~~~ fences and fences of four or more backticks (Fixes #872) (#889)

Beri18895c8dadd6
diff --git a/scripts/oss/md_links.py b/scripts/oss/md_links.py
index 1267c14cd..a5ec9e0fe 100644
--- a/scripts/oss/md_links.py
+++ b/scripts/oss/md_links.py
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
"""Markdown link check (#333): every relative link in a tracked *.md file must point at a file that is tracked in git.
-`uv run scripts/oss/md_links.py [--root DIR]`. Ignores code fences, absolute URLs (any `scheme:`), `#anchor` and
-`mailto:` links, and targets with brackets (reference-style leftovers). Anchors after `#` are not checked, only the file.
+`uv run scripts/oss/md_links.py [--root DIR]`. Ignores code (inline spans, and fences of three or more backticks or tildes), absolute URLs (any `scheme:`),
+`#anchor` and `mailto:` links, and targets with brackets (reference-style leftovers). Anchors after `#` are not checked, only the file.
Handles a link title (`[a](b "t")`), `<angle>` targets, `%20` escapes and the outer link of a badge (`[![x](img)](page)`).
A target must match a tracked file or directory exactly, so a wrong letter case fails on macOS too (CI's Linux would).
One line per dead link (file: link); exit 1 if any."""
@@ -16,7 +16,10 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
LINK = re.compile(r"\]\(<?([^)\s>]+)>?(?:\s+(?:\"[^\"]*\"|'[^']*'))?\)")
-FENCE = re.compile(r"```.*?```", re.S)
+# A fence opens with 3+ backticks or tildes and closes with the same run; an inline span is a backtick run closed by an
+# equal run and may not cross a blank line, so one stray backtick cannot swallow the rest of the file.
+FENCE = re.compile(r"(`{3,}|~{3,}).*?\1", re.S)
+INLINE_CODE = re.compile(r"(?<!`)(`+)(?!`)(?:(?!\n[ \t]*\n).)+?(?<!`)\1(?!`)", re.S)
def tracked_markdown(root: Path) -> list[str]:
@@ -38,7 +41,7 @@ def dead_links(root: Path) -> list[tuple[str, str]]:
bad = []
known = tracked_paths(root)
for f in tracked_markdown(root):
- text = FENCE.sub("", (root / f).read_text(encoding="utf-8"))
+ text = INLINE_CODE.sub("", FENCE.sub("", (root / f).read_text(encoding="utf-8")))
for m in LINK.finditer(text):
target = m.group(1)
if re.match(r"[A-Za-z][A-Za-z0-9+.-]*:|#", target) or "[" in target:
diff --git a/scripts/oss/tests/test_md_links.py b/scripts/oss/tests/test_md_links.py
index 8a7441edd..854bcc891 100644
--- a/scripts/oss/tests/test_md_links.py
+++ b/scripts/oss/tests/test_md_links.py
@@ -35,3 +35,29 @@ def test_title_badge_escape_and_case(make_repo):
'docs/a.md': '[up](..) [up2](../)\n', 'docs/b c.md': 'x\n', 'img.svg': 'x\n', 'LICENSING.md': 'x\n',
})
assert m.dead_links(root) == [('README.md', 'GONE.md'), ('README.md', 'Docs/a.md')]
+
+
+def test_links_in_inline_code_and_tilde_or_long_fences_are_not_links(make_repo):
+ root = make_repo({
+ 'README.md': 'Write links as `` `[text](path/to/file.md)` `` and `[x](gone1.md)`.\n\n'
+ '~~~\n[t](gone2.md)\n~~~\n\n'
+ '````\n```\n[f](gone3.md)\n```\n````\n',
+ })
+ assert m.dead_links(root) == []
+
+
+def test_a_real_dead_link_next_to_an_inline_code_span_is_still_found(make_repo):
+ root = make_repo({'README.md': 'Use `[a](shown.md)` then [real](missing.md) and `code` [ok](README.md).\n'})
+ assert m.dead_links(root) == [('README.md', 'missing.md')]
+
+
+def test_stray_backtick_and_shorter_fence_run_do_not_hide_real_links(make_repo):
+ root = make_repo({
+ 'README.md': 'a ` b\n\n[one](gone1.md)\n\n````\n```\n[in](x.md)\n````\n[two](gone2.md)\n',
+ })
+ assert m.dead_links(root) == [('README.md', 'gone1.md'), ('README.md', 'gone2.md')]
+
+
+def test_an_inline_span_does_not_cross_a_blank_line(make_repo):
+ root = make_repo({'README.md': 'a ` b\n\n[one](gone1.md)\n\nc ` d\n'})
+ assert m.dead_links(root) == [('README.md', 'gone1.md')]