Style Directives
styleDirectives: boolean;
Apply classes from style directive to surrounding element.
- default:
false
true
, to apply classes to surrounding element
The directive style
is supported in all three directive forms
- container -
:::style{.some-class} ... :::
around a list of elements - leaf -
::style{.some-class}
inside container elements - text -
:style{.some-class}
inside paragraphs or spans
Leaf and text directive will apply the classes from the directive to the parent element and remove the directive from the MDAST. Using the container form will apply the class to the generic <div>
element that is created by the directive itself, i.e. the following MDX snippet
:::style{.bg-accent}
## Chapter 1
::style{.decent}
A lot of text here.
:::
will result in this HTML
<div class="bg-accent decent">
<h2>Chapter 1</h2>
<p>A lot of text here.</p>
</div>
As you can see, if the classes from multiple directives are applied to the same element, the class list is joined (the class decent
from the leaf directive is applied to its containing element, which in this case is the generic <div>
element from the container style directive).
Because lists are not present in Markdown as such (only the list items), style could not be applied to the list as a whole. Therefore, there is a special directive ::list-style
that applies the classes from the directive to the succeeding list, if there is one, i.e.
::list-style{.nav}
- Home
- Blog
- Docs
will result in this HTML
<ul class="nav">
<li>Home</li>
<li>Blog</li>
<li>Docs</li>
</ul>
Prerequisite: remark-directive
⚠️ In order to use this feature, you must insert the plugin
remark-directive
beforeastro-m2dx
.
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import m2dx from 'astro-m2dx';
import remarkDirective from 'remark-directive';
/** @type {import('astro-m2dx').Options} */
const m2dxOptions = {
styleDirectives: true,
};
// https://astro.build/config
export default defineConfig({
integrations: [mdx()],
markdown: {
remarkPlugins: [
remarkDirective, // required for styleDirectives
[m2dx, m2dxOptions],
],
extendDefaultPlugins: true,
},
});
One final request: This feature allows to mix content and representation, use carefully and prefer semantic class names over visual ones (I know the examples use some visual ones ;-()