diff --git a/README.md b/README.md index 878be6a..1d89a2b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![NPM version](https://badge.fury.io/js/hexo-renderer-stylus.svg)](https://www.npmjs.com/package/hexo-renderer-stylus) [![Coverage Status](https://img.shields.io/coveralls/hexojs/hexo-renderer-stylus.svg)](https://coveralls.io/r/hexojs/hexo-renderer-stylus?branch=master) -Add support for [Stylus] with [nib]. +Add support for [Stylus] with [nib] and other plugins. ## Install @@ -28,17 +28,16 @@ stylus: inline: true sourceRoot: '' basePath: . + plugins: 'nib' ``` -- **Stylus**: - - **compress** - Compress generated CSS (default: `false`) - - -- **Sourcemaps** +- **compress** - Compress generated CSS (default: `false`) +- **sourcemaps** - **comment** - Adds a comment with the `sourceMappingURL` to the generated CSS (default: `true`) - **inline** - Inlines the sourcemap with full source text in base64 format (default: `false`) - **sourceRoot** - `sourceRoot` property of the generated sourcemap - **basePath** - Base path from which sourcemap and all sources are relative (default: `.`) +- **plugins** - Stylus plugin(s) (default: `nib`) [Stylus]: http://stylus-lang.com/ [nib]: http://stylus.github.io/nib/ diff --git a/lib/renderer.js b/lib/renderer.js index d9b92e3..f6a9b93 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -1,7 +1,6 @@ 'use strict'; const stylus = require('stylus'); -const nib = require('nib'); function getProperty(obj, name) { name = name.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, ''); @@ -28,18 +27,29 @@ function getProperty(obj, name) { return result; } +function applyPlugins(stylusConfig, plugins) { + plugins.forEach(plugin => { + const factoryFn = require(plugin.trim()); + stylusConfig.use(factoryFn()); + }); +} + module.exports = function(data, options, callback) { const config = this.config.stylus || {}; const self = this; + const plugins = ['nib'].concat(config.plugins || []); function defineConfig(style) { - style.define('hexo-config', function(data) { + style.define('hexo-config', data => { return getProperty(self.theme.config, data.val); }); } - stylus(data.text) - .use(nib()) + const stylusConfig = stylus(data.text); + + applyPlugins(stylusConfig, plugins); + + stylusConfig .use(defineConfig) .set('filename', data.path) .set('sourcemap', config.sourcemaps)