Skip to content

Commit dfdb35f

Browse files
committed
test: entry and collections parse
1 parent eaf4994 commit dfdb35f

14 files changed

Lines changed: 265 additions & 1 deletion

File tree

packages/integrations/markdoc/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if (!config) {
4545
config = {};
4646
}
4747
}
48-
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;`;
48+
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;`;
4949
},
5050
},
5151
],
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { parseHTML } from 'linkedom';
2+
import { parse as parseDevalue } from 'devalue';
3+
import { expect } from 'chai';
4+
import { loadFixture } from '../../../astro/test/test-utils.js';
5+
6+
describe('Markdoc - Content Collections', () => {
7+
let fixture;
8+
9+
before(async () => {
10+
fixture = await loadFixture({
11+
root: new URL('./fixtures/content-collections/', import.meta.url),
12+
});
13+
});
14+
15+
describe('dev', () => {
16+
let devServer;
17+
18+
before(async () => {
19+
devServer = await fixture.startDevServer();
20+
});
21+
22+
after(async () => {
23+
await devServer.stop();
24+
});
25+
26+
it('loads entry', async () => {
27+
const res = await fixture.fetch('/entry.json');
28+
const post = parseDevalue(await res.text());
29+
expect(post).to.deep.equal(simplePostEntry);
30+
});
31+
32+
it('loads collection', async () => {
33+
const res = await fixture.fetch('/collection.json');
34+
const posts = parseDevalue(await res.text());
35+
expect(posts).to.not.be.null;
36+
expect(posts.sort()).to.deep.equal([
37+
simplePostEntry,
38+
{
39+
id: 'with-components.mdoc',
40+
slug: 'with-components',
41+
collection: 'blog',
42+
data: {
43+
schemaWorks: true,
44+
title: 'Post with components',
45+
},
46+
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',
47+
},
48+
{
49+
id: 'with-config.mdoc',
50+
slug: 'with-config',
51+
collection: 'blog',
52+
data: {
53+
schemaWorks: true,
54+
title: 'Post with config',
55+
},
56+
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',
57+
},
58+
]);
59+
});
60+
});
61+
});
62+
63+
const simplePostEntry = {
64+
id: 'simple.mdoc',
65+
slug: 'simple',
66+
collection: 'blog',
67+
data: {
68+
schemaWorks: true,
69+
title: 'Simple post',
70+
},
71+
body: '\n## Simple post\n\nThis is a simple Markdoc post.\n',
72+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'astro/config';
2+
import markdoc from '@astrojs/markdoc';
3+
4+
// https://astro.build/config
5+
export default defineConfig({
6+
integrations: [markdoc()],
7+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<marquee data-custom-marquee {...Astro.props}><slot /></marquee>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Simple post
3+
---
4+
5+
## Simple post
6+
7+
This is a simple Markdoc post.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Post with components
3+
---
4+
5+
## Post with components
6+
7+
This uses a custom marquee component with a shortcode:
8+
9+
{% mq direction="right" %}
10+
I'm a marquee too!
11+
{% /mq %}
12+
13+
And a code component for code blocks:
14+
15+
```js
16+
const isRenderedWithShiki = true;
17+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Post with config
3+
---
4+
5+
## Post with config
6+
7+
This uses a shortcode to render a marquee element,
8+
with a variable to show and hide:
9+
10+
{% if $showMarquee %}
11+
{% mq direction="down" %}
12+
Im a marquee!
13+
{% /mq %}
14+
{% /if %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineCollection, z } from 'astro:content';
2+
3+
const blog = defineCollection({
4+
schema: z.object({
5+
title: z.string(),
6+
}).transform(data => ({
7+
...data,
8+
schemaWorks: true,
9+
}))
10+
});
11+
12+
export const collections = { blog };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getCollection } from 'astro:content';
2+
import { stringify } from 'devalue';
3+
import { stripAllRenderFn } from '../../utils.js';
4+
5+
export async function get() {
6+
const posts = await getCollection('blog');
7+
return {
8+
body: stringify(stripAllRenderFn(posts))
9+
};
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
import { getEntryBySlug } from "astro:content";
3+
const post = await getEntryBySlug('blog', 'simple');
4+
const { Content } = await post.render();
5+
---
6+
7+
<!DOCTYPE html>
8+
<html lang="en">
9+
<head>
10+
<meta charset="UTF-8">
11+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
12+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
13+
<title>Content - Simple</title>
14+
</head>
15+
<body>
16+
<Content />
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)