diff --git a/lib/helpers/blogophon-handlebars-quoters.js b/lib/helpers/blogophon-handlebars-quoters.js index ca6f6be..4733c4b 100644 --- a/lib/helpers/blogophon-handlebars-quoters.js +++ b/lib/helpers/blogophon-handlebars-quoters.js @@ -153,12 +153,19 @@ const blogophonHandlebarsQuoters = { * May be combined with `lazyloadImages` method. * Do not use this technique for AMP pages. * @see https://addyosmani.com/blog/lazy-loading/ - * @param {String} text [description] - * @param {String} loading Attribute value, defaults to 'lazy' - * @return {String} [description] + * @param {String} text [description] + * @param {String} loading Attribute value, defaults to 'lazy' + * @param {Boolean} butFirst Do not convert first hit, defaults to false + * @return {String} [description] */ - lazyloadAttributes: function(text, loading = 'lazy') { - return text.replace(/(<(?:img|iframe) )/g, '$1loading="' + loading + '" '); + lazyloadAttributes: function(text, loading = 'lazy', butFirst = false) { + return text.replace(/(<(?:img|iframe) )/g, function(all) { + if (butFirst) { + butFirst = false; + return all; + } + return all + 'loading="' + loading + '" '; + }); }, /** diff --git a/test/blogophon-handlebars-quoters.js b/test/blogophon-handlebars-quoters.js index e16f54e..1d49fba 100644 --- a/test/blogophon-handlebars-quoters.js +++ b/test/blogophon-handlebars-quoters.js @@ -43,6 +43,12 @@ describe('Blogophon Handlebars Quoters', function() { outputHtml = blogophonHandlebarsQuoter.lazyloadAttributes(inputHtml, 'eager'); assert.strictEqual(outputHtml.match(/ loading="eager"/g).length, 2); + + outputHtml = blogophonHandlebarsQuoter.lazyloadAttributes(inputHtml, 'lazy', true); + assert.strictEqual(outputHtml.match(/ loading="lazy"/g).length, 1); + + outputHtml = blogophonHandlebarsQuoter.lazyloadAttributes(inputHtml, 'lazy', 1); + assert.strictEqual(outputHtml.match(/ loading="lazy"/g).length, 1); }); it('must properly identify Gopher links', function() {