mirror of https://gitlab.com/curben/blog
feat(addJs): add a helper to embed <script src="">
This commit is contained in:
parent
0c05222589
commit
f90efdfe5f
|
@ -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)
|
Loading…
Reference in New Issue