test: entry and collections parse

This commit is contained in:
bholmesdev 2023-02-09 13:25:18 -05:00
parent 7aa04e20bd
commit fa9bd3bcec
14 changed files with 265 additions and 1 deletions

View file

@ -45,7 +45,7 @@ if (!config) {
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;`;
return Markdoc.transform(getParsed(), config) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: await getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`;
},
},
],

View file

@ -0,0 +1,72 @@
import { parseHTML } from 'linkedom';
import { parse as parseDevalue } from 'devalue';
import { expect } from 'chai';
import { loadFixture } from '../../../astro/test/test-utils.js';
describe('Markdoc - Content Collections', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/content-collections/', import.meta.url),
});
});
describe('dev', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('loads entry', async () => {
const res = await fixture.fetch('/entry.json');
const post = parseDevalue(await res.text());
expect(post).to.deep.equal(simplePostEntry);
});
it('loads collection', async () => {
const res = await fixture.fetch('/collection.json');
const posts = parseDevalue(await res.text());
expect(posts).to.not.be.null;
expect(posts.sort()).to.deep.equal([
simplePostEntry,
{
id: 'with-components.mdoc',
slug: 'with-components',
collection: 'blog',
data: {
schemaWorks: true,
title: 'Post with components',
},
body: '\n## Post with components\n\nThis uses a custom marquee component with a shortcode:\n\n{% mq direction="right" %}\nI\'m a marquee too!\n{% /mq %}\n\nAnd a code component for code blocks:\n\n```js\nconst isRenderedWithShiki = true;\n```\n',
},
{
id: 'with-config.mdoc',
slug: 'with-config',
collection: 'blog',
data: {
schemaWorks: true,
title: 'Post with config',
},
body: '\n## Post with config\n\nThis uses a shortcode to render a marquee element,\nwith a variable to show and hide:\n\n{% if $showMarquee %}\n{% mq direction="down" %}\nIm a marquee!\n{% /mq %}\n{% /if %}\n',
},
]);
});
});
});
const simplePostEntry = {
id: 'simple.mdoc',
slug: 'simple',
collection: 'blog',
data: {
schemaWorks: true,
title: 'Simple post',
},
body: '\n## Simple post\n\nThis is a simple Markdoc post.\n',
};

View file

@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';
// https://astro.build/config
export default defineConfig({
integrations: [markdoc()],
});

View file

@ -0,0 +1 @@
<marquee data-custom-marquee {...Astro.props}><slot /></marquee>

View file

@ -0,0 +1,7 @@
---
title: Simple post
---
## Simple post
This is a simple Markdoc post.

View file

@ -0,0 +1,17 @@
---
title: Post with components
---
## Post with components
This uses a custom marquee component with a shortcode:
{% mq direction="right" %}
I'm a marquee too!
{% /mq %}
And a code component for code blocks:
```js
const isRenderedWithShiki = true;
```

View file

@ -0,0 +1,14 @@
---
title: Post with config
---
## Post with config
This uses a shortcode to render a marquee element,
with a variable to show and hide:
{% if $showMarquee %}
{% mq direction="down" %}
Im a marquee!
{% /mq %}
{% /if %}

View file

@ -0,0 +1,12 @@
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
schema: z.object({
title: z.string(),
}).transform(data => ({
...data,
schemaWorks: true,
}))
});
export const collections = { blog };

View file

@ -0,0 +1,10 @@
import { getCollection } from 'astro:content';
import { stringify } from 'devalue';
import { stripAllRenderFn } from '../../utils.js';
export async function get() {
const posts = await getCollection('blog');
return {
body: stringify(stripAllRenderFn(posts))
};
}

View file

@ -0,0 +1,18 @@
---
import { getEntryBySlug } from "astro:content";
const post = await getEntryBySlug('blog', 'simple');
const { Content } = await post.render();
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Content - Simple</title>
</head>
<body>
<Content />
</body>
</html>

View file

@ -0,0 +1,50 @@
---
import { getEntryBySlug } from "astro:content";
import { Code } from 'astro/components';
import CustomMarquee from '../components/CustomMarquee.astro';
import Markdoc from '@markdoc/markdoc';
const post = await getEntryBySlug('blog', 'with-components');
const { Content } = await post.render();
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Content - with components</title>
</head>
<body>
<Content
config={{
tags: {
mq: {
render: 'marquee',
attributes: {
direction: {
type: String,
default: 'left',
matches: ['left', 'right', 'up', 'down'],
errorLevel: 'critical',
},
},
},
}
}}
components={{
marquee: CustomMarquee,
pre: {
component: Code,
props({ attributes, getTreeNode }) {
return {
lang: attributes['data-language'],
code: Markdoc.renderers.html(getTreeNode().children),
};
},
},
}}
/>
</body>
</html>

View file

@ -0,0 +1,38 @@
---
import { getEntryBySlug } from "astro:content";
const post = await getEntryBySlug('blog', 'with-config');
const { Content } = await post.render();
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Content - with config</title>
</head>
<body>
<Content
config={{
variables: {
showMarquee: true,
},
tags: {
mq: {
render: 'marquee',
attributes: {
direction: {
type: String,
default: 'left',
matches: ['left', 'right', 'up', 'down'],
errorLevel: 'critical',
},
},
},
}
}}
/>
</body>
</html>

View file

@ -0,0 +1,10 @@
import { getEntryBySlug } from 'astro:content';
import { stringify } from 'devalue';
import { stripRenderFn } from '../../utils.js';
export async function get() {
const post = await getEntryBySlug('blog', 'simple');
return {
body: stringify(stripRenderFn(post)),
};
}

View file

@ -0,0 +1,8 @@
export function stripRenderFn(entryWithRender) {
const { render, ...entry } = entryWithRender;
return entry;
}
export function stripAllRenderFn(collection = []) {
return collection.map(stripRenderFn);
}