Easily create and maintain style guides using CSS comments

Related tags

CSS mdcss
Overview

NPM Version Build Status

mdcss lets you easily create and maintain style guides with CSS comments using Markdown.

/*---
title:   Buttons
section: Base CSS
---

Button styles can be applied to any element. Typically you'll want to use
either a `<button>` or an `<a>` element:

```example:html
<button class="btn">Click</button>
<a class="btn" href="/some-page">Some Page</a>
```
*/

.btn {
	background-color: black;
	color: white;
}

Usage

Add mdcss to your build tool:

npm install mdcss --save-dev

Node

require('mdcss').process(YOUR_CSS, { /* options */ });

PostCSS

Add PostCSS to your build tool:

npm install postcss --save-dev

Load mdcss as a PostCSS plugin:

postcss([
	require('mdcss')({ /* options */ })
]);

Gulp

Add Gulp PostCSS to your build tool:

npm install gulp-postcss --save-dev

Enable mdcss within your Gulpfile:

var postcss = require('gulp-postcss');

gulp.task('css', function () {
	return gulp.src('./css/src/*.css').pipe(
		postcss([
			require('mdcss')({ /* options */ })
		])
	).pipe(
		gulp.dest('./css')
	);
});

Grunt

Add Grunt PostCSS to your build tool:

npm install grunt-postcss --save-dev

Enable mdcss within your Gruntfile:

grunt.loadNpmTasks('grunt-postcss');

grunt.initConfig({
	postcss: {
		options: {
			processors: [
				require('mdcss')({ /* options */ })
			]
		},
		dist: {
			src: 'css/*.css'
		}
	}
});

Options

theme

Type: NPM Repository
Default: require('mdcss-theme-github')

The theme used by mdcss to create the style guide.

require('mdcss')({
	theme: require('mdcss-theme-github')
})

Theme-specific options may also be passed in from the theme module itself, but note that any global options would then be ignored.

require('mdcss')({
	theme: require('mdcss-theme-github')(/* options */)
})

destination

Type: String
Default: 'styleguide'

The directory to write the style guide to.

assets

Type: Array
Default: []

The list of files or directories to copy into the style guide directory.

index

Type: String
Default: 'index.html'

The file to write the style guide to.

Writing documentation

To add a section of documentation, write a CSS comment that starts with three dashes ---.

/*---

This is documentation.

*/
/*

This is not documentation

*/

The contents of a section of documentation are parsed by Markdown and turned into HTML.

/*---

Button styles can be applied to **any** element. Typically you'll want to use
either a `<button>` or an `<a>` element:

​```html
<button class="btn">Click</button>
<a class="btn" href="/some-page">Some Page</a>
​```

*/
<p>Button styles can be applied to <strong>any</strong> element. Typically you&#39;ll want to use
either a <code>&lt;button&gt;</code> or an <code>&lt;a&gt;</code> element:</p>

<pre><code class="lang-html">&lt;button class=&quot;btn&quot;&gt;Click&lt;/button&gt;
&lt;a class=&quot;btn&quot; href=&quot;/some-page&quot;&gt;Some Page&lt;/a&gt;
</code></pre>

The contents of a section may also be imported from another file.

buttons.md:

Button styles can be applied to **any** element. Typically you'll want to use
either a `<button>` or an `<a>` element:

```html
<button class="btn">Click</button>
<a class="btn" href="/some-page">Some Page</a>
​```

base.css:

/*---
title:  Buttons
import: buttons.md
---*/

The contents of a section may be automatically imported as well. For example, had the import been omitted, a sibling file of base.buttons.md or base.md would have been used (in that order of preference) if they existed.

Details

Additional heading details are added before a second set of three dashes --- in a section. These heading details are parsed and added to the documentation object.

/*---
title:   Buttons
section: Base CSS
---

Button styles can be applied to **any** element.

*/
{
	"title": "Buttons",
	"section": "Base CSS",
	"content": "<p>Button styles can be applied to <strong>any</strong> element.</p>"
}

Writing themes

Creating themes requires an understanding of creating and publishing npm packages.

The easiest way to create a new theme is to visit the boilerplate theme project page, fork and clone it, and then run npm install.

To create a theme from scratch; create an index.js like this one in a new npm package directory:

module.exports = function (themeopts) {
	// initialize the theme
	// example usage:
	// 
	// require('mdcss')({
	//   theme: require('mdcss-theme-mytheme')({ /* opts */ })
	// })

	// return the theme processor
	return function (docs) {
		// do things with the documentation object
		// remember to use __dirname to target this theme directory

		// return a promise
		return new Promise(function (resolve, reject) {
			// resolve an object with an assets path and a compiled template
			resolve({
				assets:   '', // directory of files to copy
				template: '' // contents of style guide to write
			});
		});
	};
};

// this is so mdcss can check whether the plugin has already been initialized
module.exports.type = 'mdcss-theme';

The exports function is where theme options are initialized.

require('mdcss')({
	theme: require('mdcss-theme-mytheme')({ /* theme options */ });
});

The exports function returns a theme processor. The theme processor is what receives the ordered list of all the parsed documentation objects as well as the options originally passed into the mdcss plugin.

Documentation object

Each documentation object may contain the following properties:

  • title: The title of the current section of documentation.
  • name: A unique, hash-safe name of the current section of documentation.
  • section: The proper title of a parent section.
  • content: The body copy of the current section of documentation.
  • parent: The parent section.
  • children: An array of child sections.
  • context: The original Comment node used to generate the current section of documentation.
  • import: A path to the file representing the content of the current section of documentation.

In addition to these properties, a documentation object includes any additional details.

/*---
title:      Buttons
section:    Base CSS
yakkityyak: Don’t Talk Back
---

Button styles can be applied to **any** element.

*/

Have fun, and thanks for using mdcss.

Comments
  • How can I read own CSS file?

    How can I read own CSS file?

    I tried to use mdcss in gulp, but I have a problem.

    gulpfile.js:

    var postcss = require('gulp-postcss');
    var gulp = require('gulp')
    
    gulp.task('css', function () {
        return gulp.src('./src/css/*.css')
          .pipe(
            postcss([
                require('mdcss')
            ])
          );
    });
    

    src/css/sample.css(the code is picked from README):

    /*---
    title:   Buttons
    section: Base CSS
    
    ---
    
    Button styles can be applied to any element. Typically you'll want to use
    either a `<button>` or an `<a>` element:
    
    ​```example:html
    <button class="btn">Click</button>
    <a class="btn" href="/some-page">Some Page</a>
    ​```
    */
    
    .btn {
        background-color: black;
        color: white;
    }
    

    Above is the code I tried.

    But, I could not get a correct result. Here is the generated code block.

    <main>
    
            <section id="base-css">
                <h2>Base CSS</h2>
                <div>
    
    
            <section id="buttons">
                <h3>Buttons</h3>
                <div>
                    <p>Button styles can be applied to any element. Typically you&#39;ll want to use
    either a <code>&lt;button&gt;</code> or an <code>&lt;a&gt;</code> element:</p>
    <p>​```example:html</p>
    <p><button class="btn">Click</button>
    <a class="btn" href="/some-page">Some Page</a>
    ​```</p>
    
    
                </div>
            </section>
    
                </div>
            </section>
    
        <footer>Last modified Wednesday, 13 January 2016 16:09
    </main>
    

    There are no styles in sample.css(.btn{} rule).

    I wish your help.

    awaiting feedback 
    opened by morishitter 17
  • problems with the calculated height of the example:html iframe for floating items

    problems with the calculated height of the example:html iframe for floating items

    Most likely the height of the iframe is calculated before the example css files are loaded

    Screenshot 1 - the iframe is to high when the items are floating:
    iframe to high with floating items

    Screenshot 2 - when I remove the float from the items, the height fits:
    iframe fits

    bug 
    opened by maoberlehner 10
  • Grunt Task Error Fatal error: fs.emptyDir is not a function

    Grunt Task Error Fatal error: fs.emptyDir is not a function

    Hi,

    I try to add mdcss to a Postcss project. But i have the following error Fatal error: fs.emptyDir is not a function.

    My grunt task

    postcss: {
        options: {
            processors: [
                require('mdcss')
            ]
        },
        dev: {
            src: 'app/styles/main.css'
        }
    }
    

    In my package.json

    "grunt": "^0.4.5",
    "grunt-postcss": "^0.7.1",
    "mdcss": "^1.5.0",
    "mdcss-theme-github": "^2.2.0",
    "postcss": "^5.0.14"
    

    Thanks for your help ! :)

    awaiting feedback 
    opened by ethyde 9
  • Error: Cannot find module 'fs-promise'

    Error: Cannot find module 'fs-promise'

    My gulpfile has a import mdcss from 'mdcss'; line in it, and throws the following error stack whenever Gulp is run:

    Error: Cannot find module 'fs-promise'
        at Function.Module._resolveFilename (module.js:455:15)
        at Function.Module._load (module.js:403:25)
        at Module.require (module.js:483:17)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (/home/txh/test/node_modules/mdcss/index.js:2:11)
        at Module._compile (module.js:556:32)
        at Module._extensions..js (module.js:565:10)
        at Object.require.extensions.(anonymous function) [as .js] (/home/txh/test/node_modules/babel-register/lib/node.js:152:7)
        at Module.load (module.js:473:32)
        at tryModuleLoad (module.js:432:12)
        at Function.Module._load (module.js:424:3)
        at Module.require (module.js:483:17)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (/home/txh/test/gulpfile.babel.js:16:1)
        at Module._compile (module.js:556:32)
        at loader (/home/txh/test/node_modules/babel-register/lib/node.js:144:5)
    

    If the above line is commented out, the error goes away.

    opened by TxHawks 7
  • documented css should also be processed in the style guides document

    documented css should also be processed in the style guides document

    Hi,

    I have this css documentation

        /*---
        title:   f-brand + f-light
        section: Text
        ---
    
        Lowercase text:
    
        <div class='f-brand f-light'>lorem ipsum</div>
    
        ```html
        <div class='f-brand f-light'>lorem ipsum</div>
        ```
        */
        .f-brand .f-light,
        .f-brand.f-light{
            font-weight: normal;
            font-family: FuturaStdLight, Arial;
        }
    

    Which is working smoothly : )

    Except that i would really like to also display the resulting rendering, which is why i have twice the same HTML block. One within a code block to show the markup, one outside of the code block to generate the rendering.

    Problem is my documented css are not imported within the context of the document page.

    I see in the readme, the first example include a demo of the button after the render, i d like to do the same thing.

    Did i miss something ?

    opened by maboiteaspam 7
  • Front Matter and Nav Missing

    Front Matter and Nav Missing

    Just upgraded to 1.4.0 and my front matter and nav are missing. I paired it down to 2 partials as well and it won't work for some reason. screen shot 2016-01-28 at 2 19 37 pm

    In my processors:

    require('mdcss')({
           theme: require('mdcss-theme-github')({
                   js: ['dom.js'],
                   css: ['style.css', 'https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400italic,600,700',
     'https://fonts.googleapis.com/icon?family=Material+Icons'],
                   examples: ({
                           htmlcss: '',
                           bodycss: 'margin:1em;',
                           css: ['style.css', 'https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400italic,6
    00,700', 'https://fonts.googleapis.com/icon?family=Material+Icons']
                   })
           })
    }),
    

    My front matter is formatted like this:

    /*---
     title: Grid
     section: Utilities
     ---
    
     Docs...
    
    */
    

    If this is more related to how the github theme works, then I can move this issue to that repo? Thanks

    bug 
    opened by stephenway 6
  • Warning when running grunt PostCSS task after initial run

    Warning when running grunt PostCSS task after initial run

    I've added mdcss to the PostCSS grunt tasks for an upcoming project. When running grunt, after running grunt to generate the initial style guide, a warning is thrown in the PostCSS grunt task. This is a copy of the warning:

    Fatal error: Error: ENOENT, unlink '/Users/username/path/to/style-guide/examples.js',
    Error: ENOENT, unlink '/Users/username/path/to/style-guide/prism.js',
    Error: ENOENT, unlink '/Users/username/path/to/style-guide/script.js',
    Error: ENOENT, unlink '/Users/username/path/to/style-guide/mdcss-logo.png',
    Error: ENOENT, unlink '/Users/username/path/to/style-guide/style.css'
    

    I have custom options in place for mdcss that change the directory name, title, and uses a custom logo. In order to determine if the warning is due to the custom options, I remove the options and used the mdcss defaults. I then deleted the previously generated style guide, and ran grunt twice. On the second run, the same warning appears as shown above.

    I receive this warning no OS X El Capitan 10.11.1.

    awaiting feedback 
    opened by allenmoore 5
  • Request: ability to specify path for import detail

    Request: ability to specify path for import detail

    Right now, it seems like mdcss only knows to look for files imported using the import detail in the same directory as the CSS file.

    I think one common use-case for import is for importing the README file at the beginning of your styleguide.

    If I have the following file structure:

    _  ./
    |__ css/
    |__ README.md
    

    Trying to import the README.md with

    /* ---
    title: Intro
    import: ../README.md
    --- */
    

    Produces the following HTML

    <section id="intro">
      <h2>Intro</h2>
      <div>
        <h2 id="import-readme-md">import: ../README.md</h2>
      </div>
    </section>
    
    opened by TxHawks 4
  • scss support / variable documentation

    scss support / variable documentation

    one of my last (s)css documentation problems, that is not already solved by this tool, is how to document scss variable(s) (values).

    currently I'm doing something like this:

    $variable1: 'value1';
    $variable2: 'value2';
    $variable3: 'value3';
    /*---
    title: Variables
    
    ---
    
    ```css
    $variable1: '#{$variable1}';
    $variable2: '#{$variable2}';
    $variable3: '#{$variable3}';
    -```
    */
    

    The output is somewhat ok but the problem is - this is really ugly, not very "DRY" and it is impossible to document the values of scss maps that way.

    The "perfect" solution would be to use the scss files as source for mdcss (instead of the compiled css) and to implement a special comment format like:

    /*---
    title: Variables
    
    ---
    
    @var $variable1
    @var $variable2
    @var $variable3
    */
    $variable1: 'value1';
    $variable2: 'value2';
    $variable3: 'value3';
    

    The output should be something like:

    <section>
      <h2>Variables</h2>
      <div class="variables">
        <span class="variable-name">$variable1</span>: <span class="variable-value">'value1'</span>;<br />
        <span class="variable-name">$variable2</span>: <span class="variable-value">'value2'</span>;<br />
        <span class="variable-name">$variable3</span>: <span class="variable-value">'value3'</span>;
      </div>
    </section>
    
    opened by maoberlehner 4
  • being able to load html from a file ?

    being able to load html from a file ?

    Hi!

    how do you think about importing html template via a specific instruction instead of inline it within the html comment ?

    I feel little bothered to put indented html in the css comments, for some more complex components.

    thanks ! Great software ! looks really cool.

    opened by maboiteaspam 4
  • Error: EEXIST: file already exists

    Error: EEXIST: file already exists

    Hi, I'm having this error using mdcss as unique postcss plugin in a webpack build. I'm using css loader in module mode and postcss loader :

    ExtractTextPlugin.extract('style', 'css?modules!postcss')
    

    Could it be the fact that the postcss plugins run multiple times on each css module files? The styleguide would have already been created while parsing the first css module file and fail on second generation.

    Would there be a way to prevent that?

    Thanks!

    opened by jonathanasquier 3
  • Styleguide doesn't port over css

    Styleguide doesn't port over css

    I just followed the basic example

    /* index.css */
    
    /*---
    title:   Buttons
    section: Base CSS
    ---
    
    Button styles can be applied to any element. Typically you'll want to use
    either a `<button>` or an `<a>` element:
    
    \```example:html
    <button class="btn">Click</button>
    <a class="btn" href="/some-page">Some Page</a>
    \```
    */
    
    .btn {
    	background-color: black;
    	color: white;
    }
    

    And tried it with postcss:

    // postcss.config.js
    const postcssPresetEnv = require('postcss-preset-env')
    
    module.exports = {
      plugins: [
        postcssPresetEnv({ browsers: '> 3%, ie 11' }),
        require('mdcss')({}),
      ]
    }
    
    cat css/index.css | yarn postcss > output.css
    

    It generated the styleguide but it doesn't port over the styles from index.css

    opened by Risto-Stevcev 2
  • More robust comment parsing?

    More robust comment parsing?

    Hey Jonathan,

    Firstly: thank you for another neat tool. 🙌

    A couple of times I've run into the fact that mdcss is quite picky regarding CSS comment structure/whitespace issues: example:html code blocks were not picked up or the comment was shown completely inline, etc.

    Also: I've grown used to CSSDoc style CSS comments (where each line is preceded by *. This (of course) breaks the comment regexes you use.

    Have you considered using a CSS comment parser such as DSS? It might make the comment parsing a bit more robust and allow us to continue using our preferred commenting style?

    opened by davidhund 0
  • Problem to generate styleguide

    Problem to generate styleguide

    Hey, I'm with a problem to generate my styleguide on windows, it's just a simple task

    gulp.task('style',function()  {
        return gulp.src('css/*.css')
            .pipe(  postcss([ 
                require('mdcss')({
                    title: 'styleguide',
                    examples:  { css: ['../css/main.css'] },
                })
            ]))
            .pipe(gulp.dest('css'));
    });
    

    after compile, the page looks like this:

    styleguide

    Instead of windows, on my mac mdcss works fine!

    opened by bbbbruni 2
  • Documentation from more than just 1 stylesheet

    Documentation from more than just 1 stylesheet

    Unsure if this can actually be done, as I can't see from the documentation. I have partial stylesheets that get included into the main style.scss, however if I put any mdcss documentation in these partials it doesn't show up in the outputted documentation.

    It only works with mdcss comments inside the main style.scss

    Thanks!

    opened by gethinoakes 17
Owner
Jonathan Neal
I like you just the way you are.
Jonathan Neal
A markdown based documentation system for style guides.

Hologram Hologram is a Ruby gem that parses comments in your CSS and helps you turn them into a beautiful style guide. There are two steps to building

Trulia, LLC. 2.2k Nov 12, 2022
Easily create css variables without the need for a css file!

Tailwind CSS Variables This plugin allows you to configure CSS variables in the tailwind.config.js Similar to the tailwindcss configurations you are u

Mert Aşan 111 Dec 22, 2022
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress ?? Looking for v5? The master branch is un

styled-components 38k Dec 31, 2022
Dropbox’s (S)CSS authoring style guide

Dropbox (S)CSS Style Guide “Every line of code should appear to be written by a single person, no matter the number of contributors.” —@mdo General Do

Dropbox 890 Jan 4, 2023
NES-style CSS Framework | ファミコン風CSSフレームワーク

日本語 / 简体中文 / Español / Português / Русский / Français NES.css is a NES-style(8bit-like) CSS Framework. Installation Styles NES.css is available via ei

null 19.2k Jan 5, 2023
Reseter.css - A Futuristic CSS Reset / CSS Normalizer

Reseter.css A CSS Reset/Normalizer Reseter.css is an awesome CSS reset for a website. It is a great tool for any web designer. Reseter.css resets all

Krish Dev DB 1.1k Jan 2, 2023
An NPM package to help frontend developers get started with using SASS and SCSS on your project easily. The Package follows the 7-1 architecture project structure.

Project Title - Create SASS APP Ever wanted to code up a frontend project with SASS & SCSS and you are stuck with building the acclaimed 7-1 architect

Kelechi Okoronkwo 7 Sep 22, 2022
Automatically generate a style guide from your stylesheets.

StyleDocco StyleDocco generates documentation and style guide documents from your stylesheets. Stylesheet comments will be parsed through Markdown and

Jacob Rask 1.1k Sep 24, 2022
styled component for react & style-loader/usable

react-styled ES7 decorator for dynamic stylesheet usage w/ webpack install $ npm install bloody-react-styled --save-dev require import styled from "bl

Matthias Le Brun 37 Sep 26, 2022
🐒 Normalize browsers' default style

My open source work is supported by the community Special thanks to: Differences from normalize.css Smaller Includes only normalizations for the lates

Sindre Sorhus 4.8k Jan 3, 2023
This plugin was created to calibrate 3D printer settings easily.

Calibration Companion This plugin was created to calibrate your 3D printer settings easily. It comes really handy when you want to try a new filament

Guyot François 29 Jun 22, 2022
Spectre.css - A Lightweight, Responsive and Modern CSS Framework

Spectre.css Spectre.css is a lightweight, responsive and modern CSS framework. Lightweight (~10KB gzipped) starting point for your projects Flexbox-ba

Yan Zhu 11.1k Jan 8, 2023
Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation

Aphrodite Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation. Support for colocating y

Khan Academy 5.3k Jan 1, 2023
micro-library for CSS Flexbox and CSS Grid

SpeedGrid micro-library for CSS Flexbox and CSS Grid Overview SpeedGrid dynamically generates inline CSS by specifying the class name. Easy maintenanc

Toshihide Miyake 7 Mar 26, 2022
Tiny CSS framework with almost no classes and some pure CSS effects

no.css INTERACTIVE DEMO I am tired of adding classes to style my HTML. I just want to include a .css file and I expect it to style the HTML for me. no

null 96 Dec 10, 2022
Low-level CSS Toolkit – the original Functional/Utility/Atomic CSS library

Basscss Low-level CSS toolkit – the original Functional CSS library https://basscss.com Lightning-Fast Modular CSS with No Side Effects Basscss is a l

Basscss 5.8k Dec 31, 2022
CSS Boilerplate / Starter Kit: Collection of best-practice CSS selectors

Natural Selection Natural Selection is a CSS framework without any styling at all. It is just a collection of selectors that can be used to define glo

FrontAid CMS 104 Dec 8, 2022
Source code for Chrome/Edge/Firefox/Opera extension Magic CSS (Live editor for CSS, Less & Sass)

Live editor for CSS, Less & Sass (Magic CSS) Extension Live editor for CSS, Less & Sass (Magic CSS) for Google Chrome, Microsoft Edge, Mozilla Firefox

null 210 Dec 13, 2022