From 65056a38135380ae7bd0bdd002f250730d358204 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 13 Dec 2019 00:00:16 +0000 Subject: [PATCH 1/3] refactor(render): Class syntax --- lib/hexo/render.js | 162 +++++++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 80 deletions(-) diff --git a/lib/hexo/render.js b/lib/hexo/render.js index 40029d46e0..e8991986e4 100644 --- a/lib/hexo/render.js +++ b/lib/hexo/render.js @@ -11,105 +11,107 @@ function getExtname(str) { return ext[0] === '.' ? ext.slice(1) : ext; } -function Render(ctx) { - this.context = ctx; - this.renderer = ctx.extend.renderer; -} - -Render.prototype.isRenderable = function(path) { - return this.renderer.isRenderable(path); -}; - -Render.prototype.isRenderableSync = function(path) { - return this.renderer.isRenderableSync(path); -}; - -Render.prototype.getOutput = function(path) { - return this.renderer.getOutput(path); -}; - -Render.prototype.getRenderer = function(ext, sync) { - return this.renderer.get(ext, sync); -}; +class Render { + constructor(ctx) { + this.context = ctx; + this.renderer = ctx.extend.renderer; + } -Render.prototype.getRendererSync = function(ext) { - return this.getRenderer(ext, true); -}; + isRenderable(path) { + return this.renderer.isRenderable(path); + } -Render.prototype.render = function(data, options, callback) { - if (!callback && typeof options === 'function') { - callback = options; - options = {}; + isRenderableSync(path) { + return this.renderer.isRenderableSync(path); } - const ctx = this.context; - let ext = ''; + getOutput(path) { + return this.renderer.getOutput(path); + } - return new Promise((resolve, reject) => { - if (!data) return reject(new TypeError('No input file or string!')); - if (data.text != null) return resolve(data.text); - if (!data.path) return reject(new TypeError('No input file or string!')); + getRenderer(ext, sync) { + return this.renderer.get(ext, sync); + } - fs.readFile(data.path).then(resolve, reject); - }).then(text => { - data.text = text; - ext = data.engine || getExtname(data.path); - if (!ext || !this.isRenderable(ext)) return text; + getRendererSync(ext) { + return this.getRenderer(ext, true); + } - const renderer = this.getRenderer(ext); - return Reflect.apply(renderer, ctx, [data, options]); - }).then(result => { - result = toString(result, data); - if (data.onRenderEnd) { - return data.onRenderEnd(result); + render(data, options, callback) { + if (!callback && typeof options === 'function') { + callback = options; + options = {}; } - return result; - }).then(result => { - const output = this.getOutput(ext) || ext; - return ctx.execFilter(`after_render:${output}`, result, { - context: ctx, - args: [data] - }); - }).asCallback(callback); -}; + const ctx = this.context; + let ext = ''; + + return new Promise((resolve, reject) => { + if (!data) return reject(new TypeError('No input file or string!')); + if (data.text != null) return resolve(data.text); + if (!data.path) return reject(new TypeError('No input file or string!')); + + fs.readFile(data.path).then(resolve, reject); + }).then(text => { + data.text = text; + ext = data.engine || getExtname(data.path); + if (!ext || !this.isRenderable(ext)) return text; + + const renderer = this.getRenderer(ext); + return Reflect.apply(renderer, ctx, [data, options]); + }).then(result => { + result = toString(result, data); + if (data.onRenderEnd) { + return data.onRenderEnd(result); + } + + return result; + }).then(result => { + const output = this.getOutput(ext) || ext; + return ctx.execFilter(`after_render:${output}`, result, { + context: ctx, + args: [data] + }); + }).asCallback(callback); + } -Render.prototype.renderSync = function(data, options) { - if (!data) throw new TypeError('No input file or string!'); + renderSync(data, options) { + if (!data) throw new TypeError('No input file or string!'); - options = options || {}; + options = options || {}; - const ctx = this.context; + const ctx = this.context; - if (data.text == null) { - if (!data.path) throw new TypeError('No input file or string!'); - data.text = fs.readFileSync(data.path); - } + if (data.text == null) { + if (!data.path) throw new TypeError('No input file or string!'); + data.text = fs.readFileSync(data.path); + } - if (data.text == null) throw new TypeError('No input file or string!'); + if (data.text == null) throw new TypeError('No input file or string!'); - const ext = data.engine || getExtname(data.path); - let result; + const ext = data.engine || getExtname(data.path); + let result; - if (ext && this.isRenderableSync(ext)) { - const renderer = this.getRendererSync(ext); - result = Reflect.apply(renderer, ctx, [data, options]); - } else { - result = data.text; - } + if (ext && this.isRenderableSync(ext)) { + const renderer = this.getRendererSync(ext); + result = Reflect.apply(renderer, ctx, [data, options]); + } else { + result = data.text; + } - const output = this.getOutput(ext) || ext; - result = toString(result, data); + const output = this.getOutput(ext) || ext; + result = toString(result, data); - if (data.onRenderEnd) { - result = data.onRenderEnd(result); - } + if (data.onRenderEnd) { + result = data.onRenderEnd(result); + } - return ctx.execFilterSync(`after_render:${output}`, result, { - context: ctx, - args: [data] - }); -}; + return ctx.execFilterSync(`after_render:${output}`, result, { + context: ctx, + args: [data] + }); + } +} function toString(result, options) { if (!Object.prototype.hasOwnProperty.call(options, 'toString') || typeof result === 'string') return result; From bc15c323ab6c849a49bfedb747216f5efbb95da0 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 13 Dec 2019 00:02:41 +0000 Subject: [PATCH 2/3] refactor(render): arrow function --- lib/hexo/render.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/hexo/render.js b/lib/hexo/render.js index e8991986e4..294806eba8 100644 --- a/lib/hexo/render.js +++ b/lib/hexo/render.js @@ -4,12 +4,26 @@ const { extname } = require('path'); const Promise = require('bluebird'); const fs = require('hexo-fs'); -function getExtname(str) { +const getExtname = str => { if (typeof str !== 'string') return ''; const ext = extname(str); return ext[0] === '.' ? ext.slice(1) : ext; -} +}; + +const toString = (result, options) => { + if (!Object.prototype.hasOwnProperty.call(options, 'toString') || typeof result === 'string') return result; + + if (typeof options.toString === 'function') { + return options.toString(result); + } else if (typeof result === 'object') { + return JSON.stringify(result); + } else if (result.toString) { + return result.toString(); + } + + return result; +}; class Render { constructor(ctx) { @@ -113,18 +127,4 @@ class Render { } } -function toString(result, options) { - if (!Object.prototype.hasOwnProperty.call(options, 'toString') || typeof result === 'string') return result; - - if (typeof options.toString === 'function') { - return options.toString(result); - } else if (typeof result === 'object') { - return JSON.stringify(result); - } else if (result.toString) { - return result.toString(); - } - - return result; -} - module.exports = Render; From 06eeb69dd0b5c998a4915528c3b5e07c7580e2d5 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 13 Dec 2019 00:04:26 +0000 Subject: [PATCH 3/3] refactor(render): destructure hexo-fs --- lib/hexo/render.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hexo/render.js b/lib/hexo/render.js index 294806eba8..788f4dc153 100644 --- a/lib/hexo/render.js +++ b/lib/hexo/render.js @@ -2,7 +2,7 @@ const { extname } = require('path'); const Promise = require('bluebird'); -const fs = require('hexo-fs'); +const { readFile, readFileSync } = require('hexo-fs'); const getExtname = str => { if (typeof str !== 'string') return ''; @@ -65,7 +65,7 @@ class Render { if (data.text != null) return resolve(data.text); if (!data.path) return reject(new TypeError('No input file or string!')); - fs.readFile(data.path).then(resolve, reject); + readFile(data.path).then(resolve, reject); }).then(text => { data.text = text; ext = data.engine || getExtname(data.path); @@ -98,7 +98,7 @@ class Render { if (data.text == null) { if (!data.path) throw new TypeError('No input file or string!'); - data.text = fs.readFileSync(data.path); + data.text = readFileSync(data.path); } if (data.text == null) throw new TypeError('No input file or string!');