Skip to content

Commit

Permalink
Add abbility to register own processors to modify the styleguide data
Browse files Browse the repository at this point in the history
Resolves #898
  • Loading branch information
Sascha Egerer committed Mar 1, 2016
1 parent 755274d commit 3b7d31c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,40 @@ For example, to parse all .css files using postcss parser, following configurati
}
```

<a name="option-styleguideProcessors"></a>
**styleguideProcessors** (object, optional)

default:

```js
styleguideProcessors: {}
```

Styleguide has several processors that enrich or modify the data. For example the `sg-wrapper` replacement is done by a processor.
You can add you own processor to enrich the styleguide data with you own content or modifications.
You can also override existing functionality by overwriting the related processor.
Currently these processors exist by default and should not be overwritten unless you know what you are doing:

```js
styleguideProcessors: {
10: replaceSectionReferences,
20: generateSectionWrapperMarkup
}
```

You can define your own processors:

```js
styleguideProcessors: {
11: function() {
// this will run after replaceSectionReferences
},
30: function() {
// this will run after generateSectionWrapperMarkup
}
}
```

<a name="option-filesConfig"></a>
**filesConfig** (array, optional) **(Experimental feature)**

Expand Down
5 changes: 5 additions & 0 deletions demo-gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ gulp.task('styleguide:generate', function() {
styleVariables: 'lib/app/css/_styleguide_variables.css',
parsers: {
css: 'postcss'
},
styleguideProcessors: {
40: function(styleguide) {
styleguide.sections[0].description = styleguide.sections[0].description + ' [Description from custom Processor]';
}
}
}))
.pipe(gulp.dest(outputPath));
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function sanitizeOptions(opt) {
less: 'less',
postcss: 'postcss'
},
filesConfig: opt.filesConfig
filesConfig: opt.filesConfig,
styleguideProcessors: opt.styleguideProcessors || {}
};
}

Expand Down
16 changes: 13 additions & 3 deletions lib/styleguide.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,14 @@ module.exports.generate = function(options) {
throughOpts = {
objectMode: true,
allowHalfOpen: false
};
},
styleguideProcessors = _.extend(
{
10: replaceSectionReferences,
20: generateSectionWrapperMarkup
},
opt.styleguideProcessors
);

function bufferFileContents(file, enc, done) {
if (file.isNull()) {
Expand Down Expand Up @@ -295,8 +302,11 @@ module.exports.generate = function(options) {
}

options.enableJade && generateJadeMarkup(styleguide, options);
replaceSectionReferences(styleguide);
generateSectionWrapperMarkup(styleguide);

_.each(styleguideProcessors, function(processor) {
processor(styleguide);
});

copyUsedOptionsToJsonConfig(opt, styleguide);
appendUsedVariablesToEachBlock(opt, styleguide);
addFileHashesAndReplaceAbsolutePaths(styleguide);
Expand Down

0 comments on commit 3b7d31c

Please sign in to comment.