astro-better-link-checker 1.0.0 → 1.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/README.md +63 -30
- package/index.mjs +25 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# astro-better-link-checker
|
|
2
2
|
|
|
3
|
-
Fast intra-site broken link checker for Astro.
|
|
3
|
+
Fast intra-site broken link checker for Astro. The checker runs after `astro build` and reports every link, image, and anchor that doesn't resolve in the build output.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
The checker skips external links. For external links, point a tool such as [lychee](https://github.com/lycheeverse/lychee) at your deployed site.
|
|
6
6
|
|
|
7
|
-
## Why
|
|
7
|
+
## Why Faster
|
|
8
8
|
|
|
9
|
-
The common approach checks links per
|
|
9
|
+
The common approach checks links per file. When 400 pages link to `/docs/get-started`, the common approach checks `/docs/get-started` 400 times. This plugin deduplicates first and checks every unique destination once. File reads and existence checks run in parallel.
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
@@ -28,30 +28,24 @@ export default defineConfig({
|
|
|
28
28
|
|
|
29
29
|
## Options
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
failOnBrokenLinks: true,
|
|
41
|
-
|
|
42
|
-
// Log every checked href, not just broken ones (default: false)
|
|
43
|
-
verbose: false,
|
|
44
|
-
})
|
|
45
|
-
```
|
|
31
|
+
| Option | Type | Default | Description |
|
|
32
|
+
|--------|------|---------|-------------|
|
|
33
|
+
| `excludeSourcePages` | `(string\|RegExp)[]` | `[]` | Pages to skip crawling. The checker ignores every link on a matching page. |
|
|
34
|
+
| `excludeDestinations` | `(string\|RegExp)[]` | `[]` | Destinations to skip checking, including anchors on those destinations. |
|
|
35
|
+
| `failOnBrokenLinks` | `boolean` | `true` | Exit the build with code `1` when the checker finds a broken link, image, or anchor. |
|
|
36
|
+
| `checkAnchors` | `boolean` | `true` | Check each `#fragment` against the `id` and `<a name>` attributes on the target page. |
|
|
37
|
+
| `checkImages` | `boolean` | `true` | Check `src` on `<img>`, `<source>`, `<video>`, and `<audio>`, and `srcset` on any element. |
|
|
38
|
+
| `trailingSlash` | `'ignore' \| 'directory'` | `'ignore'` | With `'directory'`, flag a link without a trailing slash whose target is served only as `<folder>/index.html`. Use `'directory'` when your host redirects folder URLs to the trailing-slash form. A trailing slash on a link to a `<page>.html` page is always a broken link. |
|
|
39
|
+
| `verbose` | `boolean` | `false` | Log every checked link, image, and anchor, not only broken ones. |
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
`excludeSourcePages` and `excludeDestinations` accept strings and `RegExp` objects:
|
|
48
42
|
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
* A string matches by prefix. `'/login'` excludes every path that starts with `/login`, such as `/login/callback`. The string works like the regex `/^\/login/`.
|
|
44
|
+
* A `RegExp` tests the full path. Use a regex for a pattern that doesn't start at the beginning of the path, or for an exact match.
|
|
51
45
|
|
|
52
46
|
```ts
|
|
53
47
|
excludeDestinations: [
|
|
54
|
-
'/login', // prefix: skips /login, /login/callback, /login?next
|
|
48
|
+
'/login', // prefix: skips /login, /login/callback, /login?next=...
|
|
55
49
|
'/external-tool/', // prefix: skips /external-tool/ and everything beneath it
|
|
56
50
|
/\/beta\//, // regex: skips any path containing /beta/ (not anchored)
|
|
57
51
|
/\.(pdf|zip)$/, // regex: skips paths ending in .pdf or .zip
|
|
@@ -61,6 +55,8 @@ excludeDestinations: [
|
|
|
61
55
|
|
|
62
56
|
## Output
|
|
63
57
|
|
|
58
|
+
A clean build prints the following summary:
|
|
59
|
+
|
|
64
60
|
```
|
|
65
61
|
[link-checker] 4231 HTML files
|
|
66
62
|
[link-checker] 8847 unique destinations
|
|
@@ -68,20 +64,57 @@ excludeDestinations: [
|
|
|
68
64
|
[link-checker] all links ok
|
|
69
65
|
```
|
|
70
66
|
|
|
71
|
-
On failure
|
|
67
|
+
On failure, the checker lists each broken destination with up to five source pages. Broken anchors include the fragment, and broken images start with `img`:
|
|
72
68
|
|
|
73
69
|
```
|
|
74
|
-
[link-checker]
|
|
70
|
+
[link-checker] 3 broken links, 1 broken image:
|
|
71
|
+
/docs/api/old-endpoint
|
|
72
|
+
/docs/api/index
|
|
73
|
+
/docs/get-started#missing-section
|
|
74
|
+
/docs/overview
|
|
75
75
|
/docs/get-started/missing-page
|
|
76
76
|
/docs/overview
|
|
77
77
|
/docs/quickstarts/react
|
|
78
78
|
… and 12 more
|
|
79
|
-
/
|
|
80
|
-
/docs/
|
|
79
|
+
img /img/missing.png
|
|
80
|
+
/docs/overview
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
## Notes
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
* The checker resolves a relative href against the source file's location, then normalizes the href to a root-relative path.
|
|
86
|
+
* A link to a path resolves when the build output contains the path as a file, as `<path>.html`, or as `<path>/index.html`.
|
|
87
|
+
* A fragment-only link, such as `#options`, resolves against the current page.
|
|
88
|
+
* When a target page doesn't exist, the checker reports the page once and skips the anchor checks for links to the page.
|
|
89
|
+
|
|
90
|
+
## Markdown Link Syntax Checker
|
|
91
|
+
|
|
92
|
+
The package also exports `markdownLinkSyntaxChecker`, which scans the `.md` and `.mdx` files in `srcDir` for malformed links. A malformed link renders as plain text, so the HTML link checker never sees the link. The syntax checker reports the following mistakes:
|
|
93
|
+
|
|
94
|
+
* `missing-close-bracket`: a missing `]` before the URL, as in `[text(/docs/page)`.
|
|
95
|
+
* `space-before-href`: whitespace between `]` and `(`, as in `[text] (/docs/page)`.
|
|
96
|
+
|
|
97
|
+
The syntax checker skips front matter, code blocks, inline code, comments, import and export lines, and JSX string attributes.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
// astro.config.ts
|
|
101
|
+
import { defineConfig } from 'astro/config';
|
|
102
|
+
import linkChecker, { markdownLinkSyntaxChecker } from 'astro-better-link-checker';
|
|
103
|
+
|
|
104
|
+
export default defineConfig({
|
|
105
|
+
integrations: [markdownLinkSyntaxChecker(), linkChecker()],
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
| Option | Type | Default | Description |
|
|
110
|
+
|--------|------|---------|-------------|
|
|
111
|
+
| `excludePaths` | `(string\|RegExp)[]` | `[]` | Files to skip, matched against the path relative to `srcDir`. |
|
|
112
|
+
| `extensions` | `string[]` | `['.md', '.mdx']` | File extensions to scan. |
|
|
113
|
+
| `failOnIssues` | `boolean` | `true` | Exit the build with code `1` when the syntax checker finds an issue. |
|
|
114
|
+
|
|
115
|
+
The syntax checker prints each issue with the file, line, column, and kind:
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
[md-link-syntax] 1 issue:
|
|
119
|
+
content/docs/setup.mdx:12:5 [space-before-href] [Setup] (/docs/setup)
|
|
120
|
+
```
|
package/index.mjs
CHANGED
|
@@ -30,6 +30,8 @@
|
|
|
30
30
|
* failOnBrokenLinks {boolean} throw on any broken link (default: true)
|
|
31
31
|
* checkAnchors {boolean} validate #fragment targets (default: true)
|
|
32
32
|
* checkImages {boolean} validate img src/srcset paths (default: true)
|
|
33
|
+
* trailingSlash {'ignore'|'directory'} 'directory' flags links without a trailing slash
|
|
34
|
+
* to pages served as <dir>/index.html (default: 'ignore')
|
|
33
35
|
* verbose {boolean} log every checked href/src (default: false)
|
|
34
36
|
*/
|
|
35
37
|
|
|
@@ -186,6 +188,18 @@ async function checkExists(norm, buildDir) {
|
|
|
186
188
|
return plain || html || idx;
|
|
187
189
|
}
|
|
188
190
|
|
|
191
|
+
// For trailingSlash: 'directory'. True when a link without a slash targets a page served only
|
|
192
|
+
// as <dir>/index.html. A slash on a <page>.html link already fails checkExists as broken.
|
|
193
|
+
async function missingSlash(norm, buildDir) {
|
|
194
|
+
if (!norm || norm === '/' || norm.endsWith('/')) return false;
|
|
195
|
+
// files such as /llms.txt or /img/a.png have no slash convention
|
|
196
|
+
if (/\.[a-z0-9]+$/i.test(norm.split('/').pop())) return false;
|
|
197
|
+
const base = join(buildDir, norm.slice(1));
|
|
198
|
+
const ok = p => access(p).then(() => true, () => false);
|
|
199
|
+
const [html, idx] = await Promise.all([ok(base + '.html'), ok(join(base, 'index.html'))]);
|
|
200
|
+
return idx && !html;
|
|
201
|
+
}
|
|
202
|
+
|
|
189
203
|
// Walk a directory for .md/.mdx files.
|
|
190
204
|
async function walkMarkdown(dir, extensions) {
|
|
191
205
|
let entries;
|
|
@@ -315,6 +329,7 @@ export default function linkChecker(opts = {}) {
|
|
|
315
329
|
failOnBrokenLinks = true,
|
|
316
330
|
checkAnchors = true,
|
|
317
331
|
checkImages = true,
|
|
332
|
+
trailingSlash = 'ignore',
|
|
318
333
|
verbose = false,
|
|
319
334
|
_exit = process.exit,
|
|
320
335
|
} = opts;
|
|
@@ -401,6 +416,9 @@ export default function linkChecker(opts = {}) {
|
|
|
401
416
|
const ok = await checkExists(norm, buildDir);
|
|
402
417
|
if (verbose) log(`[link-checker] ${ok ? '✓' : '✗'} ${norm}`);
|
|
403
418
|
if (!ok) broken.push({ href: norm, sources: [...sources].sort() });
|
|
419
|
+
else if (trailingSlash === 'directory') {
|
|
420
|
+
if (await missingSlash(norm, buildDir)) broken.push({ href: norm, sources: [...sources].sort(), slash: true });
|
|
421
|
+
}
|
|
404
422
|
}),
|
|
405
423
|
...[...imageMap.entries()].map(async ([norm, sources]) => {
|
|
406
424
|
const ok = await checkExists(norm, buildDir);
|
|
@@ -454,19 +472,23 @@ export default function linkChecker(opts = {}) {
|
|
|
454
472
|
|
|
455
473
|
broken.sort((a, b) => a.href.localeCompare(b.href));
|
|
456
474
|
const report = broken
|
|
457
|
-
.map(({ href, sources, image }) => {
|
|
458
|
-
const label = image ? `img ${href}`
|
|
475
|
+
.map(({ href, sources, image, slash }) => {
|
|
476
|
+
const label = image ? `img ${href}`
|
|
477
|
+
: slash ? `${href} (add a trailing slash: the page is a folder index)`
|
|
478
|
+
: href;
|
|
459
479
|
const shown = sources.slice(0, 5).join('\n ');
|
|
460
480
|
const more = sources.length > 5 ? `\n … and ${sources.length - 5} more` : '';
|
|
461
481
|
return ` ${label}\n ${shown}${more}`;
|
|
462
482
|
})
|
|
463
483
|
.join('\n');
|
|
464
484
|
|
|
465
|
-
const brokenLinks = broken.filter(b => !b.image).length;
|
|
485
|
+
const brokenLinks = broken.filter(b => !b.image && !b.slash).length;
|
|
466
486
|
const brokenImages = broken.filter(b => b.image).length;
|
|
487
|
+
const slashLinks = broken.filter(b => b.slash).length;
|
|
467
488
|
const summary = [
|
|
468
489
|
brokenLinks && `${brokenLinks} broken link${brokenLinks === 1 ? '' : 's'}`,
|
|
469
490
|
brokenImages && `${brokenImages} broken image${brokenImages === 1 ? '' : 's'}`,
|
|
491
|
+
slashLinks && `${slashLinks} link${slashLinks === 1 ? '' : 's'} missing a trailing slash`,
|
|
470
492
|
].filter(Boolean).join(', ');
|
|
471
493
|
const msg = `[link-checker] ${summary}:\n${report}`;
|
|
472
494
|
log(msg);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-better-link-checker",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Fast intra-site broken link and image checker for Astro — deduplicates destinations and checks each one exactly once",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
15
|
"url": "git+https://github.com/better-static-sites/better-static-sites.github.io.git",
|
|
16
|
-
"directory": "
|
|
16
|
+
"directory": "lib/link-checker"
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://better-static-sites.github.io",
|
|
19
19
|
"bugs": {
|