-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Raza403/features/github-pages-template-ad…
…d-canonical-test#5497 github-pages-template added canonical test.
- Loading branch information
Showing
4 changed files
with
66 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,58 @@ | ||
// Import the Rule class from the "html-validate" module | ||
const { Rule } = require("html-validate"); | ||
|
||
// Define a custom rule named ValidateCanonical that extends the Rule class | ||
// This rule checks for head tags with link attributes that include rel="canonical" | ||
class ValidateCanonical extends Rule { | ||
// Set up the rule | ||
setup() { | ||
// Register an event listener for the "dom:ready" event | ||
this.on("dom:ready", this.domReady.bind(this)); | ||
} | ||
|
||
// Event handler for the "dom:ready" event | ||
domReady({ document }) { | ||
// Get all head elements in the document | ||
const headElements = document.getElementsByTagName("head"); | ||
|
||
// Iterate over each head element | ||
headElements.forEach((headElement) => { | ||
// Get the first <link> element within the <head> with rel="canonical" | ||
const linkElement = headElement.querySelector('link[rel="canonical"]'); | ||
|
||
// Skip to the next element if no <link rel="canonical"> is found | ||
if (!linkElement) { | ||
// Report a violation of the rule for head without a rel="canonical" | ||
this.report({ | ||
node: headElement, | ||
message: 'head without rel="canonical"', | ||
}); | ||
} else { | ||
// Check if href is extensionless (no .html, .php, etc.) | ||
const href = linkElement.getAttribute("href")?.value; | ||
if (href && /\.\w+$/.test(href)) { | ||
this.report({ | ||
node: linkElement, | ||
message: 'Canonical link href should be extensionless (no .html, .php, etc.)', | ||
}); | ||
} | ||
|
||
// Check if href is "/index" or ends with "/index" | ||
if (href && (href.toLowerCase() === "/index" || href.toLowerCase().endsWith("/index"))) { | ||
this.report({ | ||
node: linkElement, | ||
message: 'Canonical link href should be "/" and not end with "/index"', | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Export the ValidateCanonical rule as the module's default export | ||
module.exports = { ValidateCanonical }; | ||
|
||
// Export an object that maps the rule name to the ValidateCanonical rule | ||
module.exports.rules = { | ||
"pacific-medical-training/canonical": ValidateCanonical, | ||
}; |
This file contains 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 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