22
33Install the package:
44
5- ```bash
5+ ```shell
66npm i -D svelte-markdoc-preprocess
77```
88
99Add the preprocessor and new extensions to your `svelte.config.js`:
10- ```js
10+ ```javascript
1111import { markdoc } from 'svelte-markdoc-preprocess';
1212
1313const config = {
@@ -48,7 +48,7 @@ markdoc({
4848 dirname(fileURLToPath(import.meta.url)),
4949 './src/lib/Layout.svelte'
5050 ),
51- some_other_layout:join(
51+ some_other_layout: join(
5252 dirname(fileURLToPath(import.meta.url)),
5353 './src/lib/SomeOtherLayout.svelte'
5454 )
@@ -69,7 +69,7 @@ Layout files are basically Svelte components with a slot.
6969
7070And use them in using the `layout` key in frontmatter. If no layout is defined with frontmatter, the default layout will be used.
7171
72- ```md
72+ ```
7373<!-- +page.markdoc -->
7474---
7575layout: some_other_layout
@@ -78,17 +78,67 @@ layout: some_other_layout
7878# some other content
7979```
8080
81- ## Components
81+ ## Nodes
82+
83+ You can use Svelte components in your markdown files, you can define Svelte Component for each node.
84+
85+ Create a Svelte file and export Svelte components with the same name as the node from the module context.
8286
83- You can export components from your used layout and use them in your Markdoc files.
8487
8588```html
86- <!-- ./src/lib/Layout .svelte -->
89+ <!-- ./src/lib/Nodes .svelte -->
8790<script context="module">
88- export { default as Addition } from './Addition .svelte';
91+ export { default as Heading } from './Heading .svelte';
8992</script>
93+ ```
9094
91- <slot />
95+ ```js
96+ // svelte.config.js
97+ markdoc({
98+ nodes: join(dirname(fileURLToPath(import.meta.url)), './src/lib/Nodes.svelte'),
99+ })
100+ ```
101+
102+ ```html
103+ <!-- ./src/lib/Heading.svelte -->
104+ <script>
105+ export let level;
106+ </script>
107+
108+ <svelte:element this={`h${level}`}><slot /></svelte:element>
109+ ```
110+
111+ You can find a list of available nodes [here](https://markdoc.dev/docs/nodes#built-in-nodes).
112+
113+ ## Tags
114+
115+ You can use Svelte components for tags, the same way you do for nodes.
116+
117+ Create a Svelte file and export Svelte components with the same name as the node from the module context.
118+
119+
120+ ```html
121+ <!-- ./src/lib/Tags.svelte -->
122+ <script context="module">
123+ export { default as Multiply } from './Multiply.svelte';
124+ </script>
125+ ```
126+
127+ ```js
128+ // svelte.config.js
129+ markdoc({
130+ tags: join(dirname(fileURLToPath(import.meta.url)), './src/lib/Tags.svelte'),
131+ })
132+ ```
133+
134+ ```html
135+ <!-- ./src/lib/Multiply.svelte -->
136+ <script lang="ts">
137+ export let a;
138+ export let b;
139+ </script>
140+
141+ {a} * {b} = {a * b}
92142```
93143
94- You can use the components from a layout file like this `{% addition a=4 b=6 % }`.
144+ Now you can use them in your markdown files like this `{{ multiply a=2 b=3 } }`.
0 commit comments