|
| 1 | +--- |
| 2 | +id: MDS035 |
| 3 | +name: toc-directive |
| 4 | +status: ready |
| 5 | +description: Flag renderer-specific TOC directives that render as literal text on CommonMark and goldmark. |
| 6 | +--- |
| 7 | +# MDS035: toc-directive |
| 8 | + |
| 9 | +Flag renderer-specific TOC directives that |
| 10 | +render as literal text on CommonMark and |
| 11 | +goldmark. |
| 12 | + |
| 13 | +- **ID**: MDS035 |
| 14 | +- **Name**: `toc-directive` |
| 15 | +- **Status**: ready |
| 16 | +- **Default**: disabled (opt-in) |
| 17 | +- **Fixable**: no |
| 18 | +- **Implementation**: |
| 19 | + [source](./) |
| 20 | +- **Category**: meta |
| 21 | + |
| 22 | +## What it detects |
| 23 | + |
| 24 | +Four directive variants appear in the wild, |
| 25 | +each expanded by a different renderer: |
| 26 | + |
| 27 | +| Token | Expanded by | |
| 28 | +|-------------|----------------------------------------| |
| 29 | +| `[TOC]` | Python-Markdown, MultiMarkdown, Pandoc | |
| 30 | +| `[[_TOC_]]` | GitLab Flavored Markdown, Azure DevOps | |
| 31 | +| `[[toc]]` | markdown-it-toc-done-right, VitePress | |
| 32 | +| `${toc}` | some VitePress configurations | |
| 33 | + |
| 34 | +CommonMark and goldmark do not expand any of |
| 35 | +them; authors see the directive token in the |
| 36 | +rendered output instead of a table of |
| 37 | +contents. The rule flags each token when it |
| 38 | +appears on its own line inside a paragraph so |
| 39 | +authors can replace it, delete it, or maintain |
| 40 | +the list manually. |
| 41 | + |
| 42 | +`[TOC]` alone is also valid CommonMark |
| 43 | +shortcut reference link syntax. When the |
| 44 | +document contains a matching |
| 45 | +`[TOC]: <url>` definition, the rule |
| 46 | +suppresses the diagnostic because the token |
| 47 | +resolves to a real link rather than rendering |
| 48 | +as literal text. |
| 49 | + |
| 50 | +## Why not auto-fix |
| 51 | + |
| 52 | +The right replacement depends on intent. For |
| 53 | +file-index usage — an index page listing |
| 54 | +sibling documents — mdsmith's |
| 55 | +[`<?catalog?>`](../MDS019-catalog/README.md) |
| 56 | +directive is the replacement. For in-document |
| 57 | +heading TOCs, mdsmith has no equivalent; the |
| 58 | +author must drop the directive or maintain a |
| 59 | +manual list. The rule is detection-only and |
| 60 | +names both cases in its message. |
| 61 | + |
| 62 | +## Config |
| 63 | + |
| 64 | +Enable: |
| 65 | + |
| 66 | +```yaml |
| 67 | +rules: |
| 68 | + toc-directive: true |
| 69 | +``` |
| 70 | +
|
| 71 | +Disable (default): |
| 72 | +
|
| 73 | +```yaml |
| 74 | +rules: |
| 75 | + toc-directive: false |
| 76 | +``` |
| 77 | +
|
| 78 | +## Examples |
| 79 | +
|
| 80 | +### Good |
| 81 | +
|
| 82 | +<?include |
| 83 | +file: good/default.md |
| 84 | +wrap: markdown |
| 85 | +?> |
| 86 | +
|
| 87 | +````markdown |
| 88 | +# Document with no TOC directives |
| 89 | +
|
| 90 | +This document has no renderer-specific TOC |
| 91 | +markers, so MDS035 stays silent. |
| 92 | +
|
| 93 | +Normal prose is unaffected, and inline code |
| 94 | +like `[TOC]` or `${toc}` is not flagged because |
| 95 | +the tokens are inside code spans. |
| 96 | + |
| 97 | +```text |
| 98 | +[TOC] |
| 99 | +[[_TOC_]] |
| 100 | +[[toc]] |
| 101 | +${toc} |
| 102 | +``` |
| 103 | + |
| 104 | +Even the fenced block above is a code block, |
| 105 | +not a paragraph, so nothing is reported. |
| 106 | +```` |
| 107 | + |
| 108 | +<?/include?> |
| 109 | + |
| 110 | +### Bad |
| 111 | + |
| 112 | +<?include |
| 113 | +file: bad/bracketed.md |
| 114 | +wrap: markdown |
| 115 | +?> |
| 116 | + |
| 117 | +```markdown |
| 118 | +# Python-Markdown TOC directive |
| 119 | +
|
| 120 | +[TOC] |
| 121 | +
|
| 122 | +Everything below the directive renders fine, |
| 123 | +but the directive itself appears as literal |
| 124 | +text on CommonMark and goldmark renderers. |
| 125 | +``` |
| 126 | + |
| 127 | +<?/include?> |
0 commit comments