From 870ca8c22051ab90cad964dfb7452d8eeb3dc97d Mon Sep 17 00:00:00 2001 From: Christian Emmer <10749361+emmercm@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:06:34 -0800 Subject: [PATCH 1/2] metalsmith-html-*: only rewrite file contents if they changed --- package-lock.json | 6 +++--- packages/metalsmith-html-glob/index.ts | 14 ++++++++++---- packages/metalsmith-html-glob/package.json | 2 +- packages/metalsmith-html-relative/index.ts | 7 ++++++- packages/metalsmith-html-relative/package.json | 2 +- packages/metalsmith-html-sri/index.ts | 10 ++++++++-- packages/metalsmith-html-sri/package.json | 2 +- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4ae13423..ff2eaf75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22002,7 +22002,7 @@ } }, "packages/metalsmith-html-glob": { - "version": "2.0.3", + "version": "2.0.4", "license": "GPL-3.0-or-later", "dependencies": { "cheerio": "^1.0.0", @@ -22073,7 +22073,7 @@ } }, "packages/metalsmith-html-relative": { - "version": "2.0.5", + "version": "2.0.6", "license": "GPL-3.0-or-later", "dependencies": { "cheerio": "^1.0.0", @@ -22130,7 +22130,7 @@ } }, "packages/metalsmith-html-sri": { - "version": "2.1.0", + "version": "2.1.1", "license": "GPL-3.0-or-later", "dependencies": { "async": "^3.2.6", diff --git a/packages/metalsmith-html-glob/index.ts b/packages/metalsmith-html-glob/index.ts index 91662f81..cecf1f3e 100644 --- a/packages/metalsmith-html-glob/index.ts +++ b/packages/metalsmith-html-glob/index.ts @@ -26,6 +26,10 @@ 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); @@ -33,6 +37,8 @@ export default (options: Options = {}): Metalsmith.Plugin => { 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] ?? []; @@ -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, '/')) @@ -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(); diff --git a/packages/metalsmith-html-glob/package.json b/packages/metalsmith-html-glob/package.json index 1bb725f7..58673dbf 100644 --- a/packages/metalsmith-html-glob/package.json +++ b/packages/metalsmith-html-glob/package.json @@ -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", diff --git a/packages/metalsmith-html-relative/index.ts b/packages/metalsmith-html-relative/index.ts index 883c6f23..8835ab78 100644 --- a/packages/metalsmith-html-relative/index.ts +++ b/packages/metalsmith-html-relative/index.ts @@ -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] ?? []; @@ -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(); diff --git a/packages/metalsmith-html-relative/package.json b/packages/metalsmith-html-relative/package.json index 26b4ae5f..a30fe80c 100644 --- a/packages/metalsmith-html-relative/package.json +++ b/packages/metalsmith-html-relative/package.json @@ -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", diff --git a/packages/metalsmith-html-sri/index.ts b/packages/metalsmith-html-sri/index.ts index 1bb3465c..0717be74 100644 --- a/packages/metalsmith-html-sri/index.ts +++ b/packages/metalsmith-html-sri/index.ts @@ -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]; @@ -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 @@ -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 @@ -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) { diff --git a/packages/metalsmith-html-sri/package.json b/packages/metalsmith-html-sri/package.json index 51b4c924..6c529cc6 100644 --- a/packages/metalsmith-html-sri/package.json +++ b/packages/metalsmith-html-sri/package.json @@ -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", From 5c2ce9b94e9968aa58c45027447f58044e7aac1b Mon Sep 17 00:00:00 2001 From: Christian Emmer <10749361+emmercm@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:12:24 -0800 Subject: [PATCH 2/2] Fix tests --- .../test/fixtures/advanced/expected/[brackets].html | 9 +++++---- .../test/fixtures/basic/expected/index.html | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/metalsmith-html-glob/test/fixtures/advanced/expected/[brackets].html b/packages/metalsmith-html-glob/test/fixtures/advanced/expected/[brackets].html index c8285c66..14641b4a 100644 --- a/packages/metalsmith-html-glob/test/fixtures/advanced/expected/[brackets].html +++ b/packages/metalsmith-html-glob/test/fixtures/advanced/expected/[brackets].html @@ -1,5 +1,6 @@ -
+ + + [Brackets] - - - \ No newline at end of file + + diff --git a/packages/metalsmith-html-glob/test/fixtures/basic/expected/index.html b/packages/metalsmith-html-glob/test/fixtures/basic/expected/index.html index ebbb0f0e..49039413 100644 --- a/packages/metalsmith-html-glob/test/fixtures/basic/expected/index.html +++ b/packages/metalsmith-html-glob/test/fixtures/basic/expected/index.html @@ -1,4 +1,6 @@ - + + + @@ -10,6 +12,5 @@