feat(addJs): add a helper to embed <script src="">

This commit is contained in:
curben 2019-08-26 08:14:21 +09:30
parent 0c05222589
commit f90efdfe5f
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
1 changed files with 35 additions and 0 deletions

35
scripts/addJs.js Normal file
View File

@ -0,0 +1,35 @@
'use strict'
/* global hexo */
/*
* Helper to add <script src=""> with support of custom attributes
* https://github.com/hexojs/hexo/pull/3681
*/
function jsHelper (...args) {
let result = '\n';
let items = args;
if (!Array.isArray(args)) {
items = [args];
}
items.forEach(item => {
// Old syntax
if (typeof item === 'string' || item instanceof String) {
result += `<script src="${item}"></script>\n`;
} else {
// New syntax
let tmpResult = '<script';
for (const attribute in item) {
if (item[attribute] === true) tmpResult += ' ' + attribute;
else tmpResult += ` ${attribute}="${item[attribute]}"`;
}
tmpResult += '></script>\n';
result += tmpResult;
}
});
return result;
}
hexo.extend.helper.register('addJs', jsHelper)