@astrojs/markdoc 0.4.0 → 0.4.2

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.
package/README.md CHANGED
@@ -42,9 +42,8 @@ npm install @astrojs/markdoc
42
42
 
43
43
  Then, apply this integration to your `astro.config.*` file using the `integrations` property:
44
44
 
45
- **`astro.config.mjs`**
46
-
47
- ```js ins={2} "markdoc()"
45
+ ```js ins={3} "markdoc()"
46
+ // astro.config.mjs
48
47
  import { defineConfig } from 'astro/config';
49
48
  import markdoc from '@astrojs/markdoc';
50
49
 
@@ -106,13 +105,12 @@ This example renders an `Aside` component, and allows a `type` prop to be passed
106
105
 
107
106
  ```js
108
107
  // markdoc.config.mjs
109
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
110
- import Aside from './src/components/Aside.astro';
108
+ import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
111
109
 
112
110
  export default defineMarkdocConfig({
113
111
  tags: {
114
112
  aside: {
115
- render: Aside,
113
+ render: component('./src/components/Aside.astro'),
116
114
  attributes: {
117
115
  // Markdoc requires type defs for each attribute.
118
116
  // These should mirror the `Props` type of the component
@@ -138,6 +136,31 @@ Use tags like this fancy "aside" to add some _flair_ to your docs.
138
136
  {% /aside %}
139
137
  ```
140
138
 
139
+ ### Use Astro components from npm packages and TypeScript files
140
+
141
+ You may need to use Astro components exposed as named exports from TypeScript or JavaScript files. This is common when using npm packages and design systems.
142
+
143
+ You can pass the import name as the second argument to the `component()` function:
144
+
145
+ ```js
146
+ // markdoc.config.mjs
147
+ import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
148
+
149
+ export default defineMarkdocConfig({
150
+ tags: {
151
+ tabs: {
152
+ render: component('@astrojs/starlight/components', 'Tabs'),
153
+ },
154
+ },
155
+ });
156
+ ```
157
+
158
+ This generates the following import statement internally:
159
+
160
+ ```ts
161
+ import { Tabs } from '@astrojs/starlight/components';
162
+ ```
163
+
141
164
  ### Custom headings
142
165
 
143
166
  `@astrojs/markdoc` automatically adds anchor links to your headings, and [generates a list of `headings` via the content collections API](https://docs.astro.build/en/guides/content-collections/#rendering-content-to-html). To further customize how headings are rendered, you can apply an Astro component [as a Markdoc node][markdoc-nodes].
@@ -146,14 +169,13 @@ This example renders a `Heading.astro` component using the `render` property:
146
169
 
147
170
  ```js
148
171
  // markdoc.config.mjs
149
- import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
150
- import Heading from './src/components/Heading.astro';
172
+ import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
151
173
 
152
174
  export default defineMarkdocConfig({
153
175
  nodes: {
154
176
  heading: {
155
177
  ...nodes.heading, // Preserve default anchor link generation
156
- render: Heading,
178
+ render: component('./src/components/Heading.astro'),
157
179
  },
158
180
  },
159
181
  });
@@ -240,14 +262,13 @@ This example renders blockquotes with a custom `Quote.astro` component:
240
262
 
241
263
  ```js
242
264
  // markdoc.config.mjs
243
- import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
244
- import Quote from './src/components/Quote.astro';
265
+ import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
245
266
 
246
267
  export default defineMarkdocConfig({
247
268
  nodes: {
248
269
  blockquote: {
249
270
  ...nodes.blockquote, // Apply Markdoc's defaults for other options
250
- render: Quote,
271
+ render: component('./src/components/Quote.astro'),
251
272
  },
252
273
  },
253
274
  });
@@ -274,13 +295,12 @@ This Astro component can now be passed to the `render` prop for any [tag][markdo
274
295
 
275
296
  ```js
276
297
  // markdoc.config.mjs
277
- import { defineMarkdocConfig } from '@astrojs/markdoc/config';
278
- import ClientAside from './src/components/ClientAside.astro';
298
+ import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
279
299
 
280
300
  export default defineMarkdocConfig({
281
301
  tags: {
282
302
  aside: {
283
- render: ClientAside,
303
+ render: component('./src/components/ClientAside.astro'),
284
304
  attributes: {
285
305
  type: { type: String },
286
306
  },
@@ -89,7 +89,10 @@ export const ComponentNode = createComponent({
89
89
  );
90
90
 
91
91
  // Let the runtime know that this component is being used.
92
- result.propagators.set(
92
+ // `result.propagators` has been moved to `result._metadata.propagators`
93
+ // TODO: remove this fallback in the next markdoc integration major
94
+ const propagators = result._metadata.propagators || result.propagators;
95
+ propagators.set(
93
96
  {},
94
97
  {
95
98
  init() {
@@ -161,17 +161,20 @@ function shouldOptimizeImage(src) {
161
161
  function getStringifiedImports(componentConfigMap, componentNamePrefix, root) {
162
162
  let stringifiedComponentImports = "";
163
163
  for (const [key, config] of Object.entries(componentConfigMap)) {
164
- const importName = config.namedExport ? `{ ${config.namedExport} as ${componentNamePrefix + key} }` : componentNamePrefix + key;
164
+ const importName = config.namedExport ? `{ ${config.namedExport} as ${componentNamePrefix + toImportName(key)} }` : componentNamePrefix + toImportName(key);
165
165
  const resolvedPath = config.type === "local" ? new URL(config.path, root).pathname : config.path;
166
166
  stringifiedComponentImports += `import ${importName} from ${JSON.stringify(resolvedPath)};
167
167
  `;
168
168
  }
169
169
  return stringifiedComponentImports;
170
170
  }
171
+ function toImportName(unsafeName) {
172
+ return unsafeName.replace("-", "_");
173
+ }
171
174
  function getStringifiedMap(componentConfigMap, componentNamePrefix) {
172
175
  let stringifiedComponentMap = "{";
173
176
  for (const key in componentConfigMap) {
174
- stringifiedComponentMap += `${key}: ${componentNamePrefix + key},
177
+ stringifiedComponentMap += `${JSON.stringify(key)}: ${componentNamePrefix + toImportName(key)},
175
178
  `;
176
179
  }
177
180
  stringifiedComponentMap += "}";
package/dist/index.js CHANGED
@@ -1,6 +1,4 @@
1
1
  import { bold, red } from "kleur/colors";
2
- import { fileURLToPath } from "node:url";
3
- import { normalizePath } from "vite";
4
2
  import { getContentEntryType } from "./content-entry-type.js";
5
3
  import {
6
4
  loadMarkdocConfig,
@@ -16,7 +14,6 @@ function markdocIntegration(legacyConfig) {
16
14
  process.exit(0);
17
15
  }
18
16
  let markdocConfigResult;
19
- let markdocConfigResultId = "";
20
17
  let astroConfig;
21
18
  return {
22
19
  name: "@astrojs/markdoc",
@@ -25,9 +22,6 @@ function markdocIntegration(legacyConfig) {
25
22
  const { updateConfig, addContentEntryType } = params;
26
23
  astroConfig = params.config;
27
24
  markdocConfigResult = await loadMarkdocConfig(astroConfig);
28
- if (markdocConfigResult) {
29
- markdocConfigResultId = normalizePath(fileURLToPath(markdocConfigResult.fileUrl));
30
- }
31
25
  addContentEntryType(await getContentEntryType({ markdocConfigResult, astroConfig }));
32
26
  updateConfig({
33
27
  vite: {
@@ -19,7 +19,7 @@ async function loadMarkdocConfig(astroConfig) {
19
19
  }
20
20
  if (!markdocConfigUrl)
21
21
  return;
22
- const { code, dependencies } = await bundleConfigFile({
22
+ const { code } = await bundleConfigFile({
23
23
  markdocConfigUrl,
24
24
  astroConfig
25
25
  });
@@ -53,7 +53,7 @@ async function bundleConfigFile({
53
53
  build.onResolve({ filter: /.*\.astro$/ }, () => {
54
54
  markdocError = new MarkdocError({
55
55
  message: "`.astro` files are no longer supported in the Markdoc config.",
56
- hint: "Use the `component()` utility to specify a component path instead."
56
+ hint: "Use the `component()` utility to specify a component path instead. See https://docs.astro.build/en/guides/integrations-guide/markdoc/"
57
57
  });
58
58
  return {
59
59
  // Stub with an unused default export.
package/dist/utils.js CHANGED
@@ -2,7 +2,7 @@ class MarkdocError extends Error {
2
2
  constructor(props, ...params) {
3
3
  super(...params);
4
4
  this.type = "MarkdocError";
5
- const { name, title = "MarkdocError", message, stack, location, hint, frame } = props;
5
+ const { title = "MarkdocError", message, stack, location, hint, frame } = props;
6
6
  this.title = title;
7
7
  if (message)
8
8
  this.message = message;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/markdoc",
3
3
  "description": "Add support for Markdoc in your Astro site",
4
- "version": "0.4.0",
4
+ "version": "0.4.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -67,7 +67,7 @@
67
67
  "zod": "^3.17.3"
68
68
  },
69
69
  "peerDependencies": {
70
- "astro": "^2.7.2"
70
+ "astro": "^2.8.1"
71
71
  },
72
72
  "devDependencies": {
73
73
  "@astrojs/markdown-remark": "^2.2.1",
@@ -80,7 +80,7 @@
80
80
  "mocha": "^9.2.2",
81
81
  "rollup": "^3.25.1",
82
82
  "vite": "^4.3.9",
83
- "astro": "2.7.2",
83
+ "astro": "2.8.1",
84
84
  "astro-scripts": "0.0.14"
85
85
  },
86
86
  "engines": {