From d524e1036a13446041cbd94b4a1925ce89426be2 Mon Sep 17 00:00:00 2001 From: JiangTJ <116749895@qq.com> Date: Tue, 4 Feb 2020 18:00:07 +0800 Subject: [PATCH 1/3] Add filter --- .gitignore | 1 + lib/renderer.js | 1 + package.json | 5 +++-- test/index.js | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 19e149c..133fb96 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ tmp/ *.log .idea/ .nyc_output/ +yarn.lock diff --git a/lib/renderer.js b/lib/renderer.js index f6a9b93..f76fc0c 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -51,6 +51,7 @@ module.exports = function(data, options, callback) { stylusConfig .use(defineConfig) + .use(style => this.execFilterSync('stylus', style, {context: this})) .set('filename', data.path) .set('sourcemap', config.sourcemaps) .set('compress', config.compress) diff --git a/package.json b/package.json index 861eade..5b8085b 100644 --- a/package.json +++ b/package.json @@ -28,14 +28,15 @@ "author": "Tommy Chen (http://zespia.tw)", "license": "MIT", "dependencies": { - "stylus": "^0.54.5", - "nib": "^1.1.2" + "nib": "^1.1.2", + "stylus": "^0.54.5" }, "devDependencies": { "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "eslint": "^6.0.1", "eslint-config-hexo": "^4.0.0", + "hexo": "^4.2.0", "mocha": "^6.0.0", "nyc": "^15.0.0" }, diff --git a/test/index.js b/test/index.js index f39c7b2..7ec07a9 100644 --- a/test/index.js +++ b/test/index.js @@ -1,9 +1,11 @@ 'use strict'; const should = require('chai').should(); // eslint-disable-line +const Hexo = require('hexo'); describe('Stylus renderer', () => { - const ctx = { + const hexo = new Hexo(__dirname, {silent: true}); + const ctx = Object.assign(hexo, { config: { stylus: { compress: false @@ -21,7 +23,7 @@ describe('Stylus renderer', () => { } } } - }; + }); const r = require('../lib/renderer').bind(ctx); @@ -147,4 +149,37 @@ describe('Stylus renderer', () => { ].join('\n') + '\n'); }); }); + + describe('exec filter to extend', () => { + it('should execute filter registered to stylus', () => { + const hexo = new Hexo(__dirname, {silent: true}); + Object.assign(hexo, { + config: { + stylus: { + compress: false + } + } + }); + hexo.extend.filter.register('stylus', style => { + style.define('examples', () => { + return 'foo'; + }); + }); + const filterRender = require('../lib/renderer').bind(hexo); + const body = [ + '.foo', + ' content: examples()', + '' + ].join('\n'); + filterRender({text: body}, {}, (err, result) => { + if (err) throw err; + result.should.eql([ + '.foo {', + ' content: \'foo\';', + '}' + ].join('\n') + '\n'); + }); + }); + }); + }); From 2d25cb008b2c416517c3af8116568f4612b11f63 Mon Sep 17 00:00:00 2001 From: JiangTJ <116749895@qq.com> Date: Tue, 4 Feb 2020 22:47:06 +0800 Subject: [PATCH 2/3] Rename to stylus:renderer and add doc --- README.md | 24 ++++++++++++++++++++++++ lib/renderer.js | 2 +- test/index.js | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1d89a2b..e5abd76 100644 --- a/README.md +++ b/README.md @@ -41,3 +41,27 @@ stylus: [Stylus]: http://stylus-lang.com/ [nib]: http://stylus.github.io/nib/ + +## Extensibility + +This plugin provide a filter `stylus:renderer` to allows you extend it. When there’s something you cannot do in Stylus, define it in JavaScript! + +For example, to define some global variable: + +```js +hexo.extend.filter.register('stylus:renderer', function(style) { + style + // we may define a global variable by passing a `Node` + .define('has-canvas', require('stylus').nodes.false); + // stylus also casts JavaScript values to their Stylus equivalents when possible + .define('families', ['Helvetica Neue', 'Helvetica', 'sans-serif']) + // also allows you to provide a JavaScript-defined function to Stylus + .define('get-list', function(){ + return ['foo', 'bar', 'baz']; + }); +}) +``` + +Save the file in "scripts/" folder and run Hexo as usual. + +Notice: for more JavaScript api, refer to stylus's [documentation](http://stylus-lang.com/docs/js.html). diff --git a/lib/renderer.js b/lib/renderer.js index f76fc0c..4721837 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -51,7 +51,7 @@ module.exports = function(data, options, callback) { stylusConfig .use(defineConfig) - .use(style => this.execFilterSync('stylus', style, {context: this})) + .use(style => this.execFilterSync('stylus:renderer', style, {context: this})) .set('filename', data.path) .set('sourcemap', config.sourcemaps) .set('compress', config.compress) diff --git a/test/index.js b/test/index.js index 7ec07a9..791153d 100644 --- a/test/index.js +++ b/test/index.js @@ -151,7 +151,7 @@ describe('Stylus renderer', () => { }); describe('exec filter to extend', () => { - it('should execute filter registered to stylus', () => { + it('should execute filter registered to stylus:renderer', () => { const hexo = new Hexo(__dirname, {silent: true}); Object.assign(hexo, { config: { @@ -160,7 +160,7 @@ describe('Stylus renderer', () => { } } }); - hexo.extend.filter.register('stylus', style => { + hexo.extend.filter.register('stylus:renderer', style => { style.define('examples', () => { return 'foo'; }); From 42c0d2a0395bd0fef73db5461cf73b6671fe30df Mon Sep 17 00:00:00 2001 From: jiangtj <116749895@qq.com> Date: Mon, 10 Aug 2020 17:56:05 +0800 Subject: [PATCH 3/3] Update hexo version to 5.x --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 942e062..32f9dd8 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "chai-as-promised": "^7.1.1", "eslint": "^7.1.0", "eslint-config-hexo": "^4.0.0", - "hexo": "^4.2.0", + "hexo": "^5.0.2", "mocha": "^8.0.1", "nyc": "^15.0.0" },