Skip to content

Commit

Permalink
Merge pull request #51 from Raza403/features/github-pages-template-ad…
Browse files Browse the repository at this point in the history
…d-canonical-test#5497

github-pages-template added canonical test.
  • Loading branch information
Raza403 authored Nov 17, 2023
2 parents 5d37eea + 0416ae9 commit a01cba0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"license": "UNLICENSED",
"devDependencies": {
"better-sqlite3": "^9.0.0",
"better-sqlite3": "^9.1.1",
"glob": "^10.3.10",
"html-validate": "^8.7.0"
},
Expand Down
58 changes: 58 additions & 0 deletions test/plugin.html-validate.canonical.js
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,
};
3 changes: 3 additions & 0 deletions test/plugin.html-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { definePlugin } = require("html-validate");
const { rules: mailtoAwesomeRules } = require("./plugin.html-validate.mailto-awesome.js");
const { rules: externalLinksRules } = require("./plugin.html-validate.external-links.js");
const { rules: noJqueryRules } = require("./plugin.html-validate.no-jquery.js");
const { rules: ValidateCanonicalRules } = require("./plugin.html-validate.canonical.js");
const { rules: latestPackagesRules } = require("./plugin.html-validate.latest-packages.js");

module.exports = definePlugin({
Expand All @@ -18,6 +19,7 @@ module.exports = definePlugin({
...mailtoAwesomeRules,
...externalLinksRules,
...noJqueryRules,
...ValidateCanonicalRules,
...latestPackagesRules,
},
configs: {
Expand All @@ -26,6 +28,7 @@ module.exports = definePlugin({
"pacific-medical-training/mailto-awesome": "error",
"pacific-medical-training/external-links": "error",
"pacific-medical-training/no-jquery": "error",
"pacific-medical-training/canonical": "error",
"pacific-medical-training/latest-packages": "error",
},
},
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ base64-js@^1.3.1:
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

better-sqlite3@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/better-sqlite3/-/better-sqlite3-9.0.0.tgz#bca6026fa1e9e5af62bfef448a7d8402d4549958"
integrity sha512-lDxQ9qg/XuUHZG6xzrQaMHkNWl37t35/LPB/VJGV8DdScSuGFNfFSqgscXEd8UIuyk/d9wU8iaMxQa4If5Wqog==
better-sqlite3@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/better-sqlite3/-/better-sqlite3-9.1.1.tgz#f139b180a08ed396e660a0601a46ceefd78b832c"
integrity sha512-FhW7bS7cXwkB2SFnPJrSGPmQerVSCzwBgmQ1cIRcYKxLsyiKjljzCbyEqqhYXo5TTBqt5BISiBj2YE2Sy2ynaA==
dependencies:
bindings "^1.5.0"
prebuild-install "^7.1.1"
Expand Down

0 comments on commit a01cba0

Please sign in to comment.