From 15327f423b4697fe1fcd448269f244b48f1d9227 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 26 Aug 2019 06:26:33 +0930 Subject: [PATCH 1/5] feat(js_helper): add support for custom attribute --- lib/plugins/helper/js.js | 51 ++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/plugins/helper/js.js b/lib/plugins/helper/js.js index 1950fcd9de..4be3f734e9 100644 --- a/lib/plugins/helper/js.js +++ b/lib/plugins/helper/js.js @@ -1,15 +1,52 @@ 'use strict'; +const flatten = function(arr, result = []) { + for (const i in arr) { + const value = arr[i]; + if (Array.isArray(value)) { + flatten(value, result); + } else { + result.push(value); + } + } + return result; +}; + function jsHelper(...args) { - return args.reduce((result, path, i) => { - if (i) result += '\n'; + let result = '\n'; + let items = args; + + if (!Array.isArray(args)) { + items = [args]; + } + + items = flatten(items); + + items.forEach(item => { + // Old syntax + if (typeof item === 'string' || item instanceof String) { + let path = item; + if (!path.endsWith('.js')) { + path += '.js'; + } + result += `\n`; + } else { + // New syntax + let tmpResult = '`; - }, ''); + }); + return result; } module.exports = jsHelper; From 22e380887f3a8e9f72435c70bd13fc7823255f88 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 26 Aug 2019 06:26:59 +0930 Subject: [PATCH 2/5] test(js_helper): test custom attributes --- test/scripts/helpers/js.js | 55 +++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/test/scripts/helpers/js.js b/test/scripts/helpers/js.js index 693ec2c851..5fb5586bdf 100644 --- a/test/scripts/helpers/js.js +++ b/test/scripts/helpers/js.js @@ -1,5 +1,7 @@ 'use strict'; +const cheerio = require('cheerio'); + describe('js', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(__dirname); @@ -12,14 +14,26 @@ describe('js', () => { const js = require('../../../lib/plugins/helper/js').bind(ctx); - function assertResult(result) { - let expected = ''; + function assertResult(result, expected) { + const $ = cheerio.load(result); - for (let i = 1, len = arguments.length; i < len; i++) { - expected += '\n'; + if (!Array.isArray(expected)) { + expected = [expected]; } - result.should.eql(expected.trim()); + expected.forEach((item, index) => { + if (typeof item === 'string' || item instanceof String) { + $('script').eq(index).attr('src').should.eql(item); + } else { + for (const attribute in item) { + if (item[attribute] === true) { + $('script').eq(index).attr(attribute).should.eql(attribute); + } else { + $('script').eq(index).attr(attribute).should.eql(item[attribute]); + } + } + } + }); } it('a string', () => { @@ -30,18 +44,41 @@ describe('js', () => { }); it('an array', () => { - assertResult(js(['foo', 'bar', 'baz']), '/foo.js', '/bar.js', '/baz.js'); + assertResult(js(['foo', 'bar', 'baz']), ['/foo.js', '/bar.js', '/baz.js']); }); it('multiple strings', () => { - assertResult(js('foo', 'bar', 'baz'), '/foo.js', '/bar.js', '/baz.js'); + assertResult(js('foo', 'bar', 'baz'), ['/foo.js', '/bar.js', '/baz.js']); }); it('multiple arrays', () => { - assertResult(js(['foo', 'bar'], ['baz']), '/foo.js', '/bar.js', '/baz.js'); + assertResult(js(['foo', 'bar'], ['baz']), ['/foo.js', '/bar.js', '/baz.js']); }); it('mixed', () => { - assertResult(js(['foo', 'bar'], 'baz'), '/foo.js', '/bar.js', '/baz.js'); + assertResult(js(['foo', 'bar'], 'baz'), ['/foo.js', '/bar.js', '/baz.js']); + }); + + it('an object', () => { + assertResult(js({src: 'script.js'}), {src: '/script.js'}); + assertResult(js({src: '/script.js'}), {src: '/script.js'}); + assertResult(js({src: '/script.js', foo: 'bar'}), {src: '/script.js', foo: 'bar'}); + }); + + it('mulitple objects', () => { + assertResult(js({src: '/foo.js'}, {src: '/bar.js'}), [{src: '/foo.js'}, {src: '/bar.js'}]); + assertResult(js({src: '/aaa.js', bbb: 'ccc'}, {src: '/ddd.js', eee: 'fff'}), + [{src: '/aaa.js', bbb: 'ccc'}, {src: '/ddd.js', eee: 'fff'}]); + }); + + it('an array of objects', () => { + assertResult(js([{src: '/foo.js'}, {src: '/bar.js'}]), [{src: '/foo.js'}, {src: '/bar.js'}]); + assertResult(js([{src: '/aaa.js', bbb: 'ccc'}, {src: '/ddd.js', eee: 'fff'}]), + [{src: '/aaa.js', bbb: 'ccc'}, {src: '/ddd.js', eee: 'fff'}]); + }); + + it('async and defer attributes', () => { + assertResult(js({src: '/foo.js', 'async': true}), {src: '/foo.js', 'async': true}); + assertResult(js({src: '/bar.js', 'defer': true}), {src: '/bar.js', 'defer': true}); }); }); From 5fff816a64e6c46ad00e71ece78f6a2f3c3b1ea3 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 30 Aug 2019 07:36:28 +0100 Subject: [PATCH 3/5] refactor(js_helper): forEach() --- lib/plugins/helper/js.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/helper/js.js b/lib/plugins/helper/js.js index 4be3f734e9..2b67b9260f 100644 --- a/lib/plugins/helper/js.js +++ b/lib/plugins/helper/js.js @@ -33,7 +33,7 @@ function jsHelper(...args) { } else { // New syntax let tmpResult = ' { if (attribute === 'src') { item[attribute] = this.url_for(item[attribute]); if (!item[attribute].endsWith('.js')) item[attribute] += '.js'; @@ -41,7 +41,7 @@ function jsHelper(...args) { if (item[attribute] === true) tmpResult += ' ' + attribute; else tmpResult += ` ${attribute}="${item[attribute]}"`; - } + }); tmpResult += '>\n'; result += tmpResult; } From 5018dd70fe2a565eea0f708d23604d3cdd41fed2 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Thu, 17 Oct 2019 07:27:01 +0100 Subject: [PATCH 4/5] refactor(js_helper): utilize htmlTag --- lib/plugins/helper/js.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/plugins/helper/js.js b/lib/plugins/helper/js.js index 2b67b9260f..d02d5d149c 100644 --- a/lib/plugins/helper/js.js +++ b/lib/plugins/helper/js.js @@ -1,5 +1,10 @@ 'use strict'; +const { htmlTag } = require('hexo-util'); +const url_for = require('./url_for'); + +/* flatten() to be replaced by Array.flat() +after Node 10 has reached EOL */ const flatten = function(arr, result = []) { for (const i in arr) { const value = arr[i]; @@ -32,18 +37,9 @@ function jsHelper(...args) { result += `\n`; } else { // New syntax - let tmpResult = ' { - if (attribute === 'src') { - item[attribute] = this.url_for(item[attribute]); - if (!item[attribute].endsWith('.js')) item[attribute] += '.js'; - } - - if (item[attribute] === true) tmpResult += ' ' + attribute; - else tmpResult += ` ${attribute}="${item[attribute]}"`; - }); - tmpResult += '>\n'; - result += tmpResult; + item.src = url_for.call(this, item.src); + if (!item.src.endsWith('.css')) item.src += '.css'; + result += htmlTag('script', { ...item }, '') + '\n'; } }); return result; From 16cdafd52645c05b45c46f93c7b91c9cbf1fbdc6 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Thu, 17 Oct 2019 07:31:38 +0100 Subject: [PATCH 5/5] fix(js_helper): .js file extension --- lib/plugins/helper/js.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/helper/js.js b/lib/plugins/helper/js.js index d02d5d149c..c9750a65aa 100644 --- a/lib/plugins/helper/js.js +++ b/lib/plugins/helper/js.js @@ -38,7 +38,7 @@ function jsHelper(...args) { } else { // New syntax item.src = url_for.call(this, item.src); - if (!item.src.endsWith('.css')) item.src += '.css'; + if (!item.src.endsWith('.js')) item.src += '.js'; result += htmlTag('script', { ...item }, '') + '\n'; } });