Skip to content

Commit

Permalink
test: entry and collections parse
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent eaf4994 commit dfdb35f
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/integrations/markdoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;`;
},
},
],
Expand Down
72 changes: 72 additions & 0 deletions packages/integrations/markdoc/test/content-collections.test.js
Original file line number Diff line number Diff line change
@@ -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',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';

// https://astro.build/config
export default defineConfig({
integrations: [markdoc()],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<marquee data-custom-marquee {...Astro.props}><slot /></marquee>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Simple post
---

## Simple post

This is a simple Markdoc post.
Original file line number Diff line number Diff line change
@@ -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;
```
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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))
};
}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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)),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function stripRenderFn(entryWithRender) {
const { render, ...entry } = entryWithRender;
return entry;
}

export function stripAllRenderFn(collection = []) {
return collection.map(stripRenderFn);
}

0 comments on commit dfdb35f

Please sign in to comment.