Back to Explore
GuideVibeFix editorialUpdated Nov 24, 2026

Markdown Docs Self-Check: Let a Machine Inspect AI-Written Docs First

AI writes docs fast, and they rot quietly: links to deleted files, anchors that match nothing, images never committed, unclosed code fences. This guide builds a 150-line docs checker in Python's stdlib, zero third-party packages: 5 files, 78 lines, 14 headings, 10 links, 2 images, 11 issues on the first pass, 0 after fixing. Python 3.12.3.

Real photo: a person writing notes in a notebook at a wooden desk with a laptop beside them

AI writes code fast, and it writes docs even faster: one prompt and you get a README, a usage guide, and a changelog that all look convincing. But docs rot quietly: links point at files that were deleted long ago, anchors point at headings that were renamed, image files never made it into the repo, a code fence is missing its closing backticks and the rest of the document gets swallowed into a code block. Readers only find out when they click a link and land on a 404. The fix is not "be more careful when writing docs" — it is to let a machine check the docs for you before every commit. This guide builds a docs checker in Python's standard library, about 150 lines, zero third-party packages, and runs it over a fictional "Morning Reading" project's 5 docs: 5 files, 78 lines, 14 headings, 10 internal links, 2 images. The first pass finds 11 issues; after fixing each one, the re-check finds 0. Every number comes from the demo script demo247_zh.py actually running, and the English rerun is rerun_en_demo247_g1.py — both runs agree. The local environment is Python 3.12.3.

One thing up front: a checker does not write docs for you, it tells you that your docs are already broken. AI-generated docs are usually fine as prose; what breaks is structure — links to files that do not exist, anchors that match nothing, missing images, broken formatting. A human scanning the page rarely catches these; a machine scans them in milliseconds. Put the checker in your workflow and docs stop being "write and forget" — every change gets an inspection. This checker only looks at things inside your own project and never touches the network, so it is fast and stable. First, what it checks and what it does not.

Scope first: what the checker checks, and what it does not

The demo checker looks for six kinds of problems. First, headings: does every file have a top-level heading, and does any heading skip a level? Second, internal links: does the linked file exist, and does the anchor match a real heading? Third, images: is there alt text, and does the image file exist? Fourth, fenced code blocks: is any block left unclosed? Fifth, tables: is any table missing its separator row? Sixth, line length: is any line longer than 120 characters? The demo docs contain all six kinds, 11 issues in total: linked file missing 1, linked anchor missing 2, image missing alt text 1, image file missing 1, heading jump 1, missing top-level heading 2, unclosed fenced code block 1, overlong line 1, table missing separator row 1. That adds up to 11, and you can match each one against the demo transcript line by line.

What it does not check matters just as much, so you do not expect it to do everything. First, it does not check external links: the demo has 0 external links, and the checker counts links starting with http://, https://, or mailto: and skips them, because an outside site may be temporarily down, may rate-limit you, or may require login — none of which proves your docs are broken. Checking external links properly needs retries and timeouts, a separate script. Second, it does not check writing quality: whether sentences read well, whether terms are used correctly, whether examples actually run — a machine cannot tell, a human has to read. Third, it does not check visual design: spacing, colors, fonts — that is the renderer's job. The checker is a "structure gatekeeper": it only guards the things that break the moment a reader clicks or looks.

One more scope point worth stating early: the checker reads Markdown source, it does not render. The Markdown spec itself is explicit that any sequence of characters is a valid CommonMark document — there is no such thing as a Markdown "syntax error". A mistake does not raise an error, it just renders into something you did not intend. So what the checker really does is find, in advance, the writing that will render wrong — not judge whether the syntax is valid. That idea runs through every section below.

Headings: one top-level heading per file, no skipped levels

The heading check has two rules. First, every file needs one top-level heading. In the demo, usage.md and notes.md both lack one, one issue each. Why insist on it? The top-level heading is the document's name: without it, a reader arriving from another page has no idea where they landed, and generated tables of contents and full-text search both rely on it to place the page. A file can have many second- and third-level headings, but the top-level heading is the front door — a book without a title.

Second, heading levels must not skip: a fourth-level heading directly under a second-level heading is a jump. In the demo, guide.md line 9 has ## Installation followed directly by #### Advanced Options, one level missing, one issue. The problem is not rendering — the renderer will happily show a fourth-level heading. The problem is structure: readers carry a mental outline of the document, and a jump means a layer is missing from that outline; screen readers navigate by heading level, and a jump leaves them lost. The fix is trivial: change #### to ###, which is exactly what round two of the demo does.

Headings have one more easily missed detail: anchors. Platforms like GitHub auto-generate an anchor for every heading, and a link like #installation points at it. The rule is: lowercase the heading, drop punctuation, turn spaces into hyphens, keep non-Latin characters as they are. So ## Quick Start gets the anchor #quick-start, and ## Installation gets #installation. The demo checker computes anchors with exactly this rule and then checks whether the anchors in links exist. One trap to know in advance: if two headings in one file have the same text, GitHub appends -1 to the second anchor, for example #installation-1. The demo checker does not handle duplicate headings; if your docs have them, rename the headings first — a good habit anyway. The CommonMark spec, section 4.2, defines how ATX headings are written; the anchor rule itself is GitHub rendering behavior, not part of CommonMark, but if you write docs on GitHub you play by its rules.

Links: does the file exist, does the anchor match

The link check covers three cases, and the demo has all three. First, a link to a file: [Missing Document](missing.md) — the file missing.md simply does not exist, one issue, at guide.md line 3. This is the most common and most easily broken kind of link: a file gets renamed, moved, or deleted, and the link stays where it was. Second, an anchor-only link: [anchor](#nonexistent-anchor) — the file is fine, but no heading in the current file produces that anchor, one issue, at guide.md line 7. Third, a cross-file anchor: [section](usage.md#nonexistent-section) — the file usage.md exists, but it has no heading called "nonexistent section", one issue, also on line 7. Three link issues in total, the largest single category among the 11.

Two implementation points matter. First, relative paths resolve against the directory of the file that contains the link, not against the project root, so the checker joins them with os.path.join(os.path.dirname(path), dest) and normalizes with os.path.normpath to collapse things like ../. AI-generated docs often get this wrong, writing links as if they were relative to the root when the file lives in docs/, and the rendered link breaks. Second, anchor matching: the checker first collects every heading in the target file, converts each to its GitHub-style anchor, and then checks whether the anchor in the link is in that set. Note the case subtlety: GitHub lowercases anchors when generating them, so #Installation in a link and #installation mean the same thing, but #install ation does not match, because the space becomes a hyphen.

One more detail: the checker only follows links between .md files; image links are a separate check, covered in the next section. Of the demo's 10 internal links, 3 are broken and 7 are fine — for example, the link from guide.md to usage.md#faq resolves correctly after fixing. Every issue the checker reports carries a line number, such as guide.md:7: linked anchor missing, so you fix by line number instead of re-reading the whole document.

Images: alt text and the file itself, both required

The image check has two rules, and the demo breaks both at once, on guide.md line 13, which reads ![](assets/shot.png). First, the image has no alt text: the square brackets are empty. Alt text is what shows when the image fails to load, and it is what screen readers read aloud to blind users — leaving it empty says "this image does not matter", when it is clearly a screenshot of the app running. The fix is to describe it: ![App screenshot](assets/shot.png), which is what round two of the demo does. Second, the image file does not exist: there is no assets/shot.png in the repo, only assets/logo.png. AI writing docs often invents an image path out of thin air, or leaves the screenshot on its own machine and forgets to commit it, and the render shows a broken-image icon.

The two rules are checked separately because they are fixed differently: missing alt is fixed in the Markdown, a missing file is fixed by adding the file. Round two of the demo does both: it edits the alt text and creates shot.png under assets. Note also that the checker only verifies images with relative paths; remote images starting with http:// or https:// are not checked for existence, because they live outside your repo and you do not own them. Both images in the demo use local paths: the logo in README.md has alt text and the file exists, so it passes; the screenshot in guide.md fails both rules. Good and bad side by side, the rule is clear.

One more note on alt text: longer is not better. One sentence saying what the picture shows is enough. "App screenshot" is enough; "this image shows the main screen of the Morning Reading app running locally, with the book list and reading statistics visible" belongs in the body text, not in the alt attribute. The checker only tests whether alt text is empty, not whether it is well written — that is a human's job.

Code fences and tables: the two structures that rot first

The fenced-code-block check has a single rule, but it is the most consequential one: every code block must be closed. In the demo, notes.md ends with a ```python opener and never has a closing fence, one issue. An unclosed fence swallows everything after it into the code block: later headings, links, and paragraphs all render as one big monospaced blob. Worse, nothing errors — unless you look at the rendered output, you have no idea the second half of the document is gone. The checker works by scanning line by line and flipping an "inside a fence" switch every time it sees a line starting with ``` or ~~~; if the switch is still on at the end of the file, the fence was never closed. CommonMark section 4.5 defines fenced code blocks: three or more backticks or tildes open, the same number of the same character close, and the demo checker implements exactly that rule.

One implementation detail: lines inside a fence are skipped entirely. A # inside a code block is not a heading, a [link](address) inside one is not a link, and a long line inside one does not count as overlong — that is code, not prose. In the demo, README.md has a code block with two commands, and the checker does not inspect them at all, which is correct. If you ever see the checker flagging things inside a code block, the fence itself is probably unclosed and the switch state is wrong — check the fence first.

The table check also has a single rule: the header row must be followed by a separator row. GitHub-flavored Markdown tables (spec section 4.10) require a row of hyphens and pipes under the header, such as | --- | --- |; without it, the whole table fails to render and shows up as a few lines of plain text with pipes in it. In the demo, guide.md line 17 has a table missing its separator row, one issue. The checker works like this: when it sees a line starting and ending with a pipe, it looks at the next line — if it is a separator row, the whole table is skipped; if not, it records one issue and skips the following table rows together, so one broken table is not counted several times. The version matrix in README.md is well formed, has its separator row, and the checker skips it without complaint. Both cases are in the transcript.

Overlong lines: why 120 characters

The last category is overlong lines: any line longer than 120 characters. The demo has exactly one, at guide.md line 11, 280 characters long in the English version. This is the most subjective rule, and the most likely to start an argument, so the trade-off deserves an explanation. 120 is not a spec number — neither CommonMark nor GFM sets a line-length limit; it is a team habit. Lines longer than 120 characters force horizontal scrolling in GitHub's web editor and on phone screens, which is painful to read; but 80 is too strict, because a single Chinese sentence often passes 80 characters, and a checker that cries wolf all day gets ignored. 120 is a compromise, and you can change it to 100 or 140 — the demo script has MAX_LINE = 120, one number to edit — as long as the team agrees on one value.

Two exemptions apply, both of which the demo exercises. First, lines inside code fences do not count, as explained above — code has its own line-breaking logic, and forcing splits would break it. Second, table rows also tend to run long, but no table row in the demo is overlong, so this did not trigger; in a real project where table rows are routinely long, consider exempting them too, or raising the limit for table rows. When the checker reports an overlong line it tells you the length, for example guide.md:11: overlong line 280 chars (limit 120), so you know how much to cut. Round two splits that line into two, each under 120, and the re-check passes.

One language note: the same overlong line is 130 characters in the Chinese version and 280 in the English version, because the same sentence takes more characters to write in English. That is a language difference, not a checker bug: both versions report exactly 1 overlong line, only the character count differs, and the evidence file records it.

Bar chart: 11 issues found in the demo docs by category — linked anchor missing 2, missing H1 2, all others 1 each; 0 issues after fixing

The chart above shows the distribution of the 11 issues, and the numbers match the demo transcript exactly. The two biggest categories are linked anchor missing (2) and missing top-level heading (2); the other seven categories have 1 each. No single category dominates, which means the docs are broken in every direction at once — the typical shape of AI-generated documentation: each spot is only slightly wrong, and together they add up to docs you cannot ship as-is. Read the other way, it is reassuring: run these six checks and you have covered the structural problems of a document; what remains is the quality of the prose itself, and that is a human's job.

Fix and re-check: from 11 to 0

Finding problems is step one; fixing them is step two; re-checking is step three. Round two of the demo fixes all 11 issues one by one and runs the checker over the whole docs tree again: 0 issues. The fix list fits on one card: in guide.md, repoint the link from missing.md to usage.md, change the broken anchor #nonexistent-anchor to #installation, change the broken cross-file anchor to usage.md#faq, demote the fourth-level heading #### Advanced Options to third level, split the 280-character line into two, give the image alt text ![App screenshot](assets/shot.png) and add the image file, and add the separator row | --- | --- | to the table; in usage.md, add a top-level heading; in notes.md, add a top-level heading and close the fence with three backticks at the end of the file. After the fixes, the same tree — 5 files, 78 lines, 14 headings, 10 internal links, 2 images — reports 0 issues.

One practice in the demo is worth copying: before each fix, the script asserts that the text it is about to replace appears exactly once in the file, for example assert g.count(old) == 1. This guards against fixing the wrong place: if the text appears twice, the replacement would change something it should not; if it appears zero times, the document has already changed and the script should stop instead of silently skipping. A fix script should be as strict as a check script — vague automation is worse than no automation. And do not skip the re-check: without it, how do you know the fix worked? The 0 issues in the demo's re-check are the evidence that these docs are now usable.

Three uses you can copy directly

Now three uses that fit the most common vibe projects, ready to copy. First, run it before every commit: save the checker as docs_check.py in the project root and run it after editing docs, fixing whatever it reports on the spot. The demo checker is a single file with no third-party dependencies — copy it and it runs, everything it needs ships with Python 3.12. Second, set the rules on day one of a new project: build the docs tree first, write a top-level heading in every file, get the links right, and only then let AI fill in the content. Use the checker as the acceptance test for AI-written docs: if it does not pass, make the AI fix it until it does, so the docs are clean from day one instead of needing a cleanup months later.

Third, wire it into automated checks: the previous cycle's guide covered GitHub Actions, where every push automatically runs tests — the same pipeline can run the docs check by adding python docs_check.py to the workflow, and a failing check turns the run red. This guide does not repeat the Actions setup, since that guide already covers it; you only need to add the one command. All three uses share one principle: the earlier the checker runs, the cheaper the fix. Repairing a doc the moment it breaks costs one line; repairing it after three months of neglect means a dozen broken links and anchors that match nothing — an afternoon gone.

Closing checklist and boundaries

Here is the whole guide as a checklist to run the next time you write docs for a vibe project: every Markdown file starts with one top-level heading; heading levels step down one at a time, never skipping; links point only at files that exist, and anchors only at headings that exist — for cross-file anchors, confirm the target file really has that heading; images need alt text and the file must actually be in the repo; code fences that open must close, and a quick look at the closing backticks after writing is worth it; tables need their separator row under the header; keep single lines under 120 characters and split the ones that run over; after editing docs, run the checker and do not commit until the re-check reports 0 issues.

Finally, what this guide does not cover, so you do not take the checker where it does not belong: it does not teach Markdown syntax systematically — tables, task lists, and strikethrough in full are in the CommonMark spec and the GitHub-flavored Markdown spec, while this guide only covers how to check them; it does not check whether external links are alive, which needs networking, retries, and timeouts, and deserves its own guide; and it does not teach doc writing itself — how to write a good README or organize a docs tree is a writing topic, not a checking topic. On boundaries: this guide complements the Python file-I/O guide already on the site — that one teaches how to read and write files, this one teaches what to check in Markdown content once you have read it — and it complements the GitHub Actions guide, which teaches how to make checks run automatically, while this guide teaches what the check itself looks for. The checker is small, but it guards the baseline of docs: links that open, images that load, structure that is not broken.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles