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-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 @@