PostCSS Markdown Syntax
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.
Use Cases
const postcss = require("postcss");
const syntax = require("postcss-markdown")({
htmlInMd: true,
scss: require("postcss-scss"),
less: require("postcss-less"),
css: require("postcss-safe-parser"),
});
const autoprefixer = require("autoprefixer");
postcss([autoprefixer])
.process(source, { syntax: syntax })
.then(function (result) {
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
const options = {
rules: [
{
test: /^postcss$/i,
lang: "scss",
},
{
test: /^customcss$/i,
lang: "custom",
},
],
css: "postcss-safe-parser",
sass: require("postcss-sass"),
scss: "postcss-scss",
less: require.resolve("./node_modules/postcss-less"),
sugarss: require("sugarss"),
custom: require("postcss-custom-syntax"),
};
const syntax = require("postcss-markdown")(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"
]