Skip to content

Commit 97664e0

Browse files
committed
wip: play with separate markdoc config
1 parent c5d8a33 commit 97664e0

File tree

5 files changed

+66
-54
lines changed

5 files changed

+66
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<marquee {...Astro.props}><slot /></marquee>
2+
3+
<style>
4+
marquee {
5+
color: red;
6+
}
7+
</style>

examples/with-markdoc/src/components/test.mdoc

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Hey there
22

3-
This is a test file?
3+
Look at this table! Built-in to Markdoc, neat.
44

55
{% table %}
66
* Heading 1
@@ -15,14 +15,12 @@ This is a test file?
1515

1616
{% if $shouldMarquee %}
1717
{% mq direction="right" %}
18-
Testing!
18+
Im a marquee!
1919
{% /mq %}
2020
{% /if %}
2121

2222
{% link href=$href %}Link{% /link %}
2323

24-
Some `inline code` should help
25-
2624
```js
2725
const testing = true;
2826
function further() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export default {
2+
transform: {
3+
variables: {
4+
shouldMarquee: true,
5+
href: 'https://astro.build',
6+
},
7+
tags: {
8+
mq: {
9+
render: 'marquee',
10+
attributes: {
11+
direction: {
12+
type: String,
13+
default: 'left',
14+
matches: ['left', 'right', 'up', 'down'],
15+
errorLevel: 'critical',
16+
},
17+
},
18+
},
19+
link: {
20+
render: 'a',
21+
attributes: {
22+
href: {
23+
type: String,
24+
required: true,
25+
},
26+
},
27+
},
28+
},
29+
},
30+
};

examples/with-markdoc/src/pages/index.astro

+15-49
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,10 @@
11
---
2-
import { body } from '../components/test.mdoc';
32
import { Markdoc } from '@astrojs/markdoc';
43
import RenderMarkdoc from '../renderer/RenderMarkdoc.astro';
5-
import RedP from '../components/RedP.astro';
6-
import { Code } from 'astro/components';
7-
import { Tag } from '@markdoc/markdoc';
8-
import { ComponentRenderer } from '../renderer/astroNode';
9-
10-
const parsed = Markdoc.parse(body);
11-
const content = Markdoc.transform(parsed, {
12-
variables: {
13-
shouldMarquee: true,
14-
href: 'https://astro.build',
15-
},
16-
tags: {
17-
mq: {
18-
render: 'marquee',
19-
attributes: {
20-
direction: {
21-
type: String,
22-
default: 'left',
23-
matches: ['left', 'right', 'up', 'down'],
24-
errorLevel: 'critical',
25-
},
26-
},
27-
},
28-
link: {
29-
render: 'a',
30-
attributes: {
31-
href: {
32-
type: String,
33-
required: true,
34-
},
35-
},
36-
},
37-
},
38-
});
394
40-
const code: ComponentRenderer = {
41-
component: Code,
42-
props({ attributes, getTreeNode }) {
43-
return {
44-
...attributes,
45-
lang: attributes.lang ?? attributes['data-language'],
46-
code: attributes.code ?? Markdoc.renderers.html(getTreeNode().children),
47-
};
48-
},
49-
};
5+
import { getTransformed } from '../components/test.mdoc';
6+
import { Code } from 'astro/components';
7+
import Marquee from '../components/Marquee.astro';
508
---
519

5210
<html lang="en">
@@ -61,11 +19,19 @@ const code: ComponentRenderer = {
6119
<h1>Astro</h1>
6220
<article>
6321
<RenderMarkdoc
64-
content={content}
22+
content={await getTransformed()}
6523
components={{
66-
p: RedP,
67-
code,
68-
pre: code,
24+
marquee: Marquee,
25+
pre: {
26+
component: Code,
27+
props({ attributes, getTreeNode }) {
28+
return {
29+
...attributes,
30+
lang: attributes.lang ?? attributes['data-language'],
31+
code: attributes.code ?? Markdoc.renderers.html(getTreeNode().children),
32+
};
33+
},
34+
},
6935
}}
7036
/>
7137
</article>

packages/integrations/markdoc/src/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,25 @@ export default function markdoc(partialOptions: {} = {}): AstroIntegration {
99
'astro:config:setup': async ({ updateConfig, config, addPageExtension, command }: any) => {
1010
addPageExtension('.mdoc');
1111
console.log('Markdoc working!');
12+
const markdocConfigUrl = new URL('./markdoc.config.ts', config.srcDir);
1213

1314
const viteConfig: InlineConfig = {
1415
plugins: [
1516
{
1617
name: '@astrojs/markdoc',
1718
async transform(code, id) {
1819
if (!id.endsWith('.mdoc')) return;
19-
return `export const body = ${JSON.stringify(code)}`;
20+
return `import { Markdoc } from '@astrojs/markdoc';\nexport const body = ${JSON.stringify(
21+
code
22+
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) {
23+
let config = inlineConfig;
24+
if (!config) {
25+
try {
26+
const importedConfig = await import(${JSON.stringify(markdocConfigUrl.pathname)});
27+
config = importedConfig.default.transform;
28+
} catch {}
29+
}
30+
return Markdoc.transform(getParsed(), config) }`;
2031
},
2132
},
2233
],

0 commit comments

Comments
 (0)