diff --git a/scripts/events/index.js b/scripts/events/index.js index 9ca8fb6..8a49106 100644 --- a/scripts/events/index.js +++ b/scripts/events/index.js @@ -5,6 +5,7 @@ hexo.on('generateBefore', () => { // Merge config. require('./lib/config')(hexo); + require('./lib/utils')(hexo); }); hexo.on('ready', () => { diff --git a/scripts/events/lib/config.js b/scripts/events/lib/config.js index ee52362..0de8f90 100644 --- a/scripts/events/lib/config.js +++ b/scripts/events/lib/config.js @@ -6,7 +6,7 @@ 'use strict'; module.exports = hexo => { - + const { cache, language_switcher } = hexo.theme.config; const warning = function(...args) { hexo.log.warn(`Since ${args[0]} is turned on, the ${args[1]} is disabled to avoid potential hazards.`); diff --git a/scripts/events/lib/utils.js b/scripts/events/lib/utils.js new file mode 100644 index 0000000..6714d04 --- /dev/null +++ b/scripts/events/lib/utils.js @@ -0,0 +1,73 @@ +/** + * utils.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/ + */ + +'use strict'; + +module.exports = hexo => { + hexo.args = { + map: (args, keys, others) => { + if (Array.isArray(args) == false) { + return args; + } + var map = {others: Array()}; + args.forEach((arg, i) => { + let kv = arg.trim(); + if (kv.includes('://') && kv.split(':').length == 2) { + // 纯 url + map.others.push(kv); + } else { + kv = kv.split(':'); + if (kv.length > 1) { + if (keys.includes(kv[0]) == true) { + map[kv.shift()] = kv.join(':'); + } else { + map.others.push(kv.join(':')); + } + } else if (kv.length == 1) { + map.others.push(kv[0]); + } + } + }); + // 解析不带 key 的参数 + if (others && others.length > 0 && map.others.length > 0) { + if (Array.isArray(others) == false) { + others = [others]; + } + others.forEach((arg, i) => { + map[arg] = map.others.shift(); + }); + // 最后一段合并到最后一个参数中 + if (map.others.length > 0) { + map[others[others.length-1]] += ' ' + map.others.join(' '); + map.others = []; + } + } + return map; + }, + joinTags: (args, keys) => { + if (Array.isArray(keys) == false) { + keys = [keys]; + } + var ret = []; + keys.forEach((key, i) => { + if (args[key] && args[key].length > 0) { + ret.push(key + '="' + args[key] + '"'); + } + }); + return ret; + }, + joinURLParams: (args, keys) => { + if (Array.isArray(keys) == false) { + keys = [keys]; + } + var ret = []; + keys.forEach((key, i) => { + if (args[key] && args[key].length > 0) { + ret.push(key + '=' + args[key]); + } + }); + return ret.join('&'); + } + }; +}; diff --git a/scripts/tags/about.js b/scripts/tags/about.js index bb63cd9..497b598 100644 --- a/scripts/tags/about.js +++ b/scripts/tags/about.js @@ -1,7 +1,7 @@ /** * about.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/ * 格式与官方标签插件一致使用空格分隔,中括号内的是可选参数(中括号不需要写出来) - * + * * {% about [avatar:xxx] [height:80px] %} * title / body * {% endabout %} @@ -9,10 +9,8 @@ 'use strict'; -const { ArgsMap } = require('./utils'); - hexo.extend.tag.register('about', function(args, content) { - args = ArgsMap(args, ['avatar', 'height']); + args = hexo.args.map(args, ['avatar', 'height']); var rows = hexo.render.renderSync({text: content, engine: 'markdown'}).split('\n'); var el = ''; // wrapper @@ -33,14 +31,14 @@ hexo.extend.tag.register('about', function(args, content) { el += rows.shift(); // el += ''; } - + el += ''; // content el += '