Skip to content

Commit

Permalink
wip: play with separate markdoc config
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent c5d8a33 commit 97664e0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 54 deletions.
7 changes: 7 additions & 0 deletions examples/with-markdoc/src/components/Marquee.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<marquee {...Astro.props}><slot /></marquee>

<style>
marquee {
color: red;
}
</style>
6 changes: 2 additions & 4 deletions examples/with-markdoc/src/components/test.mdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hey there

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

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

{% if $shouldMarquee %}
{% mq direction="right" %}
Testing!
Im a marquee!
{% /mq %}
{% /if %}

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

Some `inline code` should help

```js
const testing = true;
function further() {
Expand Down
30 changes: 30 additions & 0 deletions examples/with-markdoc/src/markdoc.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default {
transform: {
variables: {
shouldMarquee: true,
href: 'https://astro.build',
},
tags: {
mq: {
render: 'marquee',
attributes: {
direction: {
type: String,
default: 'left',
matches: ['left', 'right', 'up', 'down'],
errorLevel: 'critical',
},
},
},
link: {
render: 'a',
attributes: {
href: {
type: String,
required: true,
},
},
},
},
},
};
64 changes: 15 additions & 49 deletions examples/with-markdoc/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,52 +1,10 @@
---
import { body } from '../components/test.mdoc';
import { Markdoc } from '@astrojs/markdoc';
import RenderMarkdoc from '../renderer/RenderMarkdoc.astro';
import RedP from '../components/RedP.astro';
import { Code } from 'astro/components';
import { Tag } from '@markdoc/markdoc';
import { ComponentRenderer } from '../renderer/astroNode';
const parsed = Markdoc.parse(body);
const content = Markdoc.transform(parsed, {
variables: {
shouldMarquee: true,
href: 'https://astro.build',
},
tags: {
mq: {
render: 'marquee',
attributes: {
direction: {
type: String,
default: 'left',
matches: ['left', 'right', 'up', 'down'],
errorLevel: 'critical',
},
},
},
link: {
render: 'a',
attributes: {
href: {
type: String,
required: true,
},
},
},
},
});
const code: ComponentRenderer = {
component: Code,
props({ attributes, getTreeNode }) {
return {
...attributes,
lang: attributes.lang ?? attributes['data-language'],
code: attributes.code ?? Markdoc.renderers.html(getTreeNode().children),
};
},
};
import { getTransformed } from '../components/test.mdoc';
import { Code } from 'astro/components';
import Marquee from '../components/Marquee.astro';
---

<html lang="en">
Expand All @@ -61,11 +19,19 @@ const code: ComponentRenderer = {
<h1>Astro</h1>
<article>
<RenderMarkdoc
content={content}
content={await getTransformed()}
components={{
p: RedP,
code,
pre: code,
marquee: Marquee,
pre: {
component: Code,
props({ attributes, getTreeNode }) {
return {
...attributes,
lang: attributes.lang ?? attributes['data-language'],
code: attributes.code ?? Markdoc.renderers.html(getTreeNode().children),
};
},
},
}}
/>
</article>
Expand Down
13 changes: 12 additions & 1 deletion packages/integrations/markdoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ export default function markdoc(partialOptions: {} = {}): AstroIntegration {
'astro:config:setup': async ({ updateConfig, config, addPageExtension, command }: any) => {
addPageExtension('.mdoc');
console.log('Markdoc working!');
const markdocConfigUrl = new URL('./markdoc.config.ts', config.srcDir);

const viteConfig: InlineConfig = {
plugins: [
{
name: '@astrojs/markdoc',
async transform(code, id) {
if (!id.endsWith('.mdoc')) return;
return `export const body = ${JSON.stringify(code)}`;
return `import { Markdoc } from '@astrojs/markdoc';\nexport const body = ${JSON.stringify(
code
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) {
let config = inlineConfig;
if (!config) {
try {
const importedConfig = await import(${JSON.stringify(markdocConfigUrl.pathname)});
config = importedConfig.default.transform;
} catch {}
}
return Markdoc.transform(getParsed(), config) }`;
},
},
],
Expand Down

0 comments on commit 97664e0

Please sign in to comment.