Skip to content

Commit

Permalink
Merge pull request #38 from curbengh/plugins
Browse files Browse the repository at this point in the history
feat: configurable plugins
  • Loading branch information
curbengh authored Sep 5, 2019
2 parents cd33c4c + 3860aa7 commit 43c4c2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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/
18 changes: 14 additions & 4 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -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(/^\./, '');
Expand All @@ -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)
Expand Down

0 comments on commit 43c4c2d

Please sign in to comment.