Skip to content

Commit

Permalink
Adds meta name="generator" check to upgrade plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Aug 1, 2024
1 parent 85a6074 commit 579fde4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = function(eleventyConfig) {
console.log(chalk.blue(`[11ty/eleventy-upgrade-help]`), `If you are migrating from 0.x or 1.x, please use a previous version of this plugin.`);
console.log(chalk.blue(`[11ty/eleventy-upgrade-help] ---`));

eleventyConfig.addPlugin(require("./src/meta-generator.js"));

// Full list of issues: https://github.com/11ty/eleventy/issues?q=milestone%3A%22Eleventy+3.0.0%22+is%3Aclosed+label%3Abreaking-change
eleventyConfig.addPlugin(require("./src/node-version.js"));
eleventyConfig.addPlugin(require("./src/explicit-config-file.js"));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"fast-glob": "^3.3.2",
"kleur": "^4.1.5",
"minimist": "^1.2.8",
"posthtml-match-helper": "^2.0.2",
"semver": "^7.6.3"
},
"11ty": {
Expand Down
44 changes: 44 additions & 0 deletions src/meta-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const chalk = require("kleur");

module.exports = async function(eleventyConfig) {
const { default: matchHelper } = await import("posthtml-match-helper");

let htmlTemplatesMissingMetaGenerator = new Set();
let found = 0;

eleventyConfig.on("eleventy.before", () => {
htmlTemplatesMissingMetaGenerator = new Set();
found = 0;
});

eleventyConfig.on("eleventy.after", () => {
if(htmlTemplatesMissingMetaGenerator.size > 0) {
console.log(chalk.blue(`[11ty/eleventy-upgrade-help] NOTICE`), `Your project has .html output files (×${htmlTemplatesMissingMetaGenerator.size}) that don’t have a populated <meta name="generator" content> tag. It would be helpful to Eleventy if you added it (but isn’t required). Applicable input files: ${Array.from(htmlTemplatesMissingMetaGenerator).join(", ")} Read more: https://www.11ty.dev/docs/data-eleventy-supplied/#use-with-meta-namegenerator`);
} else {
console.log(chalk.green(`[11ty/eleventy-upgrade-help] PASSED`), `All of your project’s .html output files (×${found}) are using <meta name="generator" content="Eleventy …">`);
}
});

eleventyConfig.htmlTransformer.addPosthtmlPlugin(
"html",
function (options) {
return function (tree) {
let missing = 1;

tree.match(matchHelper(`meta[name="generator"]`), function (node) {
if (node.attrs?.content && (node.attrs?.content || "").toLowerCase().startsWith("eleventy")) {
missing--;
}

return node;
});

if(missing > 0) {
htmlTemplatesMissingMetaGenerator.add(options.page.inputPath);
} else {
found++;
}
};
}
);
};

0 comments on commit 579fde4

Please sign in to comment.