@astrojs/markdoc 0.0.0-head-prop-20230323183456

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 (33) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +27 -0
  3. package/LICENSE +61 -0
  4. package/README.md +348 -0
  5. package/components/RenderNode.astro +30 -0
  6. package/components/Renderer.astro +21 -0
  7. package/components/astroNode.ts +51 -0
  8. package/components/index.ts +2 -0
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.js +104 -0
  11. package/dist/utils.d.ts +50 -0
  12. package/dist/utils.js +92 -0
  13. package/package.json +54 -0
  14. package/src/index.ts +122 -0
  15. package/src/utils.ts +147 -0
  16. package/template/content-module-types.d.ts +9 -0
  17. package/test/content-collections.test.js +258 -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/Code.astro +12 -0
  22. package/test/fixtures/content-collections/src/components/CustomMarquee.astro +1 -0
  23. package/test/fixtures/content-collections/src/content/blog/simple.mdoc +7 -0
  24. package/test/fixtures/content-collections/src/content/blog/with-components.mdoc +17 -0
  25. package/test/fixtures/content-collections/src/content/blog/with-config.mdoc +9 -0
  26. package/test/fixtures/content-collections/src/content/config.ts +12 -0
  27. package/test/fixtures/content-collections/src/pages/collection.json.js +10 -0
  28. package/test/fixtures/content-collections/src/pages/content-simple.astro +18 -0
  29. package/test/fixtures/content-collections/src/pages/content-with-components.astro +23 -0
  30. package/test/fixtures/content-collections/src/pages/content-with-config.astro +19 -0
  31. package/test/fixtures/content-collections/src/pages/entry.json.js +10 -0
  32. package/test/fixtures/content-collections/utils.js +8 -0
  33. package/tsconfig.json +10 -0
@@ -0,0 +1,258 @@
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
+ import markdoc from '../dist/index.js';
6
+
7
+ function formatPost(post) {
8
+ return {
9
+ ...post,
10
+ body: fixLineEndings(post.body),
11
+ };
12
+ }
13
+
14
+ const root = new URL('./fixtures/content-collections/', import.meta.url);
15
+
16
+ describe('Markdoc - Content Collections', () => {
17
+ let baseFixture;
18
+
19
+ before(async () => {
20
+ baseFixture = await loadFixture({
21
+ root,
22
+ integrations: [markdoc()],
23
+ });
24
+ });
25
+
26
+ describe('dev', () => {
27
+ let devServer;
28
+
29
+ before(async () => {
30
+ devServer = await baseFixture.startDevServer();
31
+ });
32
+
33
+ after(async () => {
34
+ await devServer.stop();
35
+ });
36
+
37
+ it('loads entry', async () => {
38
+ const res = await baseFixture.fetch('/entry.json');
39
+ const post = parseDevalue(await res.text());
40
+ expect(formatPost(post)).to.deep.equal(simplePostEntry);
41
+ });
42
+
43
+ it('loads collection', async () => {
44
+ const res = await baseFixture.fetch('/collection.json');
45
+ const posts = parseDevalue(await res.text());
46
+ expect(posts).to.not.be.null;
47
+ expect(posts.sort().map((post) => formatPost(post))).to.deep.equal([
48
+ simplePostEntry,
49
+ withComponentsEntry,
50
+ withConfigEntry,
51
+ ]);
52
+ });
53
+
54
+ it('renders content - simple', async () => {
55
+ const res = await baseFixture.fetch('/content-simple');
56
+ const html = await res.text();
57
+ const { document } = parseHTML(html);
58
+ const h2 = document.querySelector('h2');
59
+ expect(h2.textContent).to.equal('Simple post');
60
+ const p = document.querySelector('p');
61
+ expect(p.textContent).to.equal('This is a simple Markdoc post.');
62
+ });
63
+
64
+ it('renders content - with config', async () => {
65
+ const fixture = await getFixtureWithConfig();
66
+ const server = await fixture.startDevServer();
67
+
68
+ const res = await fixture.fetch('/content-with-config');
69
+ const html = await res.text();
70
+ const { document } = parseHTML(html);
71
+ const h2 = document.querySelector('h2');
72
+ expect(h2.textContent).to.equal('Post with config');
73
+ const textContent = html;
74
+
75
+ expect(textContent).to.not.include('Hello');
76
+ expect(textContent).to.include('Hola');
77
+ expect(textContent).to.include(`Konnichiwa`);
78
+
79
+ await server.stop();
80
+ });
81
+
82
+ it('renders content - with components', async () => {
83
+ const fixture = await getFixtureWithComponents();
84
+ const server = await fixture.startDevServer();
85
+
86
+ const res = await fixture.fetch('/content-with-components');
87
+ const html = await res.text();
88
+ const { document } = parseHTML(html);
89
+ const h2 = document.querySelector('h2');
90
+ expect(h2.textContent).to.equal('Post with components');
91
+
92
+ // Renders custom shortcode component
93
+ const marquee = document.querySelector('marquee');
94
+ expect(marquee).to.not.be.null;
95
+ expect(marquee.hasAttribute('data-custom-marquee')).to.equal(true);
96
+
97
+ // Renders Astro Code component
98
+ const pre = document.querySelector('pre');
99
+ expect(pre).to.not.be.null;
100
+ expect(pre.className).to.equal('astro-code');
101
+
102
+ await server.stop();
103
+ });
104
+ });
105
+
106
+ describe('build', () => {
107
+ before(async () => {
108
+ await baseFixture.build();
109
+ });
110
+
111
+ it('loads entry', async () => {
112
+ const res = await baseFixture.readFile('/entry.json');
113
+ const post = parseDevalue(res);
114
+ expect(formatPost(post)).to.deep.equal(simplePostEntry);
115
+ });
116
+
117
+ it('loads collection', async () => {
118
+ const res = await baseFixture.readFile('/collection.json');
119
+ const posts = parseDevalue(res);
120
+ expect(posts).to.not.be.null;
121
+ expect(posts.sort().map((post) => formatPost(post))).to.deep.equal([
122
+ simplePostEntry,
123
+ withComponentsEntry,
124
+ withConfigEntry,
125
+ ]);
126
+ });
127
+
128
+ it('renders content - simple', async () => {
129
+ const html = await baseFixture.readFile('/content-simple/index.html');
130
+ const { document } = parseHTML(html);
131
+ const h2 = document.querySelector('h2');
132
+ expect(h2.textContent).to.equal('Simple post');
133
+ const p = document.querySelector('p');
134
+ expect(p.textContent).to.equal('This is a simple Markdoc post.');
135
+ });
136
+
137
+ it('renders content - with config', async () => {
138
+ const fixture = await getFixtureWithConfig();
139
+ await fixture.build();
140
+
141
+ const html = await fixture.readFile('/content-with-config/index.html');
142
+ const { document } = parseHTML(html);
143
+ const h2 = document.querySelector('h2');
144
+ expect(h2.textContent).to.equal('Post with config');
145
+ const textContent = html;
146
+
147
+ expect(textContent).to.not.include('Hello');
148
+ expect(textContent).to.include('Hola');
149
+ expect(textContent).to.include(`Konnichiwa`);
150
+ });
151
+
152
+ it('renders content - with components', async () => {
153
+ const fixture = await getFixtureWithComponents();
154
+ await fixture.build();
155
+
156
+ const html = await fixture.readFile('/content-with-components/index.html');
157
+ const { document } = parseHTML(html);
158
+ const h2 = document.querySelector('h2');
159
+ expect(h2.textContent).to.equal('Post with components');
160
+
161
+ // Renders custom shortcode component
162
+ const marquee = document.querySelector('marquee');
163
+ expect(marquee).to.not.be.null;
164
+ expect(marquee.hasAttribute('data-custom-marquee')).to.equal(true);
165
+
166
+ // Renders Astro Code component
167
+ const pre = document.querySelector('pre');
168
+ expect(pre).to.not.be.null;
169
+ expect(pre.className).to.equal('astro-code');
170
+ });
171
+ });
172
+ });
173
+
174
+ function getFixtureWithConfig() {
175
+ return loadFixture({
176
+ root,
177
+ integrations: [
178
+ markdoc({
179
+ variables: {
180
+ countries: ['ES', 'JP'],
181
+ },
182
+ functions: {
183
+ includes: {
184
+ transform(parameters) {
185
+ const [array, value] = Object.values(parameters);
186
+ return Array.isArray(array) ? array.includes(value) : false;
187
+ },
188
+ },
189
+ },
190
+ }),
191
+ ],
192
+ });
193
+ }
194
+
195
+ function getFixtureWithComponents() {
196
+ return loadFixture({
197
+ root,
198
+ integrations: [
199
+ markdoc({
200
+ nodes: {
201
+ fence: {
202
+ render: 'Code',
203
+ attributes: {
204
+ language: { type: String },
205
+ content: { type: String },
206
+ },
207
+ },
208
+ },
209
+ tags: {
210
+ mq: {
211
+ render: 'CustomMarquee',
212
+ attributes: {
213
+ direction: {
214
+ type: String,
215
+ default: 'left',
216
+ matches: ['left', 'right', 'up', 'down'],
217
+ errorLevel: 'critical',
218
+ },
219
+ },
220
+ },
221
+ },
222
+ }),
223
+ ],
224
+ });
225
+ }
226
+
227
+ const simplePostEntry = {
228
+ id: 'simple.mdoc',
229
+ slug: 'simple',
230
+ collection: 'blog',
231
+ data: {
232
+ schemaWorks: true,
233
+ title: 'Simple post',
234
+ },
235
+ body: '\n## Simple post\n\nThis is a simple Markdoc post.\n',
236
+ };
237
+
238
+ const withComponentsEntry = {
239
+ id: 'with-components.mdoc',
240
+ slug: 'with-components',
241
+ collection: 'blog',
242
+ data: {
243
+ schemaWorks: true,
244
+ title: 'Post with components',
245
+ },
246
+ 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',
247
+ };
248
+
249
+ const withConfigEntry = {
250
+ id: 'with-config.mdoc',
251
+ slug: 'with-config',
252
+ collection: 'blog',
253
+ data: {
254
+ schemaWorks: true,
255
+ title: 'Post with config',
256
+ },
257
+ body: '\n## Post with config\n\n{% if includes($countries, "EN") %} Hello {% /if %}\n{% if includes($countries, "ES") %} Hola {% /if %}\n{% if includes($countries, "JP") %} Konnichiwa {% /if %}\n',
258
+ };
@@ -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,12 @@
1
+ ---
2
+ import { Code } from 'astro/components';
3
+
4
+ type Props = {
5
+ content: string;
6
+ language: string;
7
+ }
8
+
9
+ const { content, language } = Astro.props as Props;
10
+ ---
11
+
12
+ <Code lang={language} code={content} />
@@ -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,9 @@
1
+ ---
2
+ title: Post with config
3
+ ---
4
+
5
+ ## Post with config
6
+
7
+ {% if includes($countries, "EN") %} Hello {% /if %}
8
+ {% if includes($countries, "ES") %} Hola {% /if %}
9
+ {% if includes($countries, "JP") %} Konnichiwa {% /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,23 @@
1
+ ---
2
+ import { getEntryBySlug } from "astro:content";
3
+ import Code from '../components/Code.astro';
4
+ import CustomMarquee from '../components/CustomMarquee.astro';
5
+
6
+ const post = await getEntryBySlug('blog', 'with-components');
7
+ const { Content } = await post.render();
8
+ ---
9
+
10
+ <!DOCTYPE html>
11
+ <html lang="en">
12
+ <head>
13
+ <meta charset="UTF-8">
14
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
15
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
16
+ <title>Content - with components</title>
17
+ </head>
18
+ <body>
19
+ <Content
20
+ components={{ CustomMarquee, Code }}
21
+ />
22
+ </body>
23
+ </html>
@@ -0,0 +1,19 @@
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
+ </body>
19
+ </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
+ }