Skip to content

Commit

Permalink
Merge branch 'main' into bump-svelte2tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
wackbyte authored Jun 6, 2023
2 parents 5a1f6bb + 8b5c311 commit 2bedbf0
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 191 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-dolls-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdoc': patch
---

Fix Markdoc type errors for `render` and `defineMarkdocConfig()` when using a TypeScript Markdoc config file.
6 changes: 6 additions & 0 deletions .changeset/nice-falcons-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/mdx': patch
---

Fix [Object AsyncGenerator] appearing in markup for Markdoc documents
5 changes: 5 additions & 0 deletions .changeset/shaggy-deers-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdoc': patch
---

Fix missing styles and scripts for components when using `document: { render: null }` in the Markdoc config.
3 changes: 0 additions & 3 deletions packages/astro/src/runtime/server/render/astro/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export class AstroComponentInstance {
value = await value;
}
if (isHeadAndContent(value)) {
if (this.result.extraHead.length === 0 && value.head) {
yield renderChild(value.head);
}
yield* value.content;
} else {
yield* renderChild(value);
Expand Down
8 changes: 3 additions & 5 deletions packages/integrations/markdoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ import Heading from './src/components/Heading.astro';
export default defineMarkdocConfig({
nodes: {
heading: {
...nodes.heading, // Preserve default anchor link generation
render: Heading,
// Preserve default anchor link generation
...nodes.heading,
},
},
})
Expand Down Expand Up @@ -225,8 +224,8 @@ import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
nodes: {
document: {
render: null, // default 'article'
...nodes.document, // Apply defaults for other options
render: null, // default 'article'
},
},
})
Expand All @@ -246,9 +245,8 @@ import Quote from './src/components/Quote.astro';
export default defineMarkdocConfig({
nodes: {
blockquote: {
...nodes.blockquote, // Apply Markdoc's defaults for other options
render: Quote,
// Apply Markdoc's defaults for other options
...nodes.blockquote,
},
},
})
Expand Down
8 changes: 7 additions & 1 deletion packages/integrations/markdoc/components/Renderer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ const ast = Markdoc.Ast.fromJSON(stringifiedAst);
const content = Markdoc.transform(ast, config);
---

<ComponentNode treeNode={await createTreeNode(content)} />
{
Array.isArray(content) ? (
content.map(async (c) => <ComponentNode treeNode={await createTreeNode(c)} />)
) : (
<ComponentNode treeNode={await createTreeNode(content)} />
)
}
14 changes: 3 additions & 11 deletions packages/integrations/markdoc/components/TreeNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { AstroInstance } from 'astro';
import { Fragment } from 'astro/jsx-runtime';
import type { RenderableTreeNode } from '@markdoc/markdoc';
import Markdoc from '@markdoc/markdoc';
import {
Expand Down Expand Up @@ -106,18 +105,11 @@ export const ComponentNode = createComponent({
propagation: 'self',
});

export async function createTreeNode(node: RenderableTreeNode | RenderableTreeNode[]): TreeNode {
export async function createTreeNode(node: RenderableTreeNode): Promise<TreeNode> {
if (isHTMLString(node)) {
return { type: 'text', content: node as HTMLString };
} else if (typeof node === 'string' || typeof node === 'number') {
return { type: 'text', content: String(node) };
} else if (Array.isArray(node)) {
return {
type: 'component',
component: Fragment,
props: {},
children: await Promise.all(node.map((child) => createTreeNode(child))),
};
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
return { type: 'text', content: '' };
}
Expand All @@ -136,7 +128,7 @@ export async function createTreeNode(node: RenderableTreeNode | RenderableTreeNo
};
} else if (isPropagatedAssetsModule(node.name)) {
const { collectedStyles, collectedLinks, collectedScripts } = node.name;
const component = (await node.name.getMod())?.default ?? Fragment;
const component = (await node.name.getMod()).default;
const props = node.attributes;

return {
Expand All @@ -160,7 +152,7 @@ export async function createTreeNode(node: RenderableTreeNode | RenderableTreeNo

type PropagatedAssetsModule = {
__astroPropagation: true;
getMod: () => Promise<AstroInstance['default']>;
getMod: () => Promise<AstroInstance>;
collectedStyles: string[];
collectedLinks: string[];
collectedScripts: string[];
Expand Down
28 changes: 25 additions & 3 deletions packages/integrations/markdoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,37 @@
"bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://docs.astro.build/en/guides/integrations-guide/markdoc/",
"exports": {
"./prism": "./dist/extensions/prism.js",
"./shiki": "./dist/extensions/shiki.js",
"./prism": {
"types": "./dist/extensions/prism.d.ts",
"node": "./dist/extensions/prism.js"
},
"./shiki": {
"types": "./dist/extensions/shiki.d.ts",
"node": "./dist/extensions/shiki.js"
},
"./config": {
"types": "./dist/config.d.ts",
"node": "./dist/config.js"
},
".": "./dist/index.js",
"./components": "./components/index.ts",
"./runtime": "./dist/runtime.js",
"./config": "./dist/config.js",
"./experimental-assets-config": "./dist/experimental-assets-config.js",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"config": [
"./dist/config.d.ts"
],
"prism": [
"./dist/extensions/prism.d.ts"
],
"shiki": [
"./dist/extensions/shiki.d.ts"
]
}
},
"files": [
"components",
"dist",
Expand Down
26 changes: 20 additions & 6 deletions packages/integrations/markdoc/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import type { ConfigType as MarkdocConfig } from '@markdoc/markdoc';
import type {
Config,
ConfigType as MarkdocConfig,
MaybePromise,
NodeType,
Schema,
} from '@markdoc/markdoc';
import _Markdoc from '@markdoc/markdoc';
import type { AstroInstance } from 'astro';
import { heading } from './heading-ids.js';

export type AstroMarkdocConfig<C extends Record<string, any> = Record<string, any>> =
MarkdocConfig & {
ctx?: C;
extends?: ResolvedAstroMarkdocConfig[];
};
type Render = AstroInstance['default'] | string;

export type AstroMarkdocConfig<C extends Record<string, any> = Record<string, any>> = Omit<
MarkdocConfig,
'tags' | 'nodes'
> &
Partial<{
tags: Record<string, Schema<Config, Render>>;
nodes: Partial<Record<NodeType, Schema<Config, Render>>>;
ctx: C;
extends: MaybePromise<ResolvedAstroMarkdocConfig>[];
}>;

export type ResolvedAstroMarkdocConfig = Omit<AstroMarkdocConfig, 'extends'>;

Expand Down
13 changes: 8 additions & 5 deletions packages/integrations/markdoc/src/heading-ids.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Markdoc, { type RenderableTreeNode, type Schema } from '@markdoc/markdoc';
import Markdoc, {
type Config as MarkdocConfig,
type RenderableTreeNode,
type Schema,
} from '@markdoc/markdoc';
import Slugger from 'github-slugger';
import type { AstroMarkdocConfig } from './config.js';
import { getTextContent } from './runtime.js';
import { MarkdocError } from './utils.js';

Expand All @@ -19,9 +22,9 @@ function getSlug(
return slug;
}

type HeadingIdConfig = AstroMarkdocConfig<{
headingSlugger: Slugger;
}>;
type HeadingIdConfig = MarkdocConfig & {
ctx: { headingSlugger: Slugger };
};

/*
Expose standalone node for users to import in their config.
Expand Down
8 changes: 6 additions & 2 deletions packages/integrations/markdoc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import type { Node } from '@markdoc/markdoc';
import type { Config as MarkdocConfig, Node } from '@markdoc/markdoc';
import Markdoc from '@markdoc/markdoc';
import type { AstroConfig, AstroIntegration, ContentEntryType, HookParameters } from 'astro';
import fs from 'node:fs';
Expand Down Expand Up @@ -85,7 +85,11 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration

const filePath = fileURLToPath(fileUrl);

const validationErrors = Markdoc.validate(ast, markdocConfig).filter((e) => {
const validationErrors = Markdoc.validate(
ast,
/* Raised generics issue with Markdoc core https://github.com/markdoc/markdoc/discussions/400 */
markdocConfig as MarkdocConfig
).filter((e) => {
return (
// Ignore `variable-undefined` errors.
// Variables can be configured at runtime,
Expand Down
8 changes: 4 additions & 4 deletions packages/integrations/markdoc/src/load-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Config as MarkdocConfig } from '@markdoc/markdoc';
import type { AstroConfig } from 'astro';
import { build as esbuild } from 'esbuild';
import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import type { AstroMarkdocConfig } from './config.js';

const SUPPORTED_MARKDOC_CONFIG_FILES = [
'markdoc.config.js',
Expand All @@ -12,7 +12,7 @@ const SUPPORTED_MARKDOC_CONFIG_FILES = [
];

export type MarkdocConfigResult = {
config: MarkdocConfig;
config: AstroMarkdocConfig;
fileUrl: URL;
};

Expand All @@ -33,7 +33,7 @@ export async function loadMarkdocConfig(
markdocConfigUrl,
astroConfig,
});
const config: MarkdocConfig = await loadConfigFromBundledFile(astroConfig.root, code);
const config: AstroMarkdocConfig = await loadConfigFromBundledFile(astroConfig.root, code);

return {
config,
Expand Down Expand Up @@ -93,7 +93,7 @@ async function bundleConfigFile({
* with ESM only
* @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L1074
*/
async function loadConfigFromBundledFile(root: URL, code: string): Promise<MarkdocConfig> {
async function loadConfigFromBundledFile(root: URL, code: string): Promise<AstroMarkdocConfig> {
// Write it to disk, load it with native Node ESM, then delete the file.
const tmpFileUrl = new URL(`markdoc.config.timestamp-${Date.now()}.mjs`, root);
fs.writeFileSync(tmpFileUrl, code);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';

export default defineMarkdocConfig({
nodes: {
document: {
...nodes.document,
render: null,

// Defaults from `Markdoc.nodes.document`
children: [
'heading',
'paragraph',
'image',
'table',
'tag',
'fence',
'blockquote',
'comment',
'list',
'hr',
],
attributes: {
frontmatter: { render: false },
},
}
}
})
49 changes: 0 additions & 49 deletions packages/integrations/mdx/test/astro-content-css.test.js

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2bedbf0

Please sign in to comment.