Topdoc parser built in PostCSS
npm install postcss-topdoc



---
title="Philosopher’s stone, logo of PostCSS"
src="http://postcss.github.io/postcss/logo.svg">
Topdoc parser built in
postcss.
There's a few ways to add this plugin to your project, but it's probably easiest
to just use NPM to add postcss-topdoc to your
dependencies.
``sh`
npm install --save postcss-topdoc
This is an odd package, it uses the same api as the postcss plugin, but it
doesn't do what a traditional plugin should do, which is to transform the css in
someway. Instead it just gathers the information from the comments and turns it
into a TopDocument result object and attaches it to the results as the topdoc
property.
You can use it as a plugin like this:
`js
var postcss = require('postcss');
var topdoc = require('postcss-topdoc');
var input = 'some css string';
var opts = {
/ topdoc-postcss options /
};
postcss([topdoc(opts)])
.process(input, {from: 'fixtures/button.css'})
.then(function(result) {
console.log(result.topdoc); //string of resultant css
});
`
But if you don't want to use it as a plugin you can also just use the parser:
`js
var TopdocParser = require('postcss-topdoc/lib/topdoc-parser');
var input = read('./fixtures/button.css');
postcss()
.process(input, {from: 'fixtures/button.css'})
.then(result => {
var opts = {
/ postcss-npm options /
};
const topdocParser = new TopdocParser(result.root, result, opts);
console.log(topdocParser.topdoc); // this is the {TopDocument}
});
`
If set to true the Components in the TopDocument object will be returned withnodes
an array of the corresponding PostCSS Nodes
as the property.
`js
var TopdocParser = require('postcss-topdoc/lib/topdoc-parser');
var input = read('./fixtures/button.css');
postcss()
.process(input, {from: 'fixtures/button.css'})
.then(result => {
var opts = {includeNodes: true};
const topdocParser = new TopdocParser(result.root, result, opts);
console.log(topdocParser.topdoc.components[0].nodes); // these are the PostCSS {Node}s that correspond to this component.
});
`
This regex is used to identify which block comments contain Topdoc data to be
parsed. It defaults to /^(?:\s)*(topdoc)/ which matches comments like this:
`css`
/* topdoc
name: Button
description: A simple button
modifiers:
:active: Active state
.is-active: Simulates an active state on mobile devices
:disabled: Disabled state
.is-disabled: Simulates a disabled state on mobile devices
markup: |
Button
Button
Button
tags:
- desktop
- light
- mobile
- button
- quiet
*/
But if you prefer to not use the keyword topdoc feel free to pass your regex
to use as the test.
`js
var TopdocParser = require('postcss-topdoc/lib/topdoc-parser');
var input = read('./fixtures/button.css');
postcss()
.process(input, {from: 'fixtures/button.css'})
.then(result => {
var opts = {commentRegExp: /^(?:\s)*(td)/};
const topdocParser = new TopdocParser(result.root, result, opts);
console.log(topdocParser.topdoc);
});
`
The above example would match comments that start with /* td.
There is some data that might be needed by the template that isn't defined in
the individual components this can be passed to the parser as fileData which
will all be added as properties of the resultant {TopDocument}.
It is required that it has at least a title, filename, or sourcePath. IfsourcePath
given a and not provided a specific files it will use stringfilename
after the last path separator to create a , and the filename is usedtitle
to create a property if not included.
`js`
postcss([
topdoc({
fileData: {
sourcePath: 'fixtures/button.css',
template: 'lib/template.jade',
},
}),
])
.process(input, {from: 'fixtures/button.css'})
.then(result => {
console.log(result.topdoc);
});
In the above example the result.topdoc will be:
`js`
TopDocument {
sourcePath: 'fixtures/button.css',
template: 'lib/template.jade',
filename: 'button.css',
title: 'Button',
components: [] // {TopComponent}s here
}
Find the definition on
GitHub.
#### Properties
- css PostCSS {Node} root node object.results
- PostCSS {Result} object.opts
- (optional) {Object} plugin options.commentRegExp
- (optional) {RegExp} used to identify TopDoc comments;/^(?:\s)*(topdoc)/
defaults fileData
- (optional) {Object} passed through to the template.title
- (optional) {String} document title.filename
- (optional) {String} name of the original css file. Will be usedtitle
to create if it is not set.sourcePath
- (optional) {String} path to the original css file. Will be usedfilename
to create if it is not set.includeNodes
- {Bool} If true the components in the results will includenodes
an array of the corresponding PostCSS {Node}s as the property.topdoc
- {TopDocument} the result of parsing css content with
Topdoc comment blocks.
Find the definition on
GitHub.
#### Properties
- title {String} Title of the project. Can be provided to the constructor, iffilename
not PostCSS Topdoc will generate it from .filename
- {String} name of the css file that contains the componentsourcePath
definition. Can be provided to the constructor, if not PostCSS Topdoc will
generate it from .sourcePath
- {String} path to the source of the css file that contains thefrom
component definition. Can be provided to the constructor, if not PostCSS
Topdoc will try to generate it from the property in the
processOptions.
Find the definition on
GitHub.
#### Properties
- name {String} (required) Components need at least a name property.markup
- {String} (optional) If the component data is being used to tomarkup
generate html style guides, they should have a property.commentStart
- {Object} The location of the start of the Topdoc comment.line
- {int} starting line number.column
- {int} starting column number.commentEnd
- {Object} The location of the end of the Topdoc comment.line
- {int} ending line number.column
- {int} ending column number.css
- {String} (optional) If able to get css between topdoc comments it is
included as the css property.
- Anything else.
TopComponents can have any additional properties needed. They are defined as
YAML in a css block comment that matches the
commentRegExp.
Most YAML syntax is pretty straight forward, just make sure to use the | whenmarkup
defining multiline strings like .
`css`
/* topdoc
name: Button
description: A simple button
modifiers:
:active: Active state
.is-active: Simulates an active state on mobile devices
:disabled: Disabled state
.is-disabled: Simulates a disabled state on mobile devices
markup: |
Button
Button
Button
tags:
- desktop
- light
- mobile
- button
- quiet
*/
Would produce the following result:
`js``
TopComponent {
name: 'Button',
description: 'A simple button',
modifiers:
{ ':active': 'Active state',
'.is-active': 'Simulates an active state on mobile devices',
':disabled': 'Disabled state',
'.is-disabled': 'Simulates a disabled state on mobile devices' },
markup: 'Button\nButton\nButton\n',
tags: [ 'desktop', 'light', 'mobile', 'button', 'quiet' ],
css: 'css string here'