@mdit/plugin-layout
Plugin for creating Flexbox, CSS Grid, and Multi-column layouts using directives.
Installing the plugin twice is a no-op: the first installation's options, parser rules and renderers stay in effect.
Usage
import { layout } from "@mdit/plugin-layout";
import MarkdownIt from "markdown-it";
const mdIt = new MarkdownIt().use(layout);
mdIt.render(`\
@flexs gap-4
@flex
Left content
@flex
Right content
@end
`);Syntax
The plugin uses directive-based syntax with a "plural-for-container, singular-for-item" convention:
| Layout Mode | Container | Item | Close |
|---|---|---|---|
| Flexbox | @flexs | @flex | @end |
| CSS Grid | @grids | @grid | @end |
| Multi-column | @columns | @column | @end |
Any positive number of leading @ characters can open a container outside another layout container, for example @flexs, @@flexs or @@@flexs. That number is the marker count of the container: its items and closing @end must use exactly the same count.
Attribute Injection
Directives support class and id selectors attached directly to the directive name:
@flexs.nav#top gap-4 items-center.class-nameadds a CSS class#idadds an HTML id- Space-separated text after selectors are utility classes mapped to inline styles
Nesting
Containers are nested with fewer leading @ characters than their parent. An outer @@flexs can contain an inner @grids, while both items and @end keep the marker count of their own container:
@@flexs
@@flex
@grids grid-cols-2
@grid
Nested content
@end
@@endThree levels follow the same rule: @@@flexs wraps @@flexs, which wraps @grids:
@@@flexs
@@@flex
@@flexs
@@flex
@grids grid-cols-2
@grid
Deep content
@end
@@end
@@@endA nested container must have a smaller marker count than its parent. Inside @flexs, both @grids and @@grids are parsed as ordinary text:
@flexs
@flex
@grids
@grid
@@grids
@@grid
This is text, not a nested grid.
@endDirective lines may be indented by 0-3 spaces relative to the current block; the indentation is decorative. Lines indented 4+ spaces are indented code blocks or lazy paragraph continuations and never start directives, matching CommonMark.
A container without a matching @end is auto-closed at the end of its parent block. Content that is outdented past the parent block (for example a top-level line after a list-indented container) is parsed by the parent normally. At the same block level (for example inside a nested container or list item; blockquote-prefixed lines are exempt), a same-type item marker or an @end whose @ count matches an enclosing container is treated as a boundary; avoid writing such markers as literal text there.
Supported Utilities
Flexbox
Direction:
flex-rowflex-colflex-row-reverseflex-col-reverse
Wrap:
flex-wrapflex-nowrapflex-wrap-reverse
Flex:
flex-1flex-autoflex-initialflex-none
Grow/Shrink:
growgrow-0shrinkshrink-0
Order:
order-{n}order-firstorder-lastorder-none
Grid
Columns:
grid-cols-{n}grid-cols-none
Rows:
grid-rows-{n}grid-rows-none
Span:
col-span-{n}col-span-fullrow-span-{n}row-span-full
Start/End:
col-start-{n}col-end-{n}row-start-{n}row-end-{n}
Auto Flow:
grid-flow-rowgrid-flow-colgrid-flow-densegrid-flow-row-densegrid-flow-col-dense
Auto Sizing:
auto-cols-autoauto-cols-minauto-cols-maxauto-cols-frauto-rows-autoauto-rows-minauto-rows-maxauto-rows-fr
Spacing & Alignment
Gap:
gap-{n}gap-x-{n}gap-y-{n}gap-pxgap-x-pxgap-y-px
Justify:
justify-startjustify-endjustify-centerjustify-betweenjustify-aroundjustify-evenlyjustify-stretch
Justify Items/Self:
justify-items-{value}justify-self-{value}
Align Items/Self/Content:
items-{value}self-{value}content-{value}
Place:
place-content-{value}place-items-{value}place-self-{value}
Multi-column
Columns:
columns-{n}
Breaks:
break-after-{value}break-before-{value}break-inside-{value}
Span:
.span-allclass maps tocolumn-span: all
Other
Aspect Ratio:
aspect-autoaspect-squareaspect-video
Options
booleantrueWhen true, utility classes are converted to inline CSS styles. When false, utilities are added as CSS class names for use with Tailwind CSS or custom stylesheets.
Demo
Left Column
This content grows to fill available space.
Right Column
This content takes its natural width.
@flexs gap-4 items-center
@flex.flex-demo flex-1
### Left Column
This content grows to fill available space.
@flex.flex-demo
### Right Column
This content takes its natural width.
@end