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: configurable plugins #38

Merged
merged 2 commits into from
Sep 5, 2019
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
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