Skip to content

Commit

Permalink
docs: support for partials
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Aug 20, 2023
1 parent 86b6143 commit b6e96b2
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 35 deletions.
7 changes: 0 additions & 7 deletions apps/demo/src/lib/Layout.svelte

This file was deleted.

4 changes: 2 additions & 2 deletions apps/demo/src/lib/Nodes.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script context="module">
export { default as Heading } from './Heading.svelte';
export { default as Fence } from './Fence.svelte';
export { default as Heading } from './nodes/Heading.svelte';
export { default as Fence } from './nodes/Fence.svelte';
</script>
4 changes: 4 additions & 0 deletions apps/demo/src/lib/Tags.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script context="module">
export { default as Addition } from './tags/Addition.svelte';
export { default as Multiply } from './tags/Multiply.svelte';
</script>
2 changes: 0 additions & 2 deletions apps/demo/src/lib/Test.svelte

This file was deleted.

4 changes: 0 additions & 4 deletions apps/demo/src/lib/index.ts

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions apps/demo/src/lib/layouts/Default.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot />
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
* @type {number}
*/
export let b;
/**
* @type {number}
*/
export let c = 0;
</script>

<b>Addition</b>
<p>
<b>{a}</b> + <b>{b}</b> = <b>{Number(a) + Number(b) + Number(c)}</b>
<b>{a}</b> + <b>{b}</b> = <b>{Number(a) + Number(b)}</b>
</p>
File renamed without changes.
3 changes: 3 additions & 0 deletions apps/demo/src/partials/nested/file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Nested

I am a nested partial.
3 changes: 3 additions & 0 deletions apps/demo/src/partials/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Partial

I am a partial.
3 changes: 3 additions & 0 deletions apps/demo/src/partials/variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Partial with variable

{% $name %}
1 change: 1 addition & 0 deletions apps/demo/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<a href="/nodes">nodes</a> |
<a href="/tags">tags</a> |
<a href="/layouts">layouts</a> |
<a href="/partials">partials</a> |
<a href="/advanced">advanced</a> |
<a target="_blank" href="https://github.com/TorstenDittmann/svelte-markdoc-preprocess">github</a>
{#if dev}
Expand Down
38 changes: 38 additions & 0 deletions apps/demo/src/routes/partials/+page.markdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Partials

You pass a directory to the configuration and it will automatically load all files to be used as [partials](https://markdoc.dev/docs/partials).

```js
// svelte.config.js
markdoc({
partials: join(dirname(fileURLToPath(import.meta.url)), './src/lib/partials'),
})
```

```md
<!-- ./src/lib/partials/header.md -->
# My header
```

Here's an example of including the `header.md` file as a partial.

```md
<!-- ./src/routes/+page.markdoc -->
{% partial file="header.md" /%}
```

### Passing variables

Partials are like any other tags, so you can pass variables as attributes to them such as:

```md
<!-- ./src/routes/+page.markdoc -->
{% partial file="header.md" variables={name: "My header name"} /%}
```

and access the variables as you would in a regular Markdoc document:

```md
<!-- ./src/lib/partials/header.md -->
# {% $name %}
```
13 changes: 9 additions & 4 deletions apps/demo/src/routes/playground/components/+page.markdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{% mytest %}
I am slot content.
{% /mytest %}
{% addition a=4 b=4 /%}

{% addition a=4 b=6 /%}
{% multiply a=3 b=8 /%}

{% partial file="test.md" /%}

{% partial file="nested/file.md" /%}

{% partial
file="variable.md"
variables={name: "I am passed to a variable."} /%}
17 changes: 8 additions & 9 deletions apps/demo/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { markdoc } from 'svelte-markdoc-preprocess';

const layout = join(dirname(fileURLToPath(import.meta.url)), './src/lib/Layout.svelte');
const nodes = join(dirname(fileURLToPath(import.meta.url)), './src/lib/Nodes.svelte');
function absoulute(file) {
return join(dirname(fileURLToPath(import.meta.url)), file);
}

/** @type {import('@sveltejs/kit').Config} */
const config = {
Expand All @@ -14,14 +15,12 @@ const config = {
preprocess: [
vitePreprocess(),
markdoc({
tags: layout,
nodes,
tags: absoulute('./src/lib/Tags.svelte'),
nodes: absoulute('./src/lib/Nodes.svelte'),
partials: absoulute('./src/partials'),
layouts: {
default: layout,
alternative: join(
dirname(fileURLToPath(import.meta.url)),
'./src/lib/LayoutAlternative.svelte',
),
default: absoulute('./src/lib/layouts/Default.svelte'),
alternative: absoulute('./src/lib/layouts/Alternative.svelte'),
},
}),
],
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6e96b2

Please sign in to comment.