1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
"""Fail if a documentation link points at a heading or a file that does not exist.
`README.md` is the crate's page on crates.io, and it is the only document with a
hand-written table of contents. A heading gets renamed, the entry in the contents
list keeps pointing at the old anchor, and nothing notices: Markdown has no link
checker, `cargo doc` never sees the README, and a dead anchor renders as ordinary
text rather than an error. The same is true of a relative link such as
`[CONTRIBUTING.md](CONTRIBUTING.md)` once the file moves.
A fragment is resolved in the document that owns it, which is not always this
one: `[Ground rules](CONTRIBUTING.md#where-a-rule-lives)` names a heading in
`CONTRIBUTING.md`, and checking only that the file exists would accept the link
after that heading was renamed. Non-Markdown targets have no headings, so for
those the existence check is the whole check.
So the anchors are checked the way the rest of this repository checks its claims:
by a script, not by remembering to look. Only *internal* targets are resolved --
an `http(s)://` link is left alone, because a network check would fail for
reasons that have nothing to do with this repository and a CI job that reaches
out to the internet is a job that fails when the internet is slow. Fenced code
blocks and inline code spans are skipped for the same reason: a link inside one
is an example, and it renders as code rather than as something to click.
The GitHub anchor rule is reproduced rather than approximated: lowercase the
heading, drop every character that is not a letter, a digit, a space, an
underscore or a hyphen, then turn each space into a hyphen. Duplicate headings
get `-1`, `-2`, ... suffixes in document order, which is what GitHub does.
It also checks the README's hand-drawn file tree (`check_tree`): every entry the
tree *names* must exist at the directory its indentation points at. The tree is a
prose mirror of the filesystem, and a prose mirror of `ls` is the kind of second
copy that goes stale silently -- a file is renamed and the block still reads as
mechanical. It caught an `examples/probe.rs` that no longer existed and a module
list missing four entries. A file the tree omits is not an error (the tree is a
curated map, not a listing); a file the tree *names* and that is absent is.
Usage: python3 scripts/check_doc_links.py [markdown ...]
With no arguments it checks every top-level `*.md`, README first.
"""
= ..
# An inline link: the target is everything up to the closing paren. A target may
# carry a title (`](path "Title")`), which is not part of the path.
=
=
=
# A line of the README's hand-drawn file tree: an optional run of ancestor
# columns (`│ ` or ` `, four characters each), a `├── `/`└── ` connector,
# then a bare name and an optional description. The name is the first
# whitespace-delimited token (so a description may contain anything, including
# `/`), and a trailing `/` marks a directory. The description is consumed but not
# used, so the `$` anchor still holds on a line that carries one.
=
"""The GitHub anchor for a heading, before duplicate disambiguation."""
=
=
return
"""Every anchor a reader can reach in this document."""
: =
: =
= False
# A `#` inside a fenced block is code, not a heading.
= not
continue
continue
=
continue
=
=
= + 1
return
# Markdown targets are parsed for their headings; anything else (a `.rs`, a
# `.toml`) has no anchors to resolve, so only its existence can be checked.
=
"""Drop `` `...` `` spans from a line.
A link inside an inline code span is an *example*, not a link -- it renders as
code, so there is nothing for a reader to click and nothing to resolve. This
matters here rather than in theory: the notes in `AGENTS.md` about this very
checker quote a broken link, and without this they would fail the check they
describe.
"""
: =
, = 0,
+= 1
continue
=
+= 1
= -
# A code span closes only on a run of *exactly* the opening length, so
# ``a`b`` is one span and does not end at the inner backtick. Measuring
# the whole candidate run matters: `str.find` would match the first
# backtick of a longer run and treat a mismatched pair as a span, which
# drops the text between them -- including a link, which Markdown still
# renders as a link. That would hide a broken link from this check.
= -1
=
+= 1
continue
=
+= 1
=
break
= # a longer or shorter run is not a closer; skip it whole
# an unmatched opener is literal text
=
=
return
=
=
# A fragment on a relative link names a heading in the *target* file, so the
# target's anchors are needed too. Cache them: the same file is often linked
# several times, and re-reading it per link would make this quadratic.
: =
: =
=
=
=
return
= False
= not
continue
continue
continue # an external link; not this repository's to resolve
, , =
# A same-document link: the fragment names a heading here.
continue
= /
continue
# The file exists, but the fragment is a claim about the *target's*
# headings, and dropping it here is how a renamed heading in another
# document goes unnoticed: the link still resolves as a file, so it
# renders as a link that lands on the top of the page instead of the
# section it names.
return
"""Fail if the README's hand-drawn file tree names a file that is not there.
The tree is prose that mirrors `ls`, and a prose mirror of the filesystem is
the kind of second copy that goes stale silently: a file is added or renamed,
nothing reads the block, and it stays wrong. A stale tree is not cosmetic --
it lists an example that no longer exists (so the documented `cargo run
--example` fails) or omits a module, and a reader trusts it precisely because
it looks mechanical.
Only the *existence* of each named entry is checked, against the directory its
indentation points at: the check is `(parent / name).exists()`. A file the
tree forgets to list is not an error -- the tree is a curated map, not an
exhaustive listing -- but a file the tree *names* and that is absent is the
defect this catches, and it is the one that misleads.
Only `README.md` carries the tree; other documents are skipped.
"""
return
=
# Find the tree: the fenced block that contains a `├── ` line.
= None
= + 1
break
return # no tree block; nothing to check
: =
# A stack of (indent-depth, directory Path) the current line lives under.
# The root `.` is depth 0; each `│ `/` ` column is one level deeper.
: =
= + + 1
break
=
continue
=
continue
= // 4
=
=
=
# Pop to this line's parent.
=
= /
return
"""Every top-level Markdown document, README first.
Discovered rather than listed so that a document added later is checked
without anyone remembering to add it to the gate.
"""
=
= /
return +
= or
: =
continue
return 1
=
return 0