Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: native Array.flat() #4806

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions lib/plugins/helper/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@
const { htmlTag, url_for } = require('hexo-util');
const { default: moize } = require('moize');

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
args.flat(Infinity).forEach(item => {
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
// Custom attributes
item.href = url_for.call(this, item.href);
if (!item.href.endsWith('.css')) item.href += '.css';
result += htmlTag('link', { rel: 'stylesheet', ...item }) + '\n';
Expand Down
19 changes: 2 additions & 17 deletions lib/plugins/helper/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,18 @@
const { htmlTag, url_for } = require('hexo-util');
const { default: moize } = require('moize');

/* flatten() to be replaced by Array.flat()
after Node 10 has reached EOL */
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 jsHelper(...args) {
let result = '\n';

flatten(args).forEach(item => {
// Old syntax
args.flat(Infinity).forEach(item => {
if (typeof item === 'string' || item instanceof String) {
let path = item;
if (!path.endsWith('.js')) {
path += '.js';
}
result += `<script src="${url_for.call(this, path)}"></script>\n`;
} else {
// New syntax
// Custom attributes
item.src = url_for.call(this, item.src);
if (!item.src.endsWith('.js')) item.src += '.js';
result += htmlTag('script', { ...item }, '') + '\n';
Expand Down