-
-
Notifications
You must be signed in to change notification settings - Fork 429
feat: Add rule link-rel-canonical-require #1721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { Block, Listener } from '../htmlparser' | ||
| import { Rule } from '../types' | ||
|
|
||
| export default { | ||
| id: 'link-rel-canonical-require', | ||
| description: | ||
| '<link rel="canonical"> with non-blank href must be present in <head> tag.', | ||
| init(parser, reporter) { | ||
| let headSeen = false | ||
| let linkCanonicalSeen = false | ||
| let linkCanonicalHref = '' | ||
| let headEvent: Block | null = null | ||
|
|
||
| const onTagStart: Listener = (event) => { | ||
| const tagName = event.tagName.toLowerCase() | ||
| if (tagName === 'head') { | ||
| headSeen = true | ||
| headEvent = event | ||
| } else if (tagName === 'link') { | ||
| const mapAttrs = parser.getMapAttrs(event.attrs) | ||
| if (mapAttrs['rel'] && mapAttrs['rel'].toLowerCase() === 'canonical') { | ||
| linkCanonicalSeen = true | ||
| linkCanonicalHref = mapAttrs['href'] || '' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| parser.addListener('tagstart', onTagStart) | ||
| parser.addListener('end', () => { | ||
| if (headSeen && headEvent) { | ||
| if (!linkCanonicalSeen) { | ||
| reporter.error( | ||
| '<link rel="canonical"> must be present in <head> tag.', | ||
| headEvent.line, | ||
| headEvent.col, | ||
| this, | ||
| headEvent.raw | ||
| ) | ||
| } else if (linkCanonicalHref.trim() === '') { | ||
| reporter.error( | ||
| '<link rel="canonical"> href attribute must not be empty.', | ||
| headEvent.line, | ||
| headEvent.col, | ||
| this, | ||
| headEvent.raw | ||
| ) | ||
| } | ||
| } | ||
| }) | ||
| }, | ||
| } as Rule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
| const ruleId = 'link-rel-canonical-require' | ||
|
|
||
| describe('Rule: link-rel-canonical-require', () => { | ||
coliff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| it('should not report an error when a valid canonical link is present', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href="https://example.com/dresses/green-dresses"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link has uppercase rel attribute', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="CANONICAL" href="https://example.com/dresses/green-dresses"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link has mixed case rel attribute', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="Canonical" href="https://example.com/dresses/green-dresses"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should report an error when canonical link is missing', () => { | ||
| const code = `<!DOCTYPE html><html><head></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(1) | ||
| expect(messages[0].message).toBe( | ||
| '<link rel="canonical"> must be present in <head> tag.' | ||
| ) | ||
| }) | ||
|
|
||
| it('should report an error when canonical link href is blank', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href=""></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(1) | ||
| expect(messages[0].message).toBe( | ||
| '<link rel="canonical"> href attribute must not be empty.' | ||
| ) | ||
| }) | ||
|
|
||
| it('should report an error when canonical link href is only whitespace', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href=" "></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(1) | ||
| expect(messages[0].message).toBe( | ||
| '<link rel="canonical"> href attribute must not be empty.' | ||
| ) | ||
| }) | ||
|
|
||
| it('should report an error when canonical link href is missing', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(1) | ||
| expect(messages[0].message).toBe( | ||
| '<link rel="canonical"> href attribute must not be empty.' | ||
| ) | ||
| }) | ||
|
|
||
| it('should not report an error for other link tags', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="stylesheet" href="style.css"><link rel="canonical" href="https://example.com/dresses/green-dresses"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should report an error when only other link tags are present', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="stylesheet" href="style.css"><link rel="icon" href="favicon.ico"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(1) | ||
| expect(messages[0].message).toBe( | ||
| '<link rel="canonical"> must be present in <head> tag.' | ||
| ) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link is present with other meta tags', () => { | ||
| const code = `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="description" content="A description"><link rel="canonical" href="https://example.com/dresses/green-dresses"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link has relative URL', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href="/dresses/green-dresses"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link has query parameters', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href="https://example.com/dresses/green-dresses?color=green&size=m"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link has fragment', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href="https://example.com/dresses/green-dresses#section1"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('should not report an error when canonical link is self-referencing', () => { | ||
| const code = `<!DOCTYPE html><html><head><link rel="canonical" href="https://example.com/current-page"></head><body></body></html>` | ||
| const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
website/src/content/docs/rules/link-rel-canonical-require.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --- | ||
| id: link-rel-canonical-require | ||
| title: link-rel-canonical-require | ||
| description: Ensures every HTML document includes a canonical link tag within the head element for better SEO and duplicate content management. | ||
| sidebar: | ||
| badge: New | ||
| hidden: true | ||
| pagefind: false | ||
| --- | ||
|
|
||
| import { Badge } from '@astrojs/starlight/components'; | ||
|
|
||
| A `<link rel="canonical">` with non-blank href must be present in `<head>` tag. | ||
|
|
||
| Level: <Badge text="Error" variant="danger" /> | ||
|
|
||
| ## Config value | ||
|
|
||
| - `true`: enable rule | ||
| - `false`: disable rule | ||
|
|
||
| ## The following patterns are **not** considered rule violations | ||
|
|
||
| ```html | ||
| <!-- Valid canonical link with absolute URL --> | ||
| <html><head><link rel="canonical" href="https://example.com/dresses/green-dresses"></head></html> | ||
|
|
||
| <!-- Valid canonical link with relative URL --> | ||
| <html><head><link rel="canonical" href="/dresses/green-dresses"></head></html> | ||
|
|
||
| <!-- Valid canonical link with query parameters --> | ||
| <html><head><link rel="canonical" href="https://example.com/dresses/green-dresses?color=green&size=m"></head></html> | ||
|
|
||
| <!-- Valid canonical link with fragment --> | ||
| <html><head><link rel="canonical" href="https://example.com/dresses/green-dresses#section1"></head></html> | ||
| ``` | ||
|
|
||
| ## The following patterns are considered rule violations | ||
|
|
||
| ```html | ||
| <!-- Missing canonical link --> | ||
| <html><head></head></html> | ||
|
|
||
| <!-- Empty href attribute --> | ||
| <html><head><link rel="canonical" href=""></head></html> | ||
|
|
||
| <!-- Whitespace-only href attribute --> | ||
| <html><head><link rel="canonical" href=" "></head></html> | ||
|
|
||
| <!-- Missing href attribute --> | ||
| <html><head><link rel="canonical"></head></html> | ||
| ``` | ||
|
|
||
| ## Why this rule is important | ||
|
|
||
| While it's generally not critical to specify a canonical preference for your URLs, there are several reasons why you would want to explicitly tell search engines about a canonical page in a set of duplicate or similar pages: | ||
|
|
||
| - **Specify preferred URL**: Tell search engines which URL you want people to see in search results | ||
| - **Consolidate signals**: Help search engines consolidate signals from similar pages into a single, preferred URL | ||
| - **Simplify tracking**: Get consolidated metrics for specific content across multiple URLs | ||
| - **Optimize crawling**: Prevent search engines from wasting time crawling duplicate content |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.