Skip to content

Commit

Permalink
refactor: Do not use arguments object (hexojs#3102)
Browse files Browse the repository at this point in the history
* refactor: split function

* lodash.toArray to rest param

* remove arguments

* no use arguments

* fix param

* forTag.compile() does not use thisArg.
  • Loading branch information
segayuu authored and Thomas Parisot committed Jan 17, 2020
1 parent c83290d commit 2368e50
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 61 deletions.
4 changes: 1 addition & 3 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const chalk = require('chalk');
const tildify = require('tildify');
const Transform = require('stream').Transform;
const PassThrough = require('stream').PassThrough;
const _ = require('lodash');
const util = require('hexo-util');

const join = pathFn.join;
Expand Down Expand Up @@ -165,8 +164,7 @@ function generateConsole(args = {}) {
}

// Pipe a stream from one to another
function pipeStream() {
const args = _.toArray(arguments);
function pipeStream(...args) {
const src = args.shift();

return new Promise((resolve, reject) => {
Expand Down
13 changes: 5 additions & 8 deletions lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ const rLangCaption = /([^\s]+)\s*(.+)?/;
function backtickCodeBlock(data) {
const config = this.config.highlight || {};
if (!config.enable) return;
data.content = data.content.replace(rBacktick, function() {
const start = arguments[1];
const end = arguments[5];
const args = arguments[3].split('=').shift();
let content = arguments[4];
data.content = data.content.replace(rBacktick, ($0, start, $2, _args, content, end) => {
const args = _args.split('=').shift();

const options = {
hljs: config.hljs,
Expand All @@ -29,11 +26,11 @@ function backtickCodeBlock(data) {
if (config.first_line_number === 'inline') {

// setup line number by inline
arguments[3] = arguments[3].replace('=+', '=');
options.gutter = arguments[3].includes('=');
_args = _args.replace('=+', '=');
options.gutter = _args.includes('=');

// setup fiestLineNumber;
options.firstLine = options.gutter ? arguments[3].split('=')[1] || 1 : 0;
options.firstLine = options.gutter ? _args.split('=')[1] || 1 : 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/renderer/swig.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ swig.setDefaults({
});

// Hack: Override for tag of Swig
swig.setTag('for', forTag.parse, function(compiler, args, content, parents, options, blockName) {
const compile = forTag.compile.apply(this, arguments).split('\n');
swig.setTag('for', forTag.parse, (...args) => {
const compile = forTag.compile(...args).split('\n');

compile.splice(3, 0, ' if (!Array.isArray(__l) && typeof __l.toArray === "function") { __l = __l.toArray(); }');

Expand Down
86 changes: 45 additions & 41 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,43 @@ const rMark = /\s*mark:([0-9,-]+)/i;
* {% endcodeblock %}
*/

module.exports = ctx => function codeTag(args, content) {
let arg = args.join(' ');
const config = ctx.config.highlight || {};
let enable = config.enable;

if (rHighlight.test(arg)) {
arg = arg.replace(rHighlight, function() {
enable = arguments[1] === 'true';
return '';
});
}

if (!enable) {
content = escapeHTML(content);
return `<pre><code>${content}</code></pre>`;
}
function getHighlightOptions(config, arg) {

let caption = '';
let lang = '';
let line_number = config.line_number;
let first_line = 1;
let mark = [];
let match;

if (rLang.test(arg)) {
arg = arg.replace(rLang, function() {
lang = arguments[1];
arg = arg.replace(rLang, (match, _lang) => {
lang = _lang;
return '';
});
}

let line_number = config.line_number;
if (rLineNumber.test(arg)) {
arg = arg.replace(rLineNumber, function() {
line_number = arguments[1] === 'true';
arg = arg.replace(rLineNumber, (match, _line_number) => {
line_number = _line_number === 'true';
return '';
});
}

let first_line = 1;
if (rFirstLine.test(arg)) {
arg = arg.replace(rFirstLine, function() {
first_line = arguments[1];
arg = arg.replace(rFirstLine, (match, _first_line) => {
first_line = _first_line;
return '';
});
}

let mark = [];
if (rMark.test(arg)) {
arg = arg.replace(rMark, function() {
mark = arguments[1].split(',').reduce(function getMarkedLines(prev, cur) {
let a, b, temp;
arg = arg.replace(rMark, (match, _mark) => {
mark = _mark.split(',').reduce(function getMarkedLines(prev, cur) {
if (/-/.test(cur)) {
a = Number(cur.substr(0, cur.indexOf('-')));
b = Number(cur.substr(cur.indexOf('-') + 1));
let a = Number(cur.substr(0, cur.indexOf('-')));
let b = Number(cur.substr(cur.indexOf('-') + 1));
if (b < a) { // switch a & b
temp = a; a = b; b = temp;
const temp = a;
a = b;
b = temp;
}

for (; a <= b; a++) {
Expand All @@ -96,20 +79,19 @@ module.exports = ctx => function codeTag(args, content) {
});
}

let caption = '';
if (rCaptionUrlTitle.test(arg)) {
match = arg.match(rCaptionUrlTitle);
const match = arg.match(rCaptionUrlTitle);
caption = `<span>${match[1]}</span><a href="${match[2]}${match[3]}">${match[4]}</a>`;
} else if (rCaptionUrl.test(arg)) {
match = arg.match(rCaptionUrl);
const match = arg.match(rCaptionUrl);
caption = `<span>${match[1]}</span><a href="${match[2]}${match[3]}">link</a>`;
} else if (rCaption.test(arg)) {
match = arg.match(rCaption);
const match = arg.match(rCaption);
caption = `<span>${match[1]}</span>`;
}

content = stripIndent(content);

content = highlight(content, {
return {
lang,
firstLine: first_line,
caption,
Expand All @@ -118,7 +100,29 @@ module.exports = ctx => function codeTag(args, content) {
mark,
tab: config.tab_replace,
autoDetect: config.auto_detect
});
};
}

module.exports = ctx => function codeTag(args, content) {
let arg = args.join(' ');
const config = ctx.config.highlight || {};
let enable = config.enable;

if (rHighlight.test(arg)) {
arg = arg.replace(rHighlight, (match, _enable) => {
enable = _enable === 'true';
return '';
});
}

if (!enable) {
content = escapeHTML(content);
return `<pre><code>${content}</code></pre>`;
}

content = stripIndent(content);

content = highlight(content, getHighlightOptions(config, arg));

content = content.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');
Expand Down
13 changes: 6 additions & 7 deletions lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ module.exports = ctx => function includeCodeTag(args) {
const config = ctx.config.highlight || {};
let codeDir = ctx.config.code_dir;
let arg = args.join(' ');
let path = '';
let title = '';
let lang = '';
let caption = '';

// Add trailing slash to codeDir
if (codeDir[codeDir.length - 1] !== '/') codeDir += '/';

let lang = '';
if (rLang.test(arg)) {
arg = arg.replace(rLang, function() {
lang = arguments[1];
arg = arg.replace(rLang, (match, _lang) => {
lang = _lang;
return '';
});
}

let title = '';
let path = '';
if (rCaptionTitleFile.test(arg)) {
const match = arg.match(rCaptionTitleFile);
title = match[1];
Expand Down Expand Up @@ -63,7 +62,7 @@ module.exports = ctx => function includeCodeTag(args) {
// If the language is not defined, use file extension instead
lang = lang || pathFn.extname(path).substring(1);

caption = `<span>${title}</span><a href="${ctx.config.root}${codeDir}${path}">view raw</a>`;
const caption = `<span>${title}</span><a href="${ctx.config.root}${codeDir}${path}">view raw</a>`;

return highlight(code, {
lang,
Expand Down

0 comments on commit 2368e50

Please sign in to comment.