From 2368e502bf9eacb3feec5e23a42c60d6b6de3cc4 Mon Sep 17 00:00:00 2001 From: segayuu Date: Tue, 10 Apr 2018 14:29:12 +0900 Subject: [PATCH] refactor: Do not use arguments object (#3102) * refactor: split function * lodash.toArray to rest param * remove arguments * no use arguments * fix param * forTag.compile() does not use thisArg. --- lib/plugins/console/generate.js | 4 +- .../before_post_render/backtick_code_block.js | 13 ++- lib/plugins/renderer/swig.js | 4 +- lib/plugins/tag/code.js | 86 ++++++++++--------- lib/plugins/tag/include_code.js | 13 ++- 5 files changed, 59 insertions(+), 61 deletions(-) diff --git a/lib/plugins/console/generate.js b/lib/plugins/console/generate.js index 599b42c51f..0407567409 100644 --- a/lib/plugins/console/generate.js +++ b/lib/plugins/console/generate.js @@ -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; @@ -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) => { diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/plugins/filter/before_post_render/backtick_code_block.js index 01bd27ebd3..3dd879181f 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -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, @@ -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; } } diff --git a/lib/plugins/renderer/swig.js b/lib/plugins/renderer/swig.js index cf7b6d7261..d272df7a0c 100644 --- a/lib/plugins/renderer/swig.js +++ b/lib/plugins/renderer/swig.js @@ -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(); }'); diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index 36972b2c89..a07d23cbeb 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -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 `
${content}
`; - } +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++) { @@ -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 = `${match[1]}${match[4]}`; } else if (rCaptionUrl.test(arg)) { - match = arg.match(rCaptionUrl); + const match = arg.match(rCaptionUrl); caption = `${match[1]}link`; } else if (rCaption.test(arg)) { - match = arg.match(rCaption); + const match = arg.match(rCaption); caption = `${match[1]}`; } - content = stripIndent(content); - - content = highlight(content, { + return { lang, firstLine: first_line, caption, @@ -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 `
${content}
`; + } + + content = stripIndent(content); + + content = highlight(content, getHighlightOptions(config, arg)); content = content.replace(/{/g, '{') .replace(/}/g, '}'); diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js index 6ea6163db6..8c321bf50c 100644 --- a/lib/plugins/tag/include_code.js +++ b/lib/plugins/tag/include_code.js @@ -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]; @@ -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 = `${title}view raw`; + const caption = `${title}view raw`; return highlight(code, { lang,