feat: move Renderer to markdoc, get Content component!

This commit is contained in:
bholmesdev 2023-02-08 09:53:35 -05:00
parent da7bdb4e70
commit 7a941a0b2d
9 changed files with 45 additions and 32 deletions

View file

@ -3,3 +3,32 @@ title: Example!
--- ---
# Hey there # Hey there
Look at this table! Built-in to Markdoc, neat.
{% table %}
* Heading 1
* Heading 2
---
* Row 1 Cell 1
* Row 1 Cell 2
---
* Row 2 Cell 1
* Row 2 cell 2
{% /table %}
{% if $shouldMarquee %}
{% mq direction="right" %}
Im a marquee!
{% /mq %}
{% /if %}
{% link href=$href %}Link{% /link %}
```js
const testing = true;
function further() {
console.log('still highlighted!')
}
```

View file

@ -1,14 +1,13 @@
--- ---
import { Markdoc } from '@astrojs/markdoc'; import { Markdoc } from '@astrojs/markdoc';
import RenderMarkdoc from '../renderer/RenderMarkdoc.astro';
import { getTransformed } from '../components/test.mdoc';
import { Code } from 'astro/components'; import { Code } from 'astro/components';
import Marquee from '../components/Marquee.astro'; import Marquee from '../components/Marquee.astro';
import { getEntryBySlug } from 'astro:content'; import { getEntryBySlug } from 'astro:content';
import RedP from '../components/RedP.astro';
const mdocEntry = await getEntryBySlug('blog', 'test'); const testEntry = await getEntryBySlug('blog', 'test');
console.log(mdocEntry); console.log(testEntry);
const { Content } = await testEntry.render();
--- ---
<html lang="en"> <html lang="en">
@ -22,10 +21,10 @@ console.log(mdocEntry);
<body> <body>
<h1>Astro</h1> <h1>Astro</h1>
<article> <article>
<RenderMarkdoc <Content
content={await getTransformed()}
components={{ components={{
marquee: Marquee, marquee: Marquee,
p: RedP,
pre: { pre: {
component: Code, component: Code,
props({ attributes, getTreeNode }) { props({ attributes, getTreeNode }) {

View file

@ -184,7 +184,7 @@ async function render({
return { return {
Content, Content,
headings: mod.getHeadings(), headings: mod.getHeadings?.() ?? [],
remarkPluginFrontmatter: mod.frontmatter, remarkPluginFrontmatter: mod.frontmatter ?? {},
}; };
} }

View file

@ -36,12 +36,12 @@ export function createAstroNode(
return ''; return '';
} }
if (Object.hasOwn(components, node.name)) { if (node.name in components) {
const componentRenderer = components[node.name]; const componentRenderer = components[node.name];
const component = const component =
'component' in componentRenderer ? componentRenderer.component : componentRenderer; 'component' in componentRenderer ? componentRenderer.component : componentRenderer;
const props = const props =
'props' in componentRenderer 'props' in componentRenderer && typeof componentRenderer.props === 'function'
? componentRenderer.props({ ? componentRenderer.props({
attributes: node.attributes, attributes: node.attributes,
getTreeNode() { getTreeNode() {

View file

@ -0,0 +1 @@
export { default as Renderer } from './Renderer.astro';

View file

@ -17,23 +17,6 @@ const contentEntryType = {
rawData: parsed.matter, rawData: parsed.matter,
}; };
}, },
async render({ entry }: { entry: any }) {
function getParsed() {
return Markdoc.parse(entry.body);
}
async function getTransformed(inlineConfig: any) {
let config = inlineConfig;
// TODO: load config file
// if (!config) {
// try {
// const importedConfig = await import('./markdoc.config.ts');
// config = importedConfig.default.transform;
// } catch {}
// }
return Markdoc.transform(getParsed(), config);
}
return { getParsed, getTransformed };
},
}; };
export default function markdoc(partialOptions: {} = {}): AstroIntegration { export default function markdoc(partialOptions: {} = {}): AstroIntegration {
@ -51,17 +34,18 @@ export default function markdoc(partialOptions: {} = {}): AstroIntegration {
name: '@astrojs/markdoc', name: '@astrojs/markdoc',
async transform(code, id) { async transform(code, id) {
if (!id.endsWith('.mdoc')) return; if (!id.endsWith('.mdoc')) return;
return `import { Markdoc } from '@astrojs/markdoc';\nexport const body = ${JSON.stringify( return `import { jsx as h } from 'astro/jsx-runtime';\nimport { Markdoc } from '@astrojs/markdoc';\nimport { Renderer } from '@astrojs/markdoc/components';\nexport const body = ${JSON.stringify(
code code
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) { )};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) {
let config = inlineConfig; let config = inlineConfig ?? {};
if (!config) { if (!config) {
try { try {
const importedConfig = await import(${JSON.stringify(markdocConfigUrl.pathname)}); const importedConfig = await import(${JSON.stringify(markdocConfigUrl.pathname)});
console.log({ importedConfig })
config = importedConfig.default.transform; config = importedConfig.default.transform;
} catch {} } catch {}
} }
return Markdoc.transform(getParsed(), config) }`; return Markdoc.transform(getParsed(), config) }\nexport async function Content ({ transformConfig, components }) { return h(Renderer, { content: await getTransformed(transformConfig), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`;
}, },
}, },
], ],

View file

@ -1,6 +1,6 @@
{ {
"extends": "../../../tsconfig.base.json", "extends": "../../../tsconfig.base.json",
"include": ["src"], "include": ["src", "components"],
"compilerOptions": { "compilerOptions": {
"allowJs": true, "allowJs": true,
"module": "ES2020", "module": "ES2020",