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

feat(filter): add stylus filter to extend #45

Merged
merged 4 commits into from
Aug 14, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tmp/
*.log
.idea/
.nyc_output/
yarn.lock
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ theme_config:

[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).
1 change: 1 addition & 0 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function(data, options, callback) {

stylusConfig
.use(defineConfig)
.use(style => this.execFilterSync('stylus:renderer', style, {context: this}))
.set('filename', data.path)
.set('sourcemap', config.sourcemaps)
.set('compress', config.compress)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
"author": "Tommy Chen <[email protected]> (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": "^7.1.0",
"eslint-config-hexo": "^4.0.0",
"hexo": "^5.0.2",
"mocha": "^8.0.1",
"nyc": "^15.0.0"
},
Expand Down
39 changes: 37 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,7 +23,7 @@ describe('Stylus renderer', () => {
}
}
}
};
});

const r = require('../lib/renderer').bind(ctx);

Expand Down Expand Up @@ -147,4 +149,37 @@ describe('Stylus renderer', () => {
].join('\n') + '\n');
});
});

describe('exec filter to extend', () => {
it('should execute filter registered to stylus:renderer', () => {
const hexo = new Hexo(__dirname, {silent: true});
Object.assign(hexo, {
config: {
stylus: {
compress: false
}
}
});
hexo.extend.filter.register('stylus:renderer', 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');
});
});
});

});