Skip to content
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

metalsmith-html-*: only rewrite file contents if they changed #168

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions packages/metalsmith-html-glob/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ export default (options: Options = {}): Metalsmith.Plugin => {
const debug = metalsmith.debug('metalsmith-github-profile');
debug('running with options: %O', defaultedOptions);

const normalizedFilenames = Object.keys(files).map((resource) =>
resource.replace(/[/\\]/g, '/'),
);

// For each HTML file that matches the given pattern
metalsmith.match(defaultedOptions.html, Object.keys(files)).forEach((filename) => {
debug('processing file: %s', filename);

const file = files[filename];
const $ = cheerio.load(file.contents);

let fileChanged = false;

// For each given tag
Object.keys(defaultedOptions.tags).forEach((tag) => {
let attributes = defaultedOptions.tags[tag] ?? [];
Expand Down Expand Up @@ -70,9 +76,6 @@ export default (options: Options = {}): Metalsmith.Plugin => {
}

// Find all input files matching the glob in the tag
const normalizedFilenames = Object.keys(files).map((resource) =>
resource.replace(/[/\\]/g, '/'),
);
const resources = metalsmith
.match(relativeGlob, normalizedFilenames)
.map((resource) => resource.replace(/[/\\]/g, '/'))
Expand All @@ -86,12 +89,15 @@ export default (options: Options = {}): Metalsmith.Plugin => {
resource.insertBefore($(elem));
});
$(elem).remove();
fileChanged = true;
}
});
});
});

file.contents = Buffer.from($.html());
if (fileChanged) {
file.contents = Buffer.from($.html());
}
});

done();
Expand Down
2 changes: 1 addition & 1 deletion packages/metalsmith-html-glob/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metalsmith-html-glob",
"version": "2.0.3",
"version": "2.0.4",
"description": "A Metalsmith plugin to apply glob patterns within HTML.",
"keywords": [
"metalsmith",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE html><html lang="en"><head></head><body>
<!DOCTYPE html>
<html lang="en">
<body>
[Brackets]


</body></html>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<!DOCTYPE html><html lang="en"><head>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="**/*.css">
</head>
<body>
Expand All @@ -10,6 +12,5 @@
<img src="**/*.@(bmp|gif|jpg|jpeg|png)">
</a>
<script src="**/*.js"></script>


</body></html>
</body>
</html>
7 changes: 6 additions & 1 deletion packages/metalsmith-html-relative/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default (options: Options = {}): Metalsmith.Plugin => {
const normalizedFilename = filename.replace(/[/\\]/g, path.sep);
const $ = cheerio.load(file.contents);

let fileChanged = false;

// For each given tag
Object.keys(defaultedOptions.tags).forEach((tag) => {
let attributes = defaultedOptions.tags[tag] ?? [];
Expand Down Expand Up @@ -79,11 +81,14 @@ export default (options: Options = {}): Metalsmith.Plugin => {

debug(' "%s" changed to: %s', resource, relativeResource);
$(elem).attr(attribute, relativeResource);
fileChanged = true;
});
});
});

file.contents = Buffer.from($.html());
if (fileChanged) {
file.contents = Buffer.from($.html());
}
});

done();
Expand Down
2 changes: 1 addition & 1 deletion packages/metalsmith-html-relative/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metalsmith-html-relative",
"version": "2.0.5",
"version": "2.0.6",
"description": "A Metalsmith plugin to convert to relative paths within HTML.",
"keywords": [
"metalsmith",
Expand Down
10 changes: 8 additions & 2 deletions packages/metalsmith-html-sri/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ export default (options: Options = {}): Metalsmith.Plugin => {

const file = files[filename];
const normalizedFilename = filename.replace(/[/\\]/g, path.sep);
const $ = cheerio.load(file.contents);

let fileChanged = false;

// For each given tag
const $ = cheerio.load(file.contents);
for (const tag of Object.keys(defaultedOptions.tags)) {
const { attribute } = defaultedOptions.tags[tag];

Expand Down Expand Up @@ -146,6 +148,7 @@ export default (options: Options = {}): Metalsmith.Plugin => {

debug(' %s: %s', resource, files[resource].integrity);
$(elem).attr('integrity', files[resource].integrity as string);
fileChanged = true;
} else {
// Add integrity attribute to remote resources

Expand Down Expand Up @@ -175,6 +178,7 @@ export default (options: Options = {}): Metalsmith.Plugin => {

debug(' %s: %s', uri, remoteResources[uri]);
$(elem).attr('integrity', remoteResources[uri]);
fileChanged = true;

// Enforce crossorigin attribute for non-local resources with integrity attribute
// https://www.w3.org/TR/2016/REC-SRI-20160623/#cross-origin-data-leakage
Expand All @@ -190,7 +194,9 @@ export default (options: Options = {}): Metalsmith.Plugin => {
await elemPromise;
}

file.contents = Buffer.from($.html());
if (fileChanged) {
file.contents = Buffer.from($.html());
}
}),
(err) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/metalsmith-html-sri/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metalsmith-html-sri",
"version": "2.1.0",
"version": "2.1.1",
"description": "A Metalsmith to add subresource integrity attributes to HTML.",
"keywords": [
"metalsmith",
Expand Down
Loading