From 47ee17602e20c39204c3eeded665325da6d692e0 Mon Sep 17 00:00:00 2001 From: Wes Dean Date: Tue, 19 Nov 2024 12:08:51 -0500 Subject: [PATCH] Add site verification, tests; modularize validators --- .eleventy.js | 50 +++++++++++-------------------------- _includes/meta.html | 4 ++- js/environmentValidators.js | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 js/environmentValidators.js diff --git a/.eleventy.js b/.eleventy.js index 3907ba58..d0da9d62 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -10,6 +10,7 @@ const yaml = require("js-yaml"); const { sassPlugin } = require("esbuild-sass-plugin"); const svgSprite = require("eleventy-plugin-svg-sprite"); const { imageShortcode, imageWithClassShortcode } = require("./config"); +const environmentValidators = require("./js/environmentValidators.js"); require("dotenv").config(); @@ -66,36 +67,6 @@ module.exports = function (config) { const { hosts } = yaml.load(fs.readFileSync("./_data/site.yaml", "utf8")); - function isValidGitBranch(branch) { - const validGitBranch = /^[a-zA-Z0-9_\-\.\/]+$/; - return validGitBranch.test(branch); - } - - function isValidTwitterHandle(handle) { - const validTwitterHandle = /^\w{1,15}$/; - return validTwitterHandle.test(handle); - } - - function isValidDapAgency(agency) { - const validDapAgency = /^\w{1,15}$/; - return validDapAgency.test(agency); - } - - function isValidAnalyticsId(ga) { - const validAnalyticsId = /^(G|UA|YT|MO)-[a-zA-Z0-9-]+$/; - return validAnalyticsId.test(ga); - } - - function isValidSearchKey(accessKey) { - const validSearchKey = /^[0-9a-zA-Z]{1,}=*$/; - return validSearchKey.test(accessKey); - } - - function isValidSearchAffiliate(affiliate) { - const validSearchAffiliate = /^[0-9a-z-]{1,}$/; - return validSearchAffiliate.test(affiliate); - } - if (process.env.BRANCH && isValidGitBranch(process.env.BRANCH)) { switch (process.env.BRANCH) { case "main": @@ -112,35 +83,42 @@ module.exports = function (config) { config.addGlobalData("baseUrl", baseUrl); config.addGlobalData("site.baseUrl", baseUrl); - if (process.env.TWITTER && isValidTwitterHandle(process.env.TWITTER)) { + if (process.env.TWITTER && environmentValidators.isValidTwitterHandle(process.env.TWITTER)) { config.addGlobalData("site.twitter", process.env.TWITTER); } - if (process.env.DAP_AGENCY && isValidDapAgency(process.env.DAP_AGENCY)) { + if (process.env.DAP_AGENCY && environmentValidators.isValidDapAgency(process.env.DAP_AGENCY)) { config.addGlobalData("site.dap.agency", process.env.DAP_AGENCY); } if ( process.env.DAP_SUBAGENCY && - isValidDapAgency(process.env.DAP_SUBAGENCY) + environmentValidators.isValidDapAgency(process.env.DAP_SUBAGENCY) ) { config.addGlobalData("site.dap.subagency", process.env.DAP_SUBAGENCY); } - if (process.env.GA && isValidAnalyticsId(process.env.GA)) { + if (process.env.GA && environmentValidators.isValidAnalyticsId(process.env.GA)) { config.addGlobalData("site.ga", process.env.GA); } + if ( + process.env.GOOGLE_VERIFICATION_TOKEN && + environmentValidators.isValidVerificationToken (process.env.GOOGLE_VERIFICATION_TOKEN) + ) { + config.addGlobalData("site.google_verification_token", process.env.GOOGLE_VERIFICATION_TOKEN) + } + if ( process.env.SEARCH_ACCESS_KEY && - isValidSearchKey(process.env.SEARCH_ACCESS_KEY) + environmentValidators.isValidSearchKey(process.env.SEARCH_ACCESS_KEY) ) { config.addGlobalData("site.access_key", process.env.SEARCH_ACCESS_KEY); } if ( process.env.SEARCH_AFFILIATE && - isValidSearchAffiliate(process.env.SEARCH_AFFILIATE) + environmentValidators.isValidSearchAffiliate(process.env.SEARCH_AFFILIATE) ) { config.addGlobalData("site.affiliate", process.env.SEARCH_AFFILIATE); } diff --git a/_includes/meta.html b/_includes/meta.html index a04b3773..af9bca45 100644 --- a/_includes/meta.html +++ b/_includes/meta.html @@ -57,7 +57,9 @@ - + {%- if site.google_verification_token -%} + + {%- endif -%} diff --git a/js/environmentValidators.js b/js/environmentValidators.js new file mode 100644 index 00000000..8d46ce18 --- /dev/null +++ b/js/environmentValidators.js @@ -0,0 +1,44 @@ +function isValidGitBranch(branch) { + const validGitBranch = /^[a-zA-Z0-9_\-\.\/]+$/; + return validGitBranch.test(branch); +} + +function isValidTwitterHandle(handle) { + const validTwitterHandle = /^\w{1,15}$/; + return validTwitterHandle.test(handle); +} + +function isValidDapAgency(agency) { + const validDapAgency = /^\w{1,15}$/; + return validDapAgency.test(agency); +} + +function isValidAnalyticsId(ga) { + const validAnalyticsId = /^(G|UA|YT|MO)-[a-zA-Z0-9-]+$/; + return validAnalyticsId.test(ga); +} + +function isValidVerificationToken(token) { + const validToken = /^[a-zA-Z0-9_-]{42,44}$/; + return validToken.test(token); +} + +function isValidSearchKey(accessKey) { + const validSearchKey = /^[0-9a-zA-Z]{1,}=*$/; + return validSearchKey.test(accessKey); +} + +function isValidSearchAffiliate(affiliate) { + const validSearchAffiliate = /^[0-9a-z-]{1,}$/; + return validSearchAffiliate.test(affiliate); +} + +module.exports = { + isValidGitBranch, + isValidTwitterHandle, + isValidDapAgency, + isValidAnalyticsId, + isValidVerificationToken, + isValidSearchKey, + isValidSearchAffiliate +};