1234567891011121314151617181920212223242526272829303132333435363738 |
- 'use strict';
- const { htmlTag, url_for } = require('hexo-util');
- 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 cssHelper(...args) {
- let result = '\n';
- flatten(args).forEach(item => {
- // Old syntax
- if (typeof item === 'string' || item instanceof String) {
- let path = item;
- if (!path.endsWith('.css')) {
- path += '.css';
- }
- result += `<link rel="stylesheet" href="${url_for.call(this, path)}">\n`;
- } else {
- // New syntax
- item.href = url_for.call(this, item.href);
- if (!item.href.endsWith('.css')) item.href += '.css';
- result += htmlTag('link', { rel: 'stylesheet', ...item }) + '\n';
- }
- });
- return result;
- }
- module.exports = cssHelper;
|