eslint-plugin-markdown-links-unlinked 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/CONTRIBUTING.md +56 -0
- package/DESIGN.md +43 -0
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/RELEASING.md +75 -0
- package/assets/logo.svg +29 -0
- package/package.json +94 -4
- package/src/graph.js +301 -0
- package/src/index.js +20 -0
- package/src/links.js +68 -0
- package/src/rules/no-unlinked-pages.js +80 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026/11/26)
|
|
4
|
+
|
|
5
|
+
- [#2](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/2): Added the `no-unlinked-pages` rule - [@dblock](https://github.com/dblock).
|
|
6
|
+
- [#5](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/5): Added an original editable lonely-page logo, 160-pixel README placement, artwork maintenance guidance, and a packaged-asset check - [@dblock](https://github.com/dblock).
|
|
7
|
+
- [#6](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/6): Added benchmarks for graph building, sweeps, live updates, reachability, and full ESLint runs, with a quick profile in CI - [@dblock](https://github.com/dblock).
|
|
8
|
+
- [#7](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/7): Improved performance by not parsing linted pages again when their text is unchanged and by caching resolved link targets, roughly halving the rule's overhead - [@dblock](https://github.com/dblock).
|
|
9
|
+
- [#9](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/9): Canceled superseded pull request CI runs - [@dblock](https://github.com/dblock).
|
|
10
|
+
- [#10](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/10): Simplified the README for users, with a clearer `allow` and `ignore` distinction, a Limitations list, and installation ready for the 0.1.0 release, and made the contributor documentation easier to scan - [@dblock](https://github.com/dblock).
|
|
11
|
+
- [#11](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/11): Added release automation with npm-package-release 0.4.0 and npm Trusted Publishing, including name reservation with `npr bootstrap` and a simulated release test - [@dblock](https://github.com/dblock).
|
|
12
|
+
- [#12](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/12): Fixed the packed-consumer test under npm 12, which the release workflow uses, and validated the package with npm 12 in CI - [@dblock](https://github.com/dblock).
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Read [Design](DESIGN.md) and [Automated Contributor Instructions](AGENTS.md) first.
|
|
4
|
+
|
|
5
|
+
## Setup and Validation
|
|
6
|
+
|
|
7
|
+
Use Node.js `26.13.0` from `.nvmrc` for development and npm. The package supports Node.js `^22.13.0 || >=24.0.0`, ESLint `^9.39.0 || ^10.0.0`, and `@eslint/markdown` `^8.0.0 || ^9.0.0`. Documentation spelling tools require Node.js 22.18 on the 22.x line or Node.js 24 or later. Verify and prefer current stable releases when selecting or updating dependencies while preserving supported-runtime compatibility.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm ci
|
|
11
|
+
npm test
|
|
12
|
+
npm run lint
|
|
13
|
+
npm run test:coverage
|
|
14
|
+
npm run package:check
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Run `npm test` and `npm run lint` before every push, including updates to a PR. Run final checks before committing and reuse their results for the push when tested content and configuration have not changed. `npm test` includes the packed-consumer test; `npm run package:check` runs it alone. `PACKAGE_ARCHIVE` selects an existing archive for that test; an unset or empty value packs a fresh one.
|
|
18
|
+
|
|
19
|
+
`npm run lint:code` checks source and embedded JavaScript examples. `npm run lint:markdown` uses ESLint for Markdown structure, prose, spelling, single-line paragraphs, and title-case headings, combining `@eslint/markdown`, `eslint-plugin-markdown-preferences`, `eslint-plugin-markdown-preferences-paragraph` with `width: "nowrap"`, and `eslint-plugin-prose-spellcheck` with US English, software terminology, and `.spellcheck.txt`. Local rules in `scripts/documentation-plugin.js` require a first-level first heading and a final newline. The documentation lint also runs this plugin's `no-unlinked-pages` rule, so link new root documents from README. `npm run lint:workflows` checks title case for workflow, job, and step display names.
|
|
20
|
+
|
|
21
|
+
Development uses `@eslint/markdown` 9. The root `overrides` let the development-only documentation plugins, whose released peer ranges do not include 9 yet, share it; they never reach consumers. Remove each override once that plugin's peer range includes 9, and never override this plugin's own peers.
|
|
22
|
+
|
|
23
|
+
Keep human-readable Markdown headings and YAML display names in title case, preserving proper names, acronyms, and code identifiers. Keep each Markdown paragraph on one source line. Add reviewed vocabulary to `.spellcheck.txt` and fix typos instead of suppressing them.
|
|
24
|
+
|
|
25
|
+
Test, Lint, and Package are separate CI workflows. The Test matrix uses explicit inline `matrix.entry` records, without `include` or `exclude`, pairing the minimum and current Node versions with both ESLint majors and both `@eslint/markdown` majors; a Coverage job reports to Coveralls. Package validates the packed consumer on Linux, macOS, and Windows, and on Linux with npm 12.2.3, the npm version the release workflow uses, because npm 11 and 12 print different `npm pack --json` shapes. No Danger checks are configured. Each workflow's `concurrency` group cancels superseded pull request runs but never `master` runs.
|
|
26
|
+
|
|
27
|
+
## Benchmarks
|
|
28
|
+
|
|
29
|
+
`npm run bench` generates synthetic documentation trees of 100, 1,000, and 5,000 pages in temporary directories and measures them with [tinybench](https://github.com/tinylibs/tinybench); `npm run bench:quick` uses 50 and 200 pages and fewer iterations. Trees come in four shapes: `wide` (README links to every page), `chain` (each page links to the next), `random` (three random links per page, about 10% orphans), and `nested` (directories up to five levels deep). Each run first lints the tree and fails if a planned orphan is not reported.
|
|
30
|
+
|
|
31
|
+
The benchmarks measure a cold graph build, a throttled sweep without changes, reachability alone, parsing one page, live updates with unchanged and changed text, and full ESLint runs over the `random` tree with and without the rule, reporting the rule's overhead. Output is a Markdown table with medians, means with margin of error, and per-page cost; add `--json` for machine-readable results, `--sizes=100,1000` or `--shapes=random,nested` to narrow a run, and `--no-eslint` to skip the ESLint runs.
|
|
32
|
+
|
|
33
|
+
To compare a change, run the same command on the base branch and the change on an otherwise idle machine, ideally several times, and compare medians; report the numbers, Node.js version, and CPU in the PR. Timings vary across machines, so CI runs the quick profile once in a Benchmarks job and posts its table to the job summary without enforcing thresholds.
|
|
34
|
+
|
|
35
|
+
## Tests
|
|
36
|
+
|
|
37
|
+
Tests use `node --test` under `test/`:
|
|
38
|
+
|
|
39
|
+
- `test/plugin.test.js` covers the public export.
|
|
40
|
+
- `test/rule.test.js` lints the fixture trees in `test/fixtures/` (`links`, `defaults`, `options`, `missing-root`) with the real ESLint API through `test-support/lint.js` and runs `RuleTester`, clearing the per-process graph before each test because linted text updates it.
|
|
41
|
+
- `test/graph.test.js` covers link extraction, target resolution, discovery, permissions, live text, and throttled sweeps with an injected clock (`setClock`) in temporary trees.
|
|
42
|
+
- `test/package.test.js` packs the archive, inspects its contents, installs it in a temporary consumer, and runs the real ESLint CLI.
|
|
43
|
+
- `test/release.test.js` runs `createReleaseWorkflow` from `npr.config.js` with npm-package-release's simulated registry and release host against a local Git remote, skipping below that tool's Node.js minimum.
|
|
44
|
+
- `test/workflow-names.test.js` covers the display-name checker.
|
|
45
|
+
|
|
46
|
+
Aim for coverage close to 100% of project-owned runtime code with meaningful behavioral tests, including errors and edge cases, and inspect `npm run test:coverage` output rather than excluding real code.
|
|
47
|
+
|
|
48
|
+
## Pull Requests
|
|
49
|
+
|
|
50
|
+
Start changes on a `<username>/<topic>` branch in the existing checkout, for example `git switch -c dblock/topic master`; use a sibling worktree only for work that runs in parallel. Split larger work into small stacked PRs, each branching from and targeting the PR below it. Keep each PR to one coherent commit relative to its base, and merge PRs with rebase merges only. Amend task-owned branches and push them with `--force-with-lease`; never force-push `master`.
|
|
51
|
+
|
|
52
|
+
Update affected documentation. Add CHANGELOG entries for features, improvements, and fixes, but not minor typos or the initial scaffold, as `- [#123](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/pull/123): Description - [@dblock](https://github.com/dblock).` above `- Your contribution here.` in the Next section. Features increment the minor version (resetting patch for `0.x`); ask before a major bump. Keep `package.json` and `package-lock.json` synchronized.
|
|
53
|
+
|
|
54
|
+
Include your tool's AI co-author trailer in AI-assisted commits, and disclose AI authorship on behalf of `@dblock` in PR descriptions and comments. Use closing issue references. Do not merge, publish, tag, or dispatch the release workflow without explicit authorization. The release workflow is the one exception to pull-request-only changes: it pushes release and next-development commits and tags directly to `master`; see [Releasing](RELEASING.md).
|
|
55
|
+
|
|
56
|
+
After a PR merges, fetch with pruning, verify the merge on GitHub, and remove only clean, no-longer-needed topic branches and worktrees.
|
package/DESIGN.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Design
|
|
2
|
+
|
|
3
|
+
This plugin finds Markdown pages that readers cannot reach by following links from a project's root pages. It complements [eslint-plugin-markdown-links](https://github.com/ota-meshi/eslint-plugin-markdown-links), which checks that outgoing links resolve; this plugin checks the opposite direction. User-facing behavior, options, and limitations are documented in the [README](README.md).
|
|
4
|
+
|
|
5
|
+
The plugin exports one rule, `no-unlinked-pages`, enabled as an error by the `recommended` config. The entry point is `src/index.js`, the rule is `src/rules/no-unlinked-pages.js`, discovery and the link graph are `src/graph.js`, and link extraction is `src/links.js`.
|
|
6
|
+
|
|
7
|
+
## Rule
|
|
8
|
+
|
|
9
|
+
The rule reports once per unreachable linted page, at line 1, listing the configured roots, or a configuration problem at line 1 when no root page exists. Allowed pages stay in the graph, so their links count, while ignored pages are never discovered. Pages outside `rootDir` and files without a discovered page on disk, such as unsaved editor buffers, are not reported. A relative `rootDir` resolves from ESLint's working directory.
|
|
10
|
+
|
|
11
|
+
## Link Graph
|
|
12
|
+
|
|
13
|
+
ESLint does not give rules the list of linted files, so the rule discovers pages itself by walking `rootDir` with `node:fs`, skipping `node_modules`, `.git`, symbolic links, and `ignore` patterns. A glob library would add little over this walk, which also records each page's modification time and size; [picomatch](https://github.com/micromatch/picomatch) matches `roots`, `allow`, and `ignore`, including dotfiles.
|
|
14
|
+
|
|
15
|
+
Pages are parsed with [`mdast-util-from-markdown`](https://github.com/syntax-tree/mdast-util-from-markdown) and the GFM and YAML/TOML front matter extensions, the parser family `@eslint/markdown` uses, rather than private ESLint internals. The rule parses the linted page's `context.sourceCode.text` itself instead of reusing ESLint's tree, because one graph serves every linted file and ESLint's tree depends on the configured language: `markdown/commonmark` has no GFM footnotes, and front matter parses as Markdown unless enabled. A page's reachability never depends on its own links, so the linted page's text only affects other pages.
|
|
16
|
+
|
|
17
|
+
Edges come from what a reader can follow: inline links (including inside GFM footnotes), link references that resolve to a definition, and HTML `<a href>` anchors, parsed with [htmlparser2](https://github.com/fb55/htmlparser2) so quoting and character references are handled correctly. Targets resolve relative to the linking page, or from `rootDir` for `/path`; fragments and queries are removed and percent-encoding is decoded, falling back to the raw path when malformed. A directory link reaches its `README` and `index` pages with any configured extension. URLs with a scheme or starting with `//`, non-page files, and targets outside `rootDir` add no edges. Reachability is a breadth-first search from the roots, so cycles terminate.
|
|
18
|
+
|
|
19
|
+
## Caching
|
|
20
|
+
|
|
21
|
+
ESLint has no hook that runs after the last file, and a rule can only report on the file being linted, so the rule cannot collect links as ESLint visits pages and report orphans at the end; it needs the whole graph before the first report. The graph is therefore kept in memory per process:
|
|
22
|
+
|
|
23
|
+
- It is built once, keyed by `rootDir`, `roots`, `ignore`, and `extensions`, by walking and parsing every page.
|
|
24
|
+
- Each linted page replaces its own links from its current text, so unsaved editor text counts immediately. A SHA-256 digest of the parsed text skips parsing again when the text matches what was last read, which removes a second parse of every page in a CLI run.
|
|
25
|
+
- Each page caches the pages its links resolve to, tagged with a version of the page set, so after one page's links change only that page's links resolve again. Reachability is recomputed only when some page's links change.
|
|
26
|
+
- A sweep finds changes made outside ESLint, such as a `git checkout`: it walks the tree again, drops removed pages, and parses only new pages or pages whose modification time or size changed and whose digest differs. Sweeps run at most every two seconds (`SWEEP_INTERVAL`), so a CLI run walks the tree about once and editor servers pick up outside changes within seconds.
|
|
27
|
+
- Linted text that differs from disk keeps counting until that file changes on disk or the process restarts.
|
|
28
|
+
|
|
29
|
+
## Benchmarks
|
|
30
|
+
|
|
31
|
+
Benchmarks use [tinybench](https://github.com/tinylibs/tinybench), a maintained MIT library with no dependencies, for consistent warmup, sampling, and statistics. They generate deterministic trees with a seeded generator instead of committing large fixtures, time phases through the graph module's internal functions (`getPageGraph`, `buildGraph`, `setClock`), and run ESLint through the public plugin entry. They report rather than assert timings, because runners vary.
|
|
32
|
+
|
|
33
|
+
The rule's remaining cost is roughly one Markdown parse per page. A parser with only the GFM footnote extension measured about 42% faster but is not equivalent: without the table and task list extensions, a `|` in a link destination inside a table cell and a `[x]` task marker with a matching `[x]:` definition produce different edges, so the rule keeps the parser `@eslint/markdown` uses.
|
|
34
|
+
|
|
35
|
+
## Out of Scope
|
|
36
|
+
|
|
37
|
+
The plugin does not report links to missing files or fragments (use `markdown-links/no-missing-path` and `markdown/no-missing-link-fragments`), unused images or other assets, or pages reachable only through site navigation such as a VitePress sidebar.
|
|
38
|
+
|
|
39
|
+
## Logo
|
|
40
|
+
|
|
41
|
+
[`assets/logo.svg`](assets/logo.svg) is original artwork created with Claude under the project's [MIT license](LICENSE), with no adapted third-party assets. The SVG is both the editable source and README display asset: a worried amber page holding a snapped chain link apart from two linked pale blue pages, on a transparent 256-by-256 canvas.
|
|
42
|
+
|
|
43
|
+
Keep the logo above the README title at 160 pixels, with descriptive alternative text. Edit the SVG directly and preserve its view box, accessible title and description, and transparent background. Inspect full-size and README-size rendering on light and dark backgrounds, for example with `rsvg-convert -w 160 assets/logo.svg`; keep previews outside the repository. The packed-consumer test verifies that the logo is included in the archive.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dblock
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
-
|
|
1
|
+
<img src="assets/logo.svg" alt="Worried googly-eyed page holding a snapped chain link apart from two linked pages" width="160">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# ESLint Plugin Markdown Links Unlinked
|
|
4
|
+
|
|
5
|
+
[](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/actions/workflows/test.yml) [](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/actions/workflows/lint.yml) [](https://github.com/dblock/eslint-plugin-markdown-links-unlinked/actions/workflows/package.yml) [](https://coveralls.io/github/dblock/eslint-plugin-markdown-links-unlinked?branch=master)
|
|
6
|
+
|
|
7
|
+
Finds Markdown pages that nothing links to. Starting from `README.md`, the `no-unlinked-pages` rule follows your documentation's links and reports every page a reader can never reach:
|
|
8
|
+
|
|
9
|
+
```text
|
|
10
|
+
docs/old-guide.md
|
|
11
|
+
1:1 error This page is not linked from any root page (README.md). markdown-links-unlinked/no-unlinked-pages
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It complements [eslint-plugin-markdown-links](https://github.com/ota-meshi/eslint-plugin-markdown-links), whose `no-missing-path` rule checks that links point to files that exist; this plugin checks the other direction, that every page is linked from somewhere.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Install the plugin with ESLint and `@eslint/markdown`:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npm install --save-dev eslint @eslint/markdown eslint-plugin-markdown-links-unlinked
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Requires Node.js `^22.13.0 || >=24`, ESLint 9.39 or later (including 10), and `@eslint/markdown` 8 or 9.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Add the `recommended` config to `eslint.config.js`:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import markdown from "@eslint/markdown";
|
|
32
|
+
import unlinked from "eslint-plugin-markdown-links-unlinked";
|
|
33
|
+
|
|
34
|
+
export default [
|
|
35
|
+
{ files: ["**/*.md"], plugins: { markdown }, language: "markdown/gfm" },
|
|
36
|
+
{ files: ["**/*.md"], ...unlinked.configs.recommended },
|
|
37
|
+
];
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then run `npx eslint .` without `--cache` (see [Limitations](#limitations)).
|
|
41
|
+
|
|
42
|
+
A page counts as linked when a reachable page links to it with an inline link, a reference link, or an HTML `<a href>`, using a relative path or a path starting with `/` from the project root. A link to a directory reaches its `README.md` or `index.md`. Images, external URLs, and unused reference definitions do not count.
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
Options go in a rule override:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
export default [
|
|
50
|
+
// ...the Usage config above
|
|
51
|
+
{
|
|
52
|
+
files: ["**/*.md"],
|
|
53
|
+
rules: {
|
|
54
|
+
"markdown-links-unlinked/no-unlinked-pages": ["error", {
|
|
55
|
+
roots: ["README.md", "docs/index.md"],
|
|
56
|
+
allow: ["CHANGELOG.md", "docs/drafts/**"],
|
|
57
|
+
}],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
];
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
| Option | Default | Description |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `roots` | `["README.md"]` | Pages to start from. |
|
|
66
|
+
| `allow` | `["CHANGELOG.md", "LICENSE.md", "CODE_OF_CONDUCT.md", "SECURITY.md", ".github/**"]` | Pages that may be unlinked; their own links still count. Setting it replaces the defaults. |
|
|
67
|
+
| `ignore` | `[]` | Pages to treat as if they did not exist: never reported, and their links do not count. |
|
|
68
|
+
| `extensions` | `[".md"]` | File extensions that count as pages, for example `[".md", ".mdx"]`. |
|
|
69
|
+
| `rootDir` | ESLint's working directory | Where to look for pages; set it to `"docs"` to check only a documentation folder. |
|
|
70
|
+
|
|
71
|
+
Patterns are globs relative to `rootDir`. If none of the `roots` exists, every page reports `No root page found: README.md.` instead.
|
|
72
|
+
|
|
73
|
+
## Limitations
|
|
74
|
+
|
|
75
|
+
- Whether a page is unlinked depends on other files, but `eslint --cache` only re-lints files that changed. Removing a link does not report the page it orphans until that page changes, so run this rule without `--cache` in CI.
|
|
76
|
+
- In editors, unsaved edits count immediately, while changes made outside the editor, such as a `git checkout`, count within about two seconds.
|
|
77
|
+
- Pages under `rootDir` that ESLint does not lint are never reported, but their links still count.
|
|
78
|
+
- Paths are case-sensitive, like links on GitHub, even on case-insensitive filesystems, and symbolic links are not followed.
|
|
79
|
+
- `node_modules` and `.git` are skipped.
|
|
80
|
+
|
|
81
|
+
## Contributing
|
|
82
|
+
|
|
83
|
+
Read [Contributing](CONTRIBUTING.md), [Design](DESIGN.md), [Changelog](CHANGELOG.md), [Releasing](RELEASING.md), and [Automated Contributor Instructions](AGENTS.md).
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
[MIT](LICENSE).
|
package/RELEASING.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Releasing
|
|
2
|
+
|
|
3
|
+
Releases are published by GitHub Actions through npm Trusted Publishing, using [npm-package-release](https://github.com/dblock/npm-package-release) `0.4.0` (the exact development dependency) and its reusable workflow at the matching `v0.4.0` tag, called from [`.github/workflows/release.yml`](.github/workflows/release.yml). No npm token is stored anywhere. Publication, tags, GitHub Releases, authentication, trusted-publisher setup, and repository protection changes each require explicit authorization; this document does not grant it.
|
|
4
|
+
|
|
5
|
+
## Setup Once
|
|
6
|
+
|
|
7
|
+
### Create the Release Environment
|
|
8
|
+
|
|
9
|
+
The workflow publishes from a `release` environment that only the default branch can deploy to. Create it, restrict it to `master`, and read both settings back:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
gh api -X PUT repos/dblock/eslint-plugin-markdown-links-unlinked/environments/release \
|
|
13
|
+
-F 'deployment_branch_policy[protected_branches]=false' \
|
|
14
|
+
-F 'deployment_branch_policy[custom_branch_policies]=true'
|
|
15
|
+
gh api -X POST repos/dblock/eslint-plugin-markdown-links-unlinked/environments/release/deployment-branch-policies \
|
|
16
|
+
-f name=master -f type=branch
|
|
17
|
+
gh api repos/dblock/eslint-plugin-markdown-links-unlinked/environments/release --jq .deployment_branch_policy
|
|
18
|
+
gh api repos/dblock/eslint-plugin-markdown-links-unlinked/environments/release/deployment-branch-policies --jq '.branch_policies[] | {name, type}'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Allow Release Pushes
|
|
22
|
+
|
|
23
|
+
The workflow pushes the release commit and tag, and then the next development version, directly to `master`. Remove the `pull_request` rule from the default-branch ruleset, keeping the `non_fast_forward` and `deletion` rules and no bypass actors, then read the ruleset back:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
id=$(gh api repos/dblock/eslint-plugin-markdown-links-unlinked/rulesets --jq '.[] | select(.target == "branch") | .id')
|
|
27
|
+
gh api repos/dblock/eslint-plugin-markdown-links-unlinked/rulesets/$id --jq '{name, target, enforcement, bypass_actors, conditions, rules: [.rules[] | select(.type != "pull_request")]}' > ruleset.json
|
|
28
|
+
gh api -X PUT repos/dblock/eslint-plugin-markdown-links-unlinked/rulesets/$id --input ruleset.json
|
|
29
|
+
gh api repos/dblock/eslint-plugin-markdown-links-unlinked/rulesets/$id --jq '{enforcement, bypass_actors, rules: [.rules[].type]}'
|
|
30
|
+
rm ruleset.json
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
This lets every writer push fast-forward commits to `master`, not only the workflow. Ordinary development still goes through pull requests.
|
|
34
|
+
|
|
35
|
+
### Reserve the Name and Trust the Workflow
|
|
36
|
+
|
|
37
|
+
npm accepts a trusted publisher only for an existing package, so `npr bootstrap` publishes a deprecated, empty `0.0.0` placeholder to reserve the name, then configures GitHub Actions publishing for `release.yml` in the `release` environment. Preview it, log in, and complete the npm 2FA prompts yourself in your own terminal:
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
npm exec --offline -- npr bootstrap --dry-run
|
|
41
|
+
npm login --registry=https://registry.npmjs.org
|
|
42
|
+
npm exec --offline -- npr bootstrap
|
|
43
|
+
npm view eslint-plugin-markdown-links-unlinked versions --json --registry=https://registry.npmjs.org
|
|
44
|
+
npm trust list eslint-plugin-markdown-links-unlinked --json --registry=https://registry.npmjs.org
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`npr reserve` and `npr trust` run the two steps separately; rerun `npr trust` if it fails after the placeholder is published. Check that the publisher lists owner `dblock`, repository `eslint-plugin-markdown-links-unlinked`, workflow `release.yml`, environment `release`, and direct publishing. A new trusted publisher must publish successfully within two days, so set it up only when ready to release. Do not tag or create a GitHub Release for the placeholder.
|
|
48
|
+
|
|
49
|
+
## Releasing a Version
|
|
50
|
+
|
|
51
|
+
Make sure `## X.Y.Z (Next)` in [CHANGELOG](CHANGELOG.md) has real entries, that `package.json` and `package-lock.json` carry that unpublished version, and that CI passes on `master`. With authorization, dispatch the workflow from `master`:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
gh workflow run release.yml --repo dblock/eslint-plugin-markdown-links-unlinked --ref master
|
|
55
|
+
gh run list --repo dblock/eslint-plugin-markdown-links-unlinked --workflow release.yml --limit 5
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The workflow validates tests, lint, and the exact packed archive in a consumer (`npm run package:check` with `PACKAGE_ARCHIVE`), uploads recovery artifacts, publishes that archive, pushes the dated release commit and `vX.Y.Z` tag, creates the GitHub Release with the archive and changelog notes, and pushes the next patch version with a new `(Next)` section. Pass `-f version=X.Y.Z` to release a different numeric version than the checked-in one.
|
|
59
|
+
|
|
60
|
+
## Verifying a Release
|
|
61
|
+
|
|
62
|
+
A dispatch is not proof of publication. Check the run summary, the tag and GitHub Release, and that the registry's checksums match the retained archive:
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
npm view eslint-plugin-markdown-links-unlinked@X.Y.Z version dist.integrity dist.shasum --json --registry=https://registry.npmjs.org
|
|
66
|
+
gh release view vX.Y.Z --repo dblock/eslint-plugin-markdown-links-unlinked
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Recovery
|
|
70
|
+
|
|
71
|
+
Never overwrite a published version or move a release tag. Before retrying a failed or ambiguous run, download its retained artifacts and inspect the registry, remote tags, and GitHub Release, following npm-package-release's [recovery guide](https://github.com/dblock/npm-package-release/blob/v0.4.0/docs/recovery.md); a retry reconciles completed steps rather than repeating publication.
|
|
72
|
+
|
|
73
|
+
## Testing the Release Configuration
|
|
74
|
+
|
|
75
|
+
[`npr.config.js`](npr.config.js) exports `createReleaseWorkflow`, and `test/release.test.js` runs it with npm-package-release's simulated registry and release host against a local Git remote, without publishing. It skips below npm-package-release's Node.js minimum without raising the plugin's own. Do not use `npr prepare` or `npr release` as a preview in a real checkout: they commit and tag, and `release` publishes and pushes.
|
package/assets/logo.svg
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256" role="img" aria-labelledby="title description">
|
|
2
|
+
<title id="title">Unlinked Pages</title>
|
|
3
|
+
<desc id="description">A worried googly-eyed amber page holds a snapped chain link while two linked pale blue pages sit together in the background.</desc>
|
|
4
|
+
<g stroke="#b8cedf" stroke-width="3" stroke-linejoin="round">
|
|
5
|
+
<g transform="rotate(-6 48 59)">
|
|
6
|
+
<path d="M28 26h31.4L74 40.6V86a6 6 0 0 1-6 6H28a6 6 0 0 1-6-6V32a6 6 0 0 1 6-6Z" fill="#f1f7fc"/>
|
|
7
|
+
<path d="M59.4 26v14.6H74Z" fill="#deebf6"/>
|
|
8
|
+
<path d="M32 50.6h32.2M32 61.7h23.4M32 72.9h32.2" fill="none" stroke="#8cafca" stroke-width="5" stroke-linecap="round"/>
|
|
9
|
+
</g>
|
|
10
|
+
<g transform="rotate(5 122 51)">
|
|
11
|
+
<path d="M102 18h31.4L148 32.6V78a6 6 0 0 1-6 6h-40a6 6 0 0 1-6-6V24a6 6 0 0 1 6-6Z" fill="#f1f7fc"/>
|
|
12
|
+
<path d="M133.4 18v14.6H148Z" fill="#deebf6"/>
|
|
13
|
+
<path d="M106 42.6h32.2M106 53.7h23.4M106 64.9h32.2" fill="none" stroke="#8cafca" stroke-width="5" stroke-linecap="round"/>
|
|
14
|
+
</g>
|
|
15
|
+
</g>
|
|
16
|
+
<path d="M74 56h22" fill="none" stroke="#8cafca" stroke-width="6" stroke-linecap="round"/>
|
|
17
|
+
<ellipse cx="128" cy="236" rx="74" ry="9" fill="#263248" opacity=".09"/>
|
|
18
|
+
<g stroke="#263248" stroke-width="5" stroke-linecap="round" stroke-linejoin="round">
|
|
19
|
+
<path d="M88 120h64l26 26v74a8 8 0 0 1-8 8H88a8 8 0 0 1-8-8v-92a8 8 0 0 1 8-8Z" fill="#fff4d6"/>
|
|
20
|
+
<path d="M152 120v26h26Z" fill="#ffe3a3"/>
|
|
21
|
+
<ellipse cx="110" cy="164" rx="14" ry="16" fill="#fffdf6"/>
|
|
22
|
+
<ellipse cx="146" cy="164" rx="13" ry="15" fill="#fffdf6"/>
|
|
23
|
+
<circle cx="114" cy="170" r="6" fill="#263248" stroke="none"/>
|
|
24
|
+
<circle cx="142" cy="170" r="6" fill="#263248" stroke="none"/>
|
|
25
|
+
<path d="M98 150l18-8M158 150l-16-8M112 204q16-10 32 0M80 186q-22 4-30-10" fill="none"/>
|
|
26
|
+
</g>
|
|
27
|
+
<rect x="24" y="148" width="40" height="22" rx="11" fill="none" stroke="#e9a934" stroke-width="9" transform="rotate(-30 44 159)"/>
|
|
28
|
+
<path d="M66 136l8-10M56 128l-2-12" fill="none" stroke="#263248" stroke-width="4" stroke-linecap="round"/>
|
|
29
|
+
</svg>
|
package/package.json
CHANGED
|
@@ -1,6 +1,96 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-markdown-links-unlinked",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "An ESLint plugin that reports Markdown pages not linked from any root page.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.js",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src/",
|
|
12
|
+
"assets/logo.svg",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"CHANGELOG.md",
|
|
16
|
+
"CONTRIBUTING.md",
|
|
17
|
+
"DESIGN.md",
|
|
18
|
+
"RELEASING.md"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": "^22.13.0 || >=24.0.0"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "node --test",
|
|
25
|
+
"test:coverage": "c8 --reporter=text --reporter=lcov npm test",
|
|
26
|
+
"lint": "npm run lint:code && npm run lint:markdown && npm run lint:workflows",
|
|
27
|
+
"lint:code": "eslint .",
|
|
28
|
+
"lint:markdown": "eslint --config eslint.config-markdown.js \"*.md\"",
|
|
29
|
+
"lint:workflows": "node scripts/check-workflow-names.js",
|
|
30
|
+
"package:check": "node --test test/package.test.js",
|
|
31
|
+
"bench": "node bench/run.js",
|
|
32
|
+
"bench:quick": "node bench/run.js --quick"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@eslint/markdown": "^8.0.0 || ^9.0.0",
|
|
36
|
+
"eslint": "^9.39.0 || ^10.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@cspell/dict-software-terms": "^5.4.11",
|
|
40
|
+
"@eslint/js": "^10.0.1",
|
|
41
|
+
"@eslint/markdown": "^9.0.0",
|
|
42
|
+
"c8": "^12.0.0",
|
|
43
|
+
"eslint": "^10.15.0",
|
|
44
|
+
"eslint-plugin-markdown-preferences": "^0.43.0",
|
|
45
|
+
"eslint-plugin-markdown-preferences-paragraph": "^0.2.0",
|
|
46
|
+
"eslint-plugin-prose-spellcheck": "^0.1.2",
|
|
47
|
+
"npm-package-release": "0.4.0",
|
|
48
|
+
"tinybench": "^6.2.5",
|
|
49
|
+
"title-case": "^4.3.2",
|
|
50
|
+
"yaml": "^2.9.1"
|
|
51
|
+
},
|
|
52
|
+
"overrides": {
|
|
53
|
+
"eslint-plugin-markdown-preferences": {
|
|
54
|
+
"@eslint/markdown": "$@eslint/markdown"
|
|
55
|
+
},
|
|
56
|
+
"eslint-plugin-markdown-preferences-paragraph": {
|
|
57
|
+
"@eslint/markdown": "$@eslint/markdown"
|
|
58
|
+
},
|
|
59
|
+
"eslint-plugin-prose-spellcheck": {
|
|
60
|
+
"@eslint/markdown": "$@eslint/markdown"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"repository": {
|
|
64
|
+
"type": "git",
|
|
65
|
+
"url": "git+https://github.com/dblock/eslint-plugin-markdown-links-unlinked.git"
|
|
66
|
+
},
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/dblock/eslint-plugin-markdown-links-unlinked/issues"
|
|
69
|
+
},
|
|
70
|
+
"homepage": "https://github.com/dblock/eslint-plugin-markdown-links-unlinked#readme",
|
|
71
|
+
"license": "MIT",
|
|
72
|
+
"author": "dblock",
|
|
73
|
+
"keywords": [
|
|
74
|
+
"eslint",
|
|
75
|
+
"eslintplugin",
|
|
76
|
+
"markdown",
|
|
77
|
+
"links",
|
|
78
|
+
"orphan",
|
|
79
|
+
"unlinked"
|
|
80
|
+
],
|
|
81
|
+
"c8": {
|
|
82
|
+
"include": [
|
|
83
|
+
"src/**/*.js",
|
|
84
|
+
"scripts/**/*.js"
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
"dependencies": {
|
|
88
|
+
"htmlparser2": "^12.0.0",
|
|
89
|
+
"mdast-util-from-markdown": "^2.1.0",
|
|
90
|
+
"mdast-util-frontmatter": "^2.0.1",
|
|
91
|
+
"mdast-util-gfm": "^3.1.0",
|
|
92
|
+
"micromark-extension-frontmatter": "^2.0.0",
|
|
93
|
+
"micromark-extension-gfm": "^3.0.0",
|
|
94
|
+
"picomatch": "^4.0.7"
|
|
95
|
+
}
|
|
96
|
+
}
|
package/src/graph.js
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { join, posix } from "node:path";
|
|
4
|
+
import picomatch from "picomatch";
|
|
5
|
+
import { extractLinks } from "./links.js";
|
|
6
|
+
|
|
7
|
+
const SKIPPED_DIRECTORIES = new Set(["node_modules", ".git"]);
|
|
8
|
+
const DIRECTORY_PAGES = ["README", "index"];
|
|
9
|
+
const SCHEME = /^[a-z][a-z\d+.-]*:/iu;
|
|
10
|
+
|
|
11
|
+
/** Milliseconds between sweeps of the file system for changes made outside ESLint. */
|
|
12
|
+
export const SWEEP_INTERVAL = 2000;
|
|
13
|
+
|
|
14
|
+
let now = () => Date.now();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {object} PageGraph
|
|
18
|
+
* @property {Set<string>} pages Discovered pages, as `/`-separated paths relative to the root directory.
|
|
19
|
+
* @property {string[]} roots Discovered pages matching the root patterns.
|
|
20
|
+
* @property {Set<string>} reachable Pages reachable from a root.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {object} GraphState
|
|
25
|
+
* @property {Map<string, PageEntry>} pages Each discovered page.
|
|
26
|
+
* @property {number} version Incremented whenever pages are added or removed, which invalidates
|
|
27
|
+
* every page's resolved targets.
|
|
28
|
+
* @property {number} sweptAt Clock time of the last sweep.
|
|
29
|
+
* @property {PageGraph | undefined} graph Reachability for the current pages, or `undefined` once edges change.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} PageEntry
|
|
34
|
+
* @property {string} signature File modification time and size.
|
|
35
|
+
* @property {string} digest Digest of the text the links were parsed from.
|
|
36
|
+
* @property {string[]} links Link destinations.
|
|
37
|
+
* @property {string[]} [targets] Pages the links resolve to, valid while `targetsVersion` matches.
|
|
38
|
+
* @property {number} [targetsVersion] State version the targets were resolved against.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** @type {Map<string, GraphState>} */
|
|
42
|
+
const states = new Map();
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns the link graph for a root directory and options. The graph is built once per process,
|
|
46
|
+
* swept for changes made outside ESLint at most every {@link SWEEP_INTERVAL} milliseconds, and
|
|
47
|
+
* updated from the text of each linted page so its current links count without reading it from disk.
|
|
48
|
+
* @param {string} rootDir Absolute root directory.
|
|
49
|
+
* @param {{roots: string[], ignore: string[], extensions: string[]}} options Normalized options.
|
|
50
|
+
* @param {{page: string, text: string}} [live] The linted page and its current text.
|
|
51
|
+
* @returns {PageGraph} The page graph.
|
|
52
|
+
*/
|
|
53
|
+
export function getPageGraph(rootDir, options, live) {
|
|
54
|
+
const key = JSON.stringify([rootDir, options.roots, options.ignore, options.extensions]);
|
|
55
|
+
let state = states.get(key);
|
|
56
|
+
let parsedLive = false;
|
|
57
|
+
if (!state) {
|
|
58
|
+
state = { pages: new Map(), sweptAt: now(), graph: undefined, version: 0 };
|
|
59
|
+
states.set(key, state);
|
|
60
|
+
parsedLive = sweep(rootDir, options, state, live);
|
|
61
|
+
} else if (now() - state.sweptAt >= SWEEP_INTERVAL) {
|
|
62
|
+
state.sweptAt = now();
|
|
63
|
+
parsedLive = sweep(rootDir, options, state, live);
|
|
64
|
+
}
|
|
65
|
+
if (live && !parsedLive) applyLive(rootDir, options, state, live);
|
|
66
|
+
state.graph ??= buildGraph(state.pages, options, state.version);
|
|
67
|
+
return state.graph;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Clears all graphs. */
|
|
71
|
+
export function clearPageGraphCache() {
|
|
72
|
+
states.clear();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Replaces the clock that throttles sweeps. For tests.
|
|
77
|
+
* @param {() => number} clock Returns the current time in milliseconds.
|
|
78
|
+
* @returns {() => void} Restores the previous clock.
|
|
79
|
+
*/
|
|
80
|
+
export function setClock(clock) {
|
|
81
|
+
const previous = now;
|
|
82
|
+
now = clock;
|
|
83
|
+
return () => {
|
|
84
|
+
now = previous;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Walks the root directory and brings the state up to date: pages that disappeared are dropped,
|
|
90
|
+
* and pages that are new or whose modification time or size changed are parsed again unless their
|
|
91
|
+
* text is unchanged. The linted
|
|
92
|
+
* page is parsed from its live text instead of from disk.
|
|
93
|
+
* @returns {boolean} Whether the linted page was parsed.
|
|
94
|
+
*/
|
|
95
|
+
function sweep(rootDir, options, state, live) {
|
|
96
|
+
const found = discover(rootDir, options);
|
|
97
|
+
for (const page of state.pages.keys()) {
|
|
98
|
+
if (!found.has(page)) {
|
|
99
|
+
state.pages.delete(page);
|
|
100
|
+
state.graph = undefined;
|
|
101
|
+
state.version++;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
let parsedLive = false;
|
|
105
|
+
for (const [page, signature] of found) {
|
|
106
|
+
const entry = state.pages.get(page);
|
|
107
|
+
if (entry?.signature === signature) continue;
|
|
108
|
+
const isLive = live?.page === page;
|
|
109
|
+
parsedLive ||= isLive;
|
|
110
|
+
const text = isLive ? live.text : read(join(rootDir, page));
|
|
111
|
+
const textDigest = text === undefined ? "" : digest(text);
|
|
112
|
+
if (entry && text !== undefined && entry.digest === textDigest) {
|
|
113
|
+
entry.signature = signature;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (!entry) state.version++;
|
|
117
|
+
state.pages.set(page, { signature, digest: textDigest, links: text === undefined ? [] : extractLinks(text) });
|
|
118
|
+
state.graph = undefined;
|
|
119
|
+
}
|
|
120
|
+
return parsedLive;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Replaces the linted page's links with those in its current text, adding the page when it is on
|
|
125
|
+
* disk but appeared since the last sweep. Text without a qualifying file is ignored, and text whose
|
|
126
|
+
* digest matches the text the page's links were last parsed from is not parsed again.
|
|
127
|
+
*/
|
|
128
|
+
function applyLive(rootDir, options, state, { page, text }) {
|
|
129
|
+
let entry = state.pages.get(page);
|
|
130
|
+
if (!entry) {
|
|
131
|
+
const signature = pageSignature(rootDir, page, options);
|
|
132
|
+
if (signature === undefined) return;
|
|
133
|
+
entry = { signature, digest: "", links: [] };
|
|
134
|
+
state.pages.set(page, entry);
|
|
135
|
+
state.graph = undefined;
|
|
136
|
+
state.version++;
|
|
137
|
+
}
|
|
138
|
+
const textDigest = digest(text);
|
|
139
|
+
if (textDigest === entry.digest) return;
|
|
140
|
+
entry.digest = textDigest;
|
|
141
|
+
const links = extractLinks(text);
|
|
142
|
+
if (links.length === entry.links.length && links.every((link, index) => link === entry.links[index])) return;
|
|
143
|
+
entry.links = links;
|
|
144
|
+
entry.targets = undefined;
|
|
145
|
+
state.graph = undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Walks the root directory for pages and their signatures. Symbolic links are not followed.
|
|
150
|
+
* @returns {Map<string, string>} Signature of each page.
|
|
151
|
+
*/
|
|
152
|
+
function discover(rootDir, { ignore, extensions }) {
|
|
153
|
+
const isIgnored = picomatch(ignore, { dot: true });
|
|
154
|
+
const pages = new Map();
|
|
155
|
+
const walk = (relative) => {
|
|
156
|
+
let entries;
|
|
157
|
+
try {
|
|
158
|
+
entries = readdirSync(relative ? join(rootDir, relative) : rootDir, { withFileTypes: true });
|
|
159
|
+
} catch {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
for (const entry of entries) {
|
|
163
|
+
const path = relative ? `${relative}/${entry.name}` : entry.name;
|
|
164
|
+
if (entry.isDirectory()) {
|
|
165
|
+
if (!SKIPPED_DIRECTORIES.has(entry.name)) walk(path);
|
|
166
|
+
} else if (entry.isFile() && extensions.some((extension) => entry.name.endsWith(extension))
|
|
167
|
+
&& !isIgnored(path)) {
|
|
168
|
+
const value = signature(join(rootDir, path));
|
|
169
|
+
if (value !== undefined) pages.set(path, value);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
walk("");
|
|
174
|
+
return pages;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns the signature of a page that discovery would find, or `undefined` when the path is not
|
|
179
|
+
* a page: a skipped or ignored path, another extension, a missing file, or one reached through a
|
|
180
|
+
* symbolic link.
|
|
181
|
+
*/
|
|
182
|
+
function pageSignature(rootDir, page, { ignore, extensions }) {
|
|
183
|
+
const segments = page.split("/");
|
|
184
|
+
const name = segments.at(-1);
|
|
185
|
+
if (segments.slice(0, -1).some((segment) => SKIPPED_DIRECTORIES.has(segment))) return undefined;
|
|
186
|
+
if (!extensions.some((extension) => name.endsWith(extension))) return undefined;
|
|
187
|
+
if (picomatch(ignore, { dot: true })(page)) return undefined;
|
|
188
|
+
let path = rootDir;
|
|
189
|
+
for (const [index, segment] of segments.entries()) {
|
|
190
|
+
path = join(path, segment);
|
|
191
|
+
let stats;
|
|
192
|
+
try {
|
|
193
|
+
stats = lstatSync(path);
|
|
194
|
+
} catch {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
if (index === segments.length - 1 ? !stats.isFile() : !stats.isDirectory()) return undefined;
|
|
198
|
+
}
|
|
199
|
+
return signature(path);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Returns a file's modification time and size, or `undefined` when it cannot be read. */
|
|
203
|
+
function signature(path) {
|
|
204
|
+
try {
|
|
205
|
+
const stats = statSync(path);
|
|
206
|
+
return `${stats.mtimeMs}:${stats.size}`;
|
|
207
|
+
} catch {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Returns a digest of a page's text, so text that was already parsed, typically the linted page
|
|
214
|
+
* read from disk by the sweep moments earlier, is not parsed again.
|
|
215
|
+
* @param {string} text Page text.
|
|
216
|
+
* @returns {string} The digest.
|
|
217
|
+
*/
|
|
218
|
+
function digest(text) {
|
|
219
|
+
return createHash("sha256").update(text).digest("base64");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function read(path) {
|
|
223
|
+
try {
|
|
224
|
+
return readFileSync(path, "utf8");
|
|
225
|
+
} catch {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Computes which pages are reachable from the roots by a breadth-first search over the links.
|
|
232
|
+
* When a version is given, each page's resolved targets are cached on its entry and reused by
|
|
233
|
+
* later searches with the same version, so only pages whose links changed are resolved again.
|
|
234
|
+
* @param {Map<string, {links: string[], targets?: string[], targetsVersion?: number}>} pageLinks
|
|
235
|
+
* Each discovered page's link destinations.
|
|
236
|
+
* @param {{roots: string[], extensions: string[]}} options Normalized options.
|
|
237
|
+
* @param {number} [version] Version of the page set, to cache resolved targets against.
|
|
238
|
+
* @returns {PageGraph} The page graph.
|
|
239
|
+
*/
|
|
240
|
+
export function buildGraph(pageLinks, { roots: rootPatterns, extensions }, version) {
|
|
241
|
+
const pages = new Set(pageLinks.keys());
|
|
242
|
+
const isRoot = picomatch(rootPatterns, { dot: true });
|
|
243
|
+
const roots = [...pages].filter((page) => isRoot(page));
|
|
244
|
+
const reachable = new Set(roots);
|
|
245
|
+
const queue = [...roots];
|
|
246
|
+
for (let index = 0; index < queue.length; index++) {
|
|
247
|
+
const page = queue[index];
|
|
248
|
+
const entry = pageLinks.get(page);
|
|
249
|
+
let targets = version !== undefined && entry.targetsVersion === version ? entry.targets : undefined;
|
|
250
|
+
if (!targets) {
|
|
251
|
+
targets = [];
|
|
252
|
+
for (const destination of entry.links) targets.push(...resolveTargets(page, destination, pages, extensions));
|
|
253
|
+
if (version !== undefined) {
|
|
254
|
+
entry.targets = targets;
|
|
255
|
+
entry.targetsVersion = version;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
for (const target of targets) {
|
|
259
|
+
if (!reachable.has(target)) {
|
|
260
|
+
reachable.add(target);
|
|
261
|
+
queue.push(target);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return { pages, roots, reachable };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Resolves a link destination on a page to the discovered pages it reaches.
|
|
270
|
+
* @param {string} page Linking page.
|
|
271
|
+
* @param {string} destination Link destination as parsed from Markdown.
|
|
272
|
+
* @param {Set<string>} pages Discovered pages.
|
|
273
|
+
* @param {string[]} extensions Page extensions.
|
|
274
|
+
* @returns {string[]} Reached pages.
|
|
275
|
+
*/
|
|
276
|
+
export function resolveTargets(page, destination, pages, extensions) {
|
|
277
|
+
if (SCHEME.test(destination) || destination.startsWith("//")) return [];
|
|
278
|
+
const path = destination.replace(/[?#].*$/su, "");
|
|
279
|
+
if (path === "") return [];
|
|
280
|
+
let decoded;
|
|
281
|
+
try {
|
|
282
|
+
decoded = decodeURIComponent(path);
|
|
283
|
+
} catch {
|
|
284
|
+
decoded = path;
|
|
285
|
+
}
|
|
286
|
+
const joined = decoded.startsWith("/")
|
|
287
|
+
? posix.normalize(`.${decoded}`)
|
|
288
|
+
: posix.join(posix.dirname(page), decoded);
|
|
289
|
+
const target = joined.replace(/\/+$/u, "");
|
|
290
|
+
if (target === ".." || target.startsWith("../")) return [];
|
|
291
|
+
if (pages.has(target)) return [target];
|
|
292
|
+
const directory = target === "." || target === "" ? "" : `${target}/`;
|
|
293
|
+
const reached = [];
|
|
294
|
+
for (const name of DIRECTORY_PAGES) {
|
|
295
|
+
for (const extension of extensions) {
|
|
296
|
+
const candidate = `${directory}${name}${extension}`;
|
|
297
|
+
if (pages.has(candidate)) reached.push(candidate);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return reached;
|
|
301
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import noUnlinkedPages from "./rules/no-unlinked-pages.js";
|
|
3
|
+
|
|
4
|
+
const { name, version } = JSON.parse(
|
|
5
|
+
readFileSync(new URL("../package.json", import.meta.url), "utf8"),
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
const plugin = {
|
|
9
|
+
meta: { name, version, namespace: "markdown-links-unlinked" },
|
|
10
|
+
rules: { "no-unlinked-pages": noUnlinkedPages },
|
|
11
|
+
configs: {},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
plugin.configs.recommended = {
|
|
15
|
+
name: "markdown-links-unlinked/recommended",
|
|
16
|
+
plugins: { "markdown-links-unlinked": plugin },
|
|
17
|
+
rules: { "markdown-links-unlinked/no-unlinked-pages": "error" },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default plugin;
|
package/src/links.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Parser } from "htmlparser2";
|
|
2
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
3
|
+
import { frontmatterFromMarkdown } from "mdast-util-frontmatter";
|
|
4
|
+
import { gfmFromMarkdown } from "mdast-util-gfm";
|
|
5
|
+
import { frontmatter } from "micromark-extension-frontmatter";
|
|
6
|
+
import { gfm } from "micromark-extension-gfm";
|
|
7
|
+
|
|
8
|
+
const FRONTMATTER = ["yaml", "toml"];
|
|
9
|
+
|
|
10
|
+
const parseOptions = {
|
|
11
|
+
extensions: [gfm(), frontmatter(FRONTMATTER)],
|
|
12
|
+
mdastExtensions: [gfmFromMarkdown(), frontmatterFromMarkdown(FRONTMATTER)],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns the destinations of links a reader can follow from a Markdown document: inline links,
|
|
17
|
+
* link references that resolve to a definition, and HTML `<a href>` anchors. Images, image
|
|
18
|
+
* references, and unused definitions are not links.
|
|
19
|
+
* @param {string} text Markdown source.
|
|
20
|
+
* @returns {string[]} Link destinations in document order.
|
|
21
|
+
*/
|
|
22
|
+
export function extractLinks(text) {
|
|
23
|
+
const tree = fromMarkdown(text, parseOptions);
|
|
24
|
+
const destinations = [];
|
|
25
|
+
const definitions = new Map();
|
|
26
|
+
const references = [];
|
|
27
|
+
const html = [];
|
|
28
|
+
|
|
29
|
+
const visit = (node) => {
|
|
30
|
+
switch (node.type) {
|
|
31
|
+
case "link":
|
|
32
|
+
destinations.push(node.url);
|
|
33
|
+
break;
|
|
34
|
+
case "linkReference":
|
|
35
|
+
references.push(node.identifier);
|
|
36
|
+
break;
|
|
37
|
+
case "definition":
|
|
38
|
+
if (!definitions.has(node.identifier)) definitions.set(node.identifier, node.url);
|
|
39
|
+
break;
|
|
40
|
+
case "html":
|
|
41
|
+
html.push(node.value);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
for (const child of node.children ?? []) visit(child);
|
|
45
|
+
};
|
|
46
|
+
visit(tree);
|
|
47
|
+
|
|
48
|
+
for (const identifier of references) destinations.push(definitions.get(identifier));
|
|
49
|
+
for (const value of html) destinations.push(...extractAnchors(value));
|
|
50
|
+
return destinations;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns the `href` values of `<a>` elements in an HTML fragment.
|
|
55
|
+
* @param {string} value HTML source.
|
|
56
|
+
* @returns {string[]} Anchor destinations.
|
|
57
|
+
*/
|
|
58
|
+
function extractAnchors(value) {
|
|
59
|
+
const hrefs = [];
|
|
60
|
+
const parser = new Parser({
|
|
61
|
+
onopentag(name, attributes) {
|
|
62
|
+
if (name === "a" && typeof attributes.href === "string") hrefs.push(attributes.href);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
parser.write(value);
|
|
66
|
+
parser.end();
|
|
67
|
+
return hrefs;
|
|
68
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
2
|
+
import picomatch from "picomatch";
|
|
3
|
+
import { getPageGraph } from "../graph.js";
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_OPTIONS = Object.freeze({
|
|
6
|
+
roots: Object.freeze(["README.md"]),
|
|
7
|
+
allow: Object.freeze(["CHANGELOG.md", "LICENSE.md", "CODE_OF_CONDUCT.md", "SECURITY.md", ".github/**"]),
|
|
8
|
+
ignore: Object.freeze([]),
|
|
9
|
+
extensions: Object.freeze([".md"]),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
/** @type {Map<string, (path: string) => boolean>} */
|
|
13
|
+
const allowMatchers = new Map();
|
|
14
|
+
|
|
15
|
+
/** Returns a cached matcher for `allow` patterns, compiled once per distinct list. */
|
|
16
|
+
function allowMatcher(allow) {
|
|
17
|
+
const key = JSON.stringify(allow);
|
|
18
|
+
let matcher = allowMatchers.get(key);
|
|
19
|
+
if (!matcher) {
|
|
20
|
+
matcher = picomatch(allow, { dot: true });
|
|
21
|
+
allowMatchers.set(key, matcher);
|
|
22
|
+
}
|
|
23
|
+
return matcher;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const patterns = { type: "array", items: { type: "string", minLength: 1 }, uniqueItems: true };
|
|
27
|
+
|
|
28
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
29
|
+
export default {
|
|
30
|
+
meta: {
|
|
31
|
+
type: "suggestion",
|
|
32
|
+
docs: {
|
|
33
|
+
description: "Disallow Markdown pages that are not linked from any root page",
|
|
34
|
+
url: "https://github.com/dblock/eslint-plugin-markdown-links-unlinked#configuration",
|
|
35
|
+
},
|
|
36
|
+
languages: ["markdown/*"],
|
|
37
|
+
schema: [{
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: {
|
|
40
|
+
roots: { ...patterns, minItems: 1 },
|
|
41
|
+
allow: patterns,
|
|
42
|
+
ignore: patterns,
|
|
43
|
+
extensions: {
|
|
44
|
+
type: "array",
|
|
45
|
+
items: { type: "string", pattern: "^\\.[^/\\\\*?{}\\[\\]!]+$" },
|
|
46
|
+
minItems: 1,
|
|
47
|
+
uniqueItems: true,
|
|
48
|
+
},
|
|
49
|
+
rootDir: { type: "string", minLength: 1 },
|
|
50
|
+
},
|
|
51
|
+
additionalProperties: false,
|
|
52
|
+
}],
|
|
53
|
+
messages: {
|
|
54
|
+
unlinked: "This page is not linked from any root page ({{roots}}).",
|
|
55
|
+
missingRoot: "No root page found: {{roots}}.",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
create(context) {
|
|
60
|
+
const options = { ...DEFAULT_OPTIONS, ...context.options[0] };
|
|
61
|
+
const rootDir = resolve(context.cwd, options.rootDir ?? ".");
|
|
62
|
+
const path = relative(rootDir, context.filename);
|
|
63
|
+
if (path === "" || path === ".." || path.startsWith(`..${sep}`) || isAbsolute(path)) return {};
|
|
64
|
+
const page = path.split(sep).join("/");
|
|
65
|
+
if (allowMatcher(options.allow)(page)) return {};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
root(node) {
|
|
69
|
+
const graph = getPageGraph(rootDir, options, { page, text: context.sourceCode.text });
|
|
70
|
+
if (!graph.pages.has(page) || graph.reachable.has(page)) return;
|
|
71
|
+
const roots = options.roots.join(", ");
|
|
72
|
+
context.report({
|
|
73
|
+
loc: node.position.start,
|
|
74
|
+
messageId: graph.roots.length === 0 ? "missingRoot" : "unlinked",
|
|
75
|
+
data: { roots },
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
};
|