Skip to content

Commit 36833c7

Browse files
authored
Merge pull request #14 from fulldecent/fix-issue-13-skip-canonical-alternate-links
Fix #13: Skip canonical and alternate links in external link validation
2 parents 088a0be + 24ad112 commit 36833c7

5 files changed

Lines changed: 35 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ This rule has no configurable options.
119119

120120
Validates that all external links are live and accessible. This rule helps maintain website quality by catching broken external links before they go live, improving user experience and SEO.
121121

122+
**Note:** This rule automatically skips validation of:
123+
124+
- `<link rel="canonical">` - Canonical URLs point to the site itself and may not be published yet during development/preview
125+
- `<link rel="alternate">` - Alternate language URLs also point to the site itself and may not exist during development
126+
127+
This allows you to validate your HTML before publishing, even when the canonical and alternate URLs reference the final production URLs.
128+
122129
```diff
123130
- <a href="https://wrong-subdomain.example.com">This link is broken</a>
124131
+ <a href="https://example.com/nonexistent-page">This link works</a>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@
8383
"registry": "https://registry.npmjs.org/",
8484
"access": "public"
8585
}
86-
}
86+
}

src/rules/ExternalLinksRule.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ export default class ExternalLinksRule extends Rule<void, RuleOptions> {
187187
return
188188
}
189189

190+
// Skip canonical and alternate language links as they point to the site itself
191+
// and may reference not-yet-published URLs during development/preview
192+
if (tagName === 'link') {
193+
const rel = target.getAttribute('rel')?.value
194+
if (rel === 'canonical' || rel === 'alternate') {
195+
return
196+
}
197+
}
198+
190199
const rawUrl = target.getAttribute(urlAttribute)?.value
191200
if (typeof rawUrl !== 'string' || !rawUrl) {
192201
return
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Test: Canonical and Alternate Links Should Be Skipped</title>
6+
<!-- These should NOT trigger external link validation errors, even though they point to non-existent URLs -->
7+
<link rel="canonical" href="https://example.com/this-does-not-exist-yet" />
8+
<link rel="alternate" hreflang="es" href="https://example.com/es/not-published-yet" />
9+
<link rel="alternate" hreflang="fr" href="https://example.com/fr/also-not-published" />
10+
</head>
11+
12+
<body>
13+
<p>
14+
This page tests that canonical and alternate links are skipped from external link validation.
15+
</p>
16+
</body>
17+
</html>

tests/fixtures/required-reports.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,6 @@
204204
"ruleUrl": "https://github.com/fulldecent/html-validate-nice-checkers/blob/main/README.m#rules"
205205
}
206206
],
207+
"tests/fixtures/ExternalLinkRule-skip-canonical-alternate.html": [],
207208
"tests/fixtures/no-errors.html": []
208209
}

0 commit comments

Comments
 (0)