Skip to content

Commit 3c21965

Browse files
authored
metalsmith-html-*: only rewrite file contents if they changed (#168)
1 parent 47d7563 commit 3c21965

File tree

9 files changed

+40
-21
lines changed

9 files changed

+40
-21
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/metalsmith-html-glob/index.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ export default (options: Options = {}): Metalsmith.Plugin => {
2626
const debug = metalsmith.debug('metalsmith-github-profile');
2727
debug('running with options: %O', defaultedOptions);
2828

29+
const normalizedFilenames = Object.keys(files).map((resource) =>
30+
resource.replace(/[/\\]/g, '/'),
31+
);
32+
2933
// For each HTML file that matches the given pattern
3034
metalsmith.match(defaultedOptions.html, Object.keys(files)).forEach((filename) => {
3135
debug('processing file: %s', filename);
3236

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

40+
let fileChanged = false;
41+
3642
// For each given tag
3743
Object.keys(defaultedOptions.tags).forEach((tag) => {
3844
let attributes = defaultedOptions.tags[tag] ?? [];
@@ -70,9 +76,6 @@ export default (options: Options = {}): Metalsmith.Plugin => {
7076
}
7177

7278
// Find all input files matching the glob in the tag
73-
const normalizedFilenames = Object.keys(files).map((resource) =>
74-
resource.replace(/[/\\]/g, '/'),
75-
);
7679
const resources = metalsmith
7780
.match(relativeGlob, normalizedFilenames)
7881
.map((resource) => resource.replace(/[/\\]/g, '/'))
@@ -86,12 +89,15 @@ export default (options: Options = {}): Metalsmith.Plugin => {
8689
resource.insertBefore($(elem));
8790
});
8891
$(elem).remove();
92+
fileChanged = true;
8993
}
9094
});
9195
});
9296
});
9397

94-
file.contents = Buffer.from($.html());
98+
if (fileChanged) {
99+
file.contents = Buffer.from($.html());
100+
}
95101
});
96102

97103
done();

packages/metalsmith-html-glob/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metalsmith-html-glob",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "A Metalsmith plugin to apply glob patterns within HTML.",
55
"keywords": [
66
"metalsmith",
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
<!DOCTYPE html><html lang="en"><head></head><body>
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
24
[Brackets]
3-
4-
5-
</body></html>
5+
</body>
6+
</html>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<!DOCTYPE html><html lang="en"><head>
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
24
<link rel="stylesheet" href="**/*.css">
35
</head>
46
<body>
@@ -10,6 +12,5 @@
1012
<img src="**/*.@(bmp|gif|jpg|jpeg|png)">
1113
</a>
1214
<script src="**/*.js"></script>
13-
14-
15-
</body></html>
15+
</body>
16+
</html>

packages/metalsmith-html-relative/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export default (options: Options = {}): Metalsmith.Plugin => {
3636
const normalizedFilename = filename.replace(/[/\\]/g, path.sep);
3737
const $ = cheerio.load(file.contents);
3838

39+
let fileChanged = false;
40+
3941
// For each given tag
4042
Object.keys(defaultedOptions.tags).forEach((tag) => {
4143
let attributes = defaultedOptions.tags[tag] ?? [];
@@ -79,11 +81,14 @@ export default (options: Options = {}): Metalsmith.Plugin => {
7981

8082
debug(' "%s" changed to: %s', resource, relativeResource);
8183
$(elem).attr(attribute, relativeResource);
84+
fileChanged = true;
8285
});
8386
});
8487
});
8588

86-
file.contents = Buffer.from($.html());
89+
if (fileChanged) {
90+
file.contents = Buffer.from($.html());
91+
}
8792
});
8893

8994
done();

packages/metalsmith-html-relative/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metalsmith-html-relative",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"description": "A Metalsmith plugin to convert to relative paths within HTML.",
55
"keywords": [
66
"metalsmith",

packages/metalsmith-html-sri/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ export default (options: Options = {}): Metalsmith.Plugin => {
9393

9494
const file = files[filename];
9595
const normalizedFilename = filename.replace(/[/\\]/g, path.sep);
96+
const $ = cheerio.load(file.contents);
97+
98+
let fileChanged = false;
9699

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

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

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

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

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

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

193-
file.contents = Buffer.from($.html());
197+
if (fileChanged) {
198+
file.contents = Buffer.from($.html());
199+
}
194200
}),
195201
(err) => {
196202
if (err) {

packages/metalsmith-html-sri/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metalsmith-html-sri",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "A Metalsmith to add subresource integrity attributes to HTML.",
55
"keywords": [
66
"metalsmith",

0 commit comments

Comments
 (0)