npm.io
2.0.0 • Published 2d ago

postcss-markdown

Licence
MIT
Version
2.0.0
Deps
4
Size
30 kB
Vulns
0
Weekly
0
Stars
6

PostCSS Markdown Syntax

NPM license NPM version NPM downloads NPM downloads NPM downloads Build Status

PostCSS syntax for parsing Markdown

Getting Started

First thing's first, install the module:

npm install postcss-markdown --save-dev

If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install the corresponding module.

This package is written in ESM and requires Node.js ^22.12 || >=24. CommonJS consumers can still load it with require("postcss-markdown") on these Node.js versions (via require(esm)).

Use Cases

import postcss from "postcss";
import postcssMarkdown from "postcss-markdown";
import postcssScss from "postcss-scss";
import postcssLess from "postcss-less";
import postcssSafeParser from "postcss-safe-parser";
import autoprefixer from "autoprefixer";

const syntax = postcssMarkdown({
    // Enable support for HTML (default: true)
    htmlInMd: true,
    // syntax for parse scss (non-required options)
    scss: postcssScss,
    // syntax for parse less (non-required options)
    less: postcssLess,
    // syntax for parse css blocks (non-required options)
    css: postcssSafeParser,
});
postcss([autoprefixer])
    .process(source, { syntax: syntax })
    .then(function (result) {
        // An alias for the result.css property. Use it with syntaxes that generate non-CSS output.
        result.content;
    });

input:

# title

```css
::placeholder {
    color: gray;
}
```

output:

# title

```css
::-moz-placeholder {
    color: gray;
}
:-ms-input-placeholder {
    color: gray;
}
::placeholder {
    color: gray;
}
```

If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install these module:

Advanced Use Cases

Options
import { createRequire } from "module";
import postcssMarkdown from "postcss-markdown";
import postcssSass from "postcss-sass";
import sugarss from "sugarss";
import postcssCustomSyntax from "postcss-custom-syntax";

const options = {
    rules: [
        {
            // custom language
            test: /^postcss$/i,
            lang: "scss",
        },
        {
            // custom language
            test: /^customcss$/i,
            lang: "custom",
        },
    ],

    // custom parser for CSS (using `postcss-safe-parser`)
    css: "postcss-safe-parser",
    // custom parser for SASS (PostCSS-compatible syntax.)
    sass: postcssSass,
    // custom parser for SCSS (by module name)
    scss: "postcss-scss",
    // custom parser for LESS (by module path)
    less: createRequire(import.meta.url).resolve("postcss-less"),
    // custom parser for SugarSS
    sugarss: sugarss,
    // custom parser for custom language
    custom: postcssCustomSyntax,
};
const syntax = postcssMarkdown(options);

Turning PostCSS off from within your Markdown

PostCSS can be temporarily turned off by using special comments in your Markdown. For example:

<!-- postcss-ignore -->
```css
a {
    color: red;
}
```

Linting with Stylelint

The main use case of this plugin is to apply linting with Stylelint to CSS (and CSS-like) code blocks in markdown file.

You can use it by configuring your stylelint config as follows:

{
    "overrides": [
        {
            "files": ["*.md", "**/*.md"],
            "customSyntax": "postcss-markdown"
        }
    ]
}
Editor integrations
Visual Studio Code

Use the stylelint.vscode-stylelint extension that Stylelint provides officially.

You have to configure the stylelint.validate option of the extension to check .md files, because the extension does not check the *.md file by default.

Example .vscode/settings.json:

{
  "stylelint.validate": [
      ...,
      // ↓ Add "markdown" language.
      "markdown"
  ]

Keywords