@astrojs/markdoc 0.0.0-markdoc-20230214163516

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +620 -0
  3. package/LICENSE +61 -0
  4. package/README.md +160 -0
  5. package/components/RenderNode.astro +30 -0
  6. package/components/Renderer.astro +14 -0
  7. package/components/astroNode.ts +64 -0
  8. package/components/index.ts +2 -0
  9. package/dist/index.d.ts +4 -0
  10. package/dist/index.js +57 -0
  11. package/dist/utils.d.ts +6 -0
  12. package/dist/utils.js +20 -0
  13. package/package.json +54 -0
  14. package/src/index.ts +50 -0
  15. package/src/utils.ts +25 -0
  16. package/template/content-module-types.d.ts +20 -0
  17. package/test/content-collections.test.js +181 -0
  18. package/test/fixtures/content-collections/astro.config.mjs +7 -0
  19. package/test/fixtures/content-collections/node_modules/.bin/astro +17 -0
  20. package/test/fixtures/content-collections/package.json +13 -0
  21. package/test/fixtures/content-collections/src/components/CustomMarquee.astro +1 -0
  22. package/test/fixtures/content-collections/src/content/blog/simple.mdoc +7 -0
  23. package/test/fixtures/content-collections/src/content/blog/with-components.mdoc +17 -0
  24. package/test/fixtures/content-collections/src/content/blog/with-config.mdoc +14 -0
  25. package/test/fixtures/content-collections/src/content/config.ts +12 -0
  26. package/test/fixtures/content-collections/src/pages/collection.json.js +10 -0
  27. package/test/fixtures/content-collections/src/pages/content-simple.astro +18 -0
  28. package/test/fixtures/content-collections/src/pages/content-with-components.astro +50 -0
  29. package/test/fixtures/content-collections/src/pages/content-with-config.astro +38 -0
  30. package/test/fixtures/content-collections/src/pages/entry.json.js +10 -0
  31. package/test/fixtures/content-collections/utils.js +8 -0
  32. package/tsconfig.json +10 -0
@@ -0,0 +1,181 @@
1
+ import { parseHTML } from 'linkedom';
2
+ import { parse as parseDevalue } from 'devalue';
3
+ import { expect } from 'chai';
4
+ import { loadFixture, fixLineEndings } from '../../../astro/test/test-utils.js';
5
+
6
+ function formatPost(post) {
7
+ return {
8
+ ...post,
9
+ body: fixLineEndings(post.body),
10
+ };
11
+ }
12
+
13
+ describe('Markdoc - Content Collections', () => {
14
+ let fixture;
15
+
16
+ before(async () => {
17
+ fixture = await loadFixture({
18
+ root: new URL('./fixtures/content-collections/', import.meta.url),
19
+ });
20
+ });
21
+
22
+ describe('dev', () => {
23
+ let devServer;
24
+
25
+ before(async () => {
26
+ devServer = await fixture.startDevServer();
27
+ });
28
+
29
+ after(async () => {
30
+ await devServer.stop();
31
+ });
32
+
33
+ it('loads entry', async () => {
34
+ const res = await fixture.fetch('/entry.json');
35
+ const post = parseDevalue(await res.text());
36
+ expect(formatPost(post)).to.deep.equal(simplePostEntry);
37
+ });
38
+
39
+ it('loads collection', async () => {
40
+ const res = await fixture.fetch('/collection.json');
41
+ const posts = parseDevalue(await res.text());
42
+ expect(posts).to.not.be.null;
43
+ expect(posts.sort().map((post) => formatPost(post))).to.deep.equal([
44
+ simplePostEntry,
45
+ withComponentsEntry,
46
+ withConfigEntry,
47
+ ]);
48
+ });
49
+
50
+ it('renders content - simple', async () => {
51
+ const res = await fixture.fetch('/content-simple');
52
+ const html = await res.text();
53
+ const { document } = parseHTML(html);
54
+ const h2 = document.querySelector('h2');
55
+ expect(h2.textContent).to.equal('Simple post');
56
+ const p = document.querySelector('p');
57
+ expect(p.textContent).to.equal('This is a simple Markdoc post.');
58
+ });
59
+
60
+ it('renders content - with config', async () => {
61
+ const res = await fixture.fetch('/content-with-config');
62
+ const html = await res.text();
63
+ const { document } = parseHTML(html);
64
+ const h2 = document.querySelector('h2');
65
+ expect(h2.textContent).to.equal('Post with config');
66
+ const marquee = document.querySelector('marquee');
67
+ expect(marquee).to.not.be.null;
68
+ expect(marquee.textContent).to.equal('Im a marquee!');
69
+ });
70
+
71
+ it('renders content - with components', async () => {
72
+ const res = await fixture.fetch('/content-with-components');
73
+ const html = await res.text();
74
+ const { document } = parseHTML(html);
75
+ const h2 = document.querySelector('h2');
76
+ expect(h2.textContent).to.equal('Post with components');
77
+
78
+ // Renders custom shortcode component
79
+ const marquee = document.querySelector('marquee');
80
+ expect(marquee).to.not.be.null;
81
+ expect(marquee.hasAttribute('data-custom-marquee')).to.equal(true);
82
+
83
+ // Renders Astro Code component
84
+ const pre = document.querySelector('pre');
85
+ expect(pre).to.not.be.null;
86
+ expect(pre.className).to.equal('astro-code');
87
+ });
88
+ });
89
+
90
+ describe('build', () => {
91
+ before(async () => {
92
+ await fixture.build();
93
+ });
94
+
95
+ it('loads entry', async () => {
96
+ const res = await fixture.readFile('/entry.json');
97
+ const post = parseDevalue(res);
98
+ expect(formatPost(post)).to.deep.equal(simplePostEntry);
99
+ });
100
+
101
+ it('loads collection', async () => {
102
+ const res = await fixture.readFile('/collection.json');
103
+ const posts = parseDevalue(res);
104
+ expect(posts).to.not.be.null;
105
+ expect(posts.sort().map((post) => formatPost(post))).to.deep.equal([
106
+ simplePostEntry,
107
+ withComponentsEntry,
108
+ withConfigEntry,
109
+ ]);
110
+ });
111
+
112
+ it('renders content - simple', async () => {
113
+ const html = await fixture.readFile('/content-simple/index.html');
114
+ const { document } = parseHTML(html);
115
+ const h2 = document.querySelector('h2');
116
+ expect(h2.textContent).to.equal('Simple post');
117
+ const p = document.querySelector('p');
118
+ expect(p.textContent).to.equal('This is a simple Markdoc post.');
119
+ });
120
+
121
+ it('renders content - with config', async () => {
122
+ const html = await fixture.readFile('/content-with-config/index.html');
123
+ const { document } = parseHTML(html);
124
+ const h2 = document.querySelector('h2');
125
+ expect(h2.textContent).to.equal('Post with config');
126
+ const marquee = document.querySelector('marquee');
127
+ expect(marquee).to.not.be.null;
128
+ expect(marquee.textContent).to.equal('Im a marquee!');
129
+ });
130
+
131
+ it('renders content - with components', async () => {
132
+ const html = await fixture.readFile('/content-with-components/index.html');
133
+ const { document } = parseHTML(html);
134
+ const h2 = document.querySelector('h2');
135
+ expect(h2.textContent).to.equal('Post with components');
136
+
137
+ // Renders custom shortcode component
138
+ const marquee = document.querySelector('marquee');
139
+ expect(marquee).to.not.be.null;
140
+ expect(marquee.hasAttribute('data-custom-marquee')).to.equal(true);
141
+
142
+ // Renders Astro Code component
143
+ const pre = document.querySelector('pre');
144
+ expect(pre).to.not.be.null;
145
+ expect(pre.className).to.equal('astro-code');
146
+ });
147
+ });
148
+ });
149
+
150
+ const simplePostEntry = {
151
+ id: 'simple.mdoc',
152
+ slug: 'simple',
153
+ collection: 'blog',
154
+ data: {
155
+ schemaWorks: true,
156
+ title: 'Simple post',
157
+ },
158
+ body: '\n## Simple post\n\nThis is a simple Markdoc post.\n',
159
+ };
160
+
161
+ const withComponentsEntry = {
162
+ id: 'with-components.mdoc',
163
+ slug: 'with-components',
164
+ collection: 'blog',
165
+ data: {
166
+ schemaWorks: true,
167
+ title: 'Post with components',
168
+ },
169
+ 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',
170
+ };
171
+
172
+ const withConfigEntry = {
173
+ id: 'with-config.mdoc',
174
+ slug: 'with-config',
175
+ collection: 'blog',
176
+ data: {
177
+ schemaWorks: true,
178
+ title: 'Post with config',
179
+ },
180
+ 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',
181
+ };
@@ -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
+ });
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
6
+ esac
7
+
8
+ if [ -z "$NODE_PATH" ]; then
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
+ else
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
+ fi
13
+ if [ -x "$basedir/node" ]; then
14
+ exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
15
+ else
16
+ exec node "$basedir/../../../../../../../astro/astro.js" "$@"
17
+ fi
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@test/markdoc-content-collections",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "@astrojs/markdoc": "workspace:*",
7
+ "@markdoc/markdoc": "^0.2.2",
8
+ "astro": "workspace:*"
9
+ },
10
+ "devDependencies": {
11
+ "shiki": "^0.11.1"
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ <marquee data-custom-marquee {...Astro.props}><slot /></marquee>
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: Simple post
3
+ ---
4
+
5
+ ## Simple post
6
+
7
+ This is a simple Markdoc post.
@@ -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
+ ```
@@ -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 %}
@@ -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 };
@@ -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
+ }
@@ -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,0 +1,50 @@
1
+ ---
2
+ import { getEntryBySlug } from "astro:content";
3
+ import { Code } from 'astro/components';
4
+ import CustomMarquee from '../components/CustomMarquee.astro';
5
+ import Markdoc from '@markdoc/markdoc';
6
+
7
+ const post = await getEntryBySlug('blog', 'with-components');
8
+ const { Content } = await post.render();
9
+ ---
10
+
11
+ <!DOCTYPE html>
12
+ <html lang="en">
13
+ <head>
14
+ <meta charset="UTF-8">
15
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
16
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
17
+ <title>Content - with components</title>
18
+ </head>
19
+ <body>
20
+ <Content
21
+ config={{
22
+ tags: {
23
+ mq: {
24
+ render: 'marquee',
25
+ attributes: {
26
+ direction: {
27
+ type: String,
28
+ default: 'left',
29
+ matches: ['left', 'right', 'up', 'down'],
30
+ errorLevel: 'critical',
31
+ },
32
+ },
33
+ },
34
+ }
35
+ }}
36
+ components={{
37
+ marquee: CustomMarquee,
38
+ pre: {
39
+ component: Code,
40
+ props({ attributes, getTreeNode }) {
41
+ return {
42
+ lang: attributes['data-language'],
43
+ code: Markdoc.renderers.html(getTreeNode().children),
44
+ };
45
+ },
46
+ },
47
+ }}
48
+ />
49
+ </body>
50
+ </html>
@@ -0,0 +1,38 @@
1
+ ---
2
+ import { getEntryBySlug } from "astro:content";
3
+
4
+ const post = await getEntryBySlug('blog', 'with-config');
5
+ const { Content } = await post.render();
6
+ ---
7
+
8
+ <!DOCTYPE html>
9
+ <html lang="en">
10
+ <head>
11
+ <meta charset="UTF-8">
12
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
13
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
14
+ <title>Content - with config</title>
15
+ </head>
16
+ <body>
17
+ <Content
18
+ config={{
19
+ variables: {
20
+ showMarquee: true,
21
+ },
22
+ tags: {
23
+ mq: {
24
+ render: 'marquee',
25
+ attributes: {
26
+ direction: {
27
+ type: String,
28
+ default: 'left',
29
+ matches: ['left', 'right', 'up', 'down'],
30
+ errorLevel: 'critical',
31
+ },
32
+ },
33
+ },
34
+ }
35
+ }}
36
+ />
37
+ </body>
38
+ </html>
@@ -0,0 +1,10 @@
1
+ import { getEntryBySlug } from 'astro:content';
2
+ import { stringify } from 'devalue';
3
+ import { stripRenderFn } from '../../utils.js';
4
+
5
+ export async function get() {
6
+ const post = await getEntryBySlug('blog', 'simple');
7
+ return {
8
+ body: stringify(stripRenderFn(post)),
9
+ };
10
+ }
@@ -0,0 +1,8 @@
1
+ export function stripRenderFn(entryWithRender) {
2
+ const { render, ...entry } = entryWithRender;
3
+ return entry;
4
+ }
5
+
6
+ export function stripAllRenderFn(collection = []) {
7
+ return collection.map(stripRenderFn);
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "include": ["src"],
4
+ "compilerOptions": {
5
+ "allowJs": true,
6
+ "module": "ES2020",
7
+ "outDir": "./dist",
8
+ "target": "ES2020"
9
+ }
10
+ }