css.js 906 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const { htmlTag, url_for } = require('hexo-util');
  3. const flatten = function(arr, result = []) {
  4. for (const i in arr) {
  5. const value = arr[i];
  6. if (Array.isArray(value)) {
  7. flatten(value, result);
  8. } else {
  9. result.push(value);
  10. }
  11. }
  12. return result;
  13. };
  14. function cssHelper(...args) {
  15. let result = '\n';
  16. flatten(args).forEach(item => {
  17. // Old syntax
  18. if (typeof item === 'string' || item instanceof String) {
  19. let path = item;
  20. if (!path.endsWith('.css')) {
  21. path += '.css';
  22. }
  23. result += `<link rel="stylesheet" href="${url_for.call(this, path)}">\n`;
  24. } else {
  25. // New syntax
  26. item.href = url_for.call(this, item.href);
  27. if (!item.href.endsWith('.css')) item.href += '.css';
  28. result += htmlTag('link', { rel: 'stylesheet', ...item }) + '\n';
  29. }
  30. });
  31. return result;
  32. }
  33. module.exports = cssHelper;