Skip to content

Commit

Permalink
Merge pull request #899 from sascha-egerer/master
Browse files Browse the repository at this point in the history
Add abbility to register custom processors to modify the styleguide data
  • Loading branch information
varya committed Mar 2, 2016
2 parents 487378a + 393cf2e commit 9dc6409
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,41 @@ 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(styleguide) {
// this will run after replaceSectionReferences
styleguide.sections[0].description = styleguide.sections[0].description + ' [Description from custom Processor]';
},
30: function(styleguide) {
// 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 @@ -225,7 +225,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 @@ -296,8 +303,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 9dc6409

Please sign in to comment.