@astrojs/mdx 0.3.0 → 0.5.0

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 (31) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/CHANGELOG.md +29 -0
  3. package/README.md +100 -2
  4. package/dist/index.js +49 -16
  5. package/dist/rehype-collect-headings.d.ts +6 -0
  6. package/dist/rehype-collect-headings.js +47 -0
  7. package/dist/remark-prism.js +2 -34
  8. package/dist/utils.d.ts +15 -3
  9. package/dist/utils.js +51 -2
  10. package/package.json +11 -7
  11. package/src/index.ts +57 -24
  12. package/src/rehype-collect-headings.ts +50 -0
  13. package/src/remark-prism.ts +2 -44
  14. package/src/utils.ts +69 -5
  15. package/test/fixtures/mdx-frontmatter/src/layouts/Base.astro +18 -0
  16. package/test/fixtures/mdx-frontmatter/src/pages/index.mdx +2 -1
  17. package/test/fixtures/mdx-get-headings/src/pages/pages.json.js +11 -0
  18. package/test/fixtures/mdx-get-headings/src/pages/test-with-jsx-expressions.mdx +8 -0
  19. package/test/fixtures/mdx-get-headings/src/pages/test.mdx +9 -0
  20. package/test/fixtures/mdx-get-static-paths/src/content/1.mdx +5 -0
  21. package/test/fixtures/mdx-get-static-paths/src/pages/[slug].astro +34 -0
  22. package/test/fixtures/mdx-page/astro.config.ts +5 -0
  23. package/test/fixtures/mdx-page/node_modules/.bin/astro +17 -0
  24. package/test/fixtures/mdx-page/package.json +7 -0
  25. package/test/fixtures/mdx-rehype-plugins/src/pages/reading-time.json.js +7 -0
  26. package/test/fixtures/mdx-rehype-plugins/src/pages/space-ipsum.mdx +25 -0
  27. package/test/mdx-frontmatter.test.js +31 -0
  28. package/test/mdx-get-headings.test.js +60 -0
  29. package/test/mdx-get-static-paths.test.js +32 -0
  30. package/test/mdx-page.test.js +0 -1
  31. package/test/mdx-rehype-plugins.test.js +81 -0
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 5ec97556db3f4322
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.3.0 build /home/runner/work/astro/astro/packages/integrations/mdx
4
- @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
- @astrojs/mdx:build: 
1
+ @astrojs/mdx:build: cache hit, replaying output 87ac7debf5e14016
2
+ @astrojs/mdx:build: 
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.5.0 build /home/runner/work/astro/astro/packages/integrations/mdx
4
+ @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
+ @astrojs/mdx:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # @astrojs/mdx
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#4095](https://github.com/withastro/astro/pull/4095) [`40ef43a59`](https://github.com/withastro/astro/commit/40ef43a59b08a1a8fbcd9f4a53745a9636a4fbb9) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Add IDs to MDX headings and expose via getHeadings() export
8
+
9
+ * [#4114](https://github.com/withastro/astro/pull/4114) [`64432bcb8`](https://github.com/withastro/astro/commit/64432bcb873efd0e4297c00fc9583a1fe516dfe7) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Refactor `@astrojs/mdx` and `@astrojs/markdown-remark` to use `@astrojs/prism` instead of duplicating the code
10
+
11
+ ### Patch Changes
12
+
13
+ - [#4112](https://github.com/withastro/astro/pull/4112) [`e33fc9bc4`](https://github.com/withastro/astro/commit/e33fc9bc46ff0a30013deb6dc76e545e70cc3a3e) Thanks [@matthewp](https://github.com/matthewp)! - Fix MDX working with a ts config file
14
+
15
+ * [#4049](https://github.com/withastro/astro/pull/4049) [`b60cc0538`](https://github.com/withastro/astro/commit/b60cc0538bc5c68dd411117780d20d892530789d) Thanks [@natemoo-re](https://github.com/natemoo-re)! - Improve `injectScript` handling for non-Astro pages
16
+
17
+ * Updated dependencies [[`64432bcb8`](https://github.com/withastro/astro/commit/64432bcb873efd0e4297c00fc9583a1fe516dfe7)]:
18
+ - @astrojs/prism@0.7.0
19
+
20
+ ## 0.4.0
21
+
22
+ ### Minor Changes
23
+
24
+ - [#4088](https://github.com/withastro/astro/pull/4088) [`1743fe140`](https://github.com/withastro/astro/commit/1743fe140eb58d60e26cbd11a066bb60de046e0c) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support "layout" frontmatter property
25
+
26
+ ## 0.3.1
27
+
28
+ ### Patch Changes
29
+
30
+ - [#4076](https://github.com/withastro/astro/pull/4076) [`6120a71e5`](https://github.com/withastro/astro/commit/6120a71e5425ad55a17ddac800d64a3f50273bce) Thanks [@matthewp](https://github.com/matthewp)! - Ensure file and url are always present in MDX for Astro.glob
31
+
3
32
  ## 0.3.0
4
33
 
5
34
  ### Minor Changes
package/README.md CHANGED
@@ -103,6 +103,24 @@ const posts = await Astro.glob('./*.mdx');
103
103
 
104
104
  See [the official "how MDX works" guide](https://mdxjs.com/docs/using-mdx/#how-mdx-works) for more on MDX variables.
105
105
 
106
+ ### Exported properties
107
+
108
+ Alongside your [MDX variable exports](#variables), we generate a few helpful exports as well. These are accessible when importing an MDX file via `import` statements or [`Astro.glob`](https://docs.astro.build/en/reference/api-reference/#astroglob).
109
+
110
+ #### `file`
111
+
112
+ The absolute path to the MDX file (e.g. `home/user/projects/.../file.md`).
113
+
114
+ #### `url`
115
+
116
+ The browser-ready URL for MDX files under `src/pages/`. For example, `src/pages/en/about.mdx` will provide a `url` of `/en/about/`. For MDX files outside of `src/pages`, `url` will be `undefined`.
117
+
118
+ #### `getHeadings()`
119
+
120
+ **Returns:** `{ depth: number; slug: string; text: string }[]`
121
+
122
+ A function that returns an array of all headings (i.e. `h1 -> h6` elements) in the MDX file. Each heading’s `slug` corresponds to the generated ID for a given heading and can be used for anchor links.
123
+
106
124
  ### Frontmatter
107
125
 
108
126
  Astro also supports YAML-based frontmatter out-of-the-box using the [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter) plugin. By default, all variables declared in a frontmatter fence (`---`) will be accessible via the `frontmatter` export. See the `frontmatterOptions` configuration to customize this behavior.
@@ -134,6 +152,71 @@ const posts = await Astro.glob('./*.mdx');
134
152
  ))}
135
153
  ```
136
154
 
155
+ ### Layouts
156
+
157
+ Layouts can be applied [in the same way as standard Astro Markdown](https://docs.astro.build/en/guides/markdown-content/#markdown-layouts). You can add a `layout` to [your frontmatter](#frontmatter) like so:
158
+
159
+ ```yaml
160
+ ---
161
+ layout: '../layouts/BaseLayout.astro'
162
+ title: 'My Blog Post'
163
+ ---
164
+ ```
165
+
166
+ Then, you can retrieve all other frontmatter properties from your layout via the `content` property, and render your MDX using the default [`<slot />`](https://docs.astro.build/en/core-concepts/astro-components/#slots):
167
+
168
+ ```astro
169
+ ---
170
+ // src/layouts/BaseLayout.astro
171
+ const { content } = Astro.props;
172
+ ---
173
+ <html>
174
+ <head>
175
+ <title>{content.title}</title>
176
+ </head>
177
+ <body>
178
+ <h1>{content.title}</h1>
179
+ <!-- Rendered MDX will be passed into the default slot. -->
180
+ <slot />
181
+ </body>
182
+ </html>
183
+ ```
184
+
185
+ #### Importing layouts manually
186
+
187
+ You may need to pass information to your layouts that does not (or cannot) exist in your frontmatter. In this case, you can import and use a [`<Layout />` component](https://docs.astro.build/en/core-concepts/layouts/) like any other component:
188
+
189
+ ```mdx
190
+ ---
191
+ // src/pages/posts/first-post.mdx
192
+
193
+ title: 'My first MDX post'
194
+ publishDate: '21 September 2022'
195
+ ---
196
+ import BaseLayout from '../layouts/BaseLayout.astro';
197
+
198
+ function fancyJsHelper() {
199
+ return "Try doing that with YAML!";
200
+ }
201
+
202
+ <BaseLayout title={frontmatter.title} fancyJsHelper={fancyJsHelper}>
203
+ Welcome to my new Astro blog, using MDX!
204
+ </BaseLayout>
205
+ ```
206
+ Then, your values are available to you through `Astro.props` in your layout, and your MDX content will be injected into the page where your `<slot />` component is written:
207
+
208
+ ```astro
209
+ ---
210
+ // src/layouts/BaseLayout.astro
211
+ const { title, fancyJsHelper } = Astro.props;
212
+ ---
213
+ <!-- -->
214
+ <h1>{title}</h1>
215
+ <slot />
216
+ <p>{fancyJsHelper()}</p>
217
+ <!-- -->
218
+ ```
219
+
137
220
  ### Syntax highlighting
138
221
 
139
222
  The MDX integration respects [your project's `markdown.syntaxHighlight` configuration](https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting).
@@ -214,11 +297,26 @@ export default {
214
297
  <details>
215
298
  <summary><strong>rehypePlugins</strong></summary>
216
299
 
217
- **Default plugins:** none
300
+ **Default plugins:** [`collect-headings`](https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/rehype-collect-headings.ts)
218
301
 
219
302
  [Rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) allow you to transform the HTML that your Markdown generates. We recommend checking the [Remark plugin](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) catalog first _before_ considering rehype plugins, since most users want to transform their Markdown syntax instead. If HTML transforms are what you need, we encourage you to browse [awesome-rehype](https://github.com/rehypejs/awesome-rehype) for a full curated list of plugins!
220
303
 
221
- To apply rehype plugins, use the `rehypePlugins` configuration option like so:
304
+ We apply our own [`collect-headings`](https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/rehype-collect-headings.ts) plugin by default. This applies IDs to all headings (i.e. `h1 -> h6`) in your MDX files to [link to headings via anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#linking_to_an_element_on_the_same_page).
305
+
306
+ To apply rehype plugins _while preserving_ Astro's default plugins, use a nested `extends` object like so:
307
+
308
+ ```js
309
+ // astro.config.mjs
310
+ import rehypeMinifyHtml from 'rehype-minify';
311
+
312
+ export default {
313
+ integrations: [mdx({
314
+ rehypePlugins: { extends: [rehypeMinifyHtml] },
315
+ })],
316
+ }
317
+ ```
318
+
319
+ To apply plugins _without_ Astro's defaults, you can apply a plain array:
222
320
 
223
321
  ```js
224
322
  // astro.config.mjs
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { nodeTypes } from "@mdx-js/mdx";
1
+ import { compile as mdxCompile, nodeTypes } from "@mdx-js/mdx";
2
2
  import mdxPlugin from "@mdx-js/rollup";
3
3
  import { parse as parseESM } from "es-module-lexer";
4
4
  import rehypeRaw from "rehype-raw";
@@ -7,9 +7,12 @@ import remarkGfm from "remark-gfm";
7
7
  import remarkMdxFrontmatter from "remark-mdx-frontmatter";
8
8
  import remarkShikiTwoslash from "remark-shiki-twoslash";
9
9
  import remarkSmartypants from "remark-smartypants";
10
+ import { VFile } from "vfile";
11
+ import rehypeCollectHeadings from "./rehype-collect-headings.js";
10
12
  import remarkPrism from "./remark-prism.js";
11
- import { getFileInfo } from "./utils.js";
13
+ import { getFileInfo, getFrontmatter } from "./utils.js";
12
14
  const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
15
+ const DEFAULT_REHYPE_PLUGINS = [rehypeCollectHeadings];
13
16
  function handleExtends(config, defaults = []) {
14
17
  if (Array.isArray(config))
15
18
  return config;
@@ -22,10 +25,10 @@ function mdx(mdxOptions = {}) {
22
25
  "astro:config:setup": ({ updateConfig, config, addPageExtension, command }) => {
23
26
  addPageExtension(".mdx");
24
27
  let remarkPlugins = handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS);
25
- let rehypePlugins = handleExtends(mdxOptions.rehypePlugins);
28
+ let rehypePlugins = handleExtends(mdxOptions.rehypePlugins, DEFAULT_REHYPE_PLUGINS);
26
29
  if (config.markdown.syntaxHighlight === "shiki") {
27
30
  remarkPlugins.push([
28
- remarkShikiTwoslash.default,
31
+ remarkShikiTwoslash.default ?? remarkShikiTwoslash,
29
32
  config.markdown.shikiConfig
30
33
  ]);
31
34
  rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
@@ -42,32 +45,62 @@ function mdx(mdxOptions = {}) {
42
45
  ...mdxOptions.frontmatterOptions
43
46
  }
44
47
  ]);
48
+ const mdxPluginOpts = {
49
+ remarkPlugins,
50
+ rehypePlugins,
51
+ jsx: true,
52
+ jsxImportSource: "astro",
53
+ format: "mdx",
54
+ mdExtensions: []
55
+ };
45
56
  updateConfig({
46
57
  vite: {
47
58
  plugins: [
48
59
  {
49
60
  enforce: "pre",
50
- ...mdxPlugin({
51
- remarkPlugins,
52
- rehypePlugins,
53
- jsx: true,
54
- jsxImportSource: "astro",
55
- format: "mdx",
56
- mdExtensions: []
57
- })
61
+ ...mdxPlugin(mdxPluginOpts),
62
+ async transform(code, id) {
63
+ var _a;
64
+ if (!id.endsWith("mdx"))
65
+ return;
66
+ if (!((_a = mdxOptions.frontmatterOptions) == null ? void 0 : _a.parsers)) {
67
+ const frontmatter = getFrontmatter(code, id);
68
+ if (frontmatter.layout) {
69
+ const { layout, ...content } = frontmatter;
70
+ code += `
71
+ export default async function({ children }) {
72
+ const Layout = (await import(${JSON.stringify(
73
+ frontmatter.layout
74
+ )})).default;
75
+ return <Layout content={${JSON.stringify(
76
+ content
77
+ )}}>{children}</Layout> }`;
78
+ }
79
+ }
80
+ const compiled = await mdxCompile(
81
+ new VFile({ value: code, path: id }),
82
+ mdxPluginOpts
83
+ );
84
+ return {
85
+ code: String(compiled.value),
86
+ map: compiled.map
87
+ };
88
+ }
58
89
  },
59
90
  {
60
- name: "@astrojs/mdx",
91
+ name: "@astrojs/mdx-postprocess",
61
92
  transform(code, id) {
62
93
  if (!id.endsWith(".mdx"))
63
94
  return;
64
95
  const [, moduleExports] = parseESM(code);
65
- code += `
66
- import "${"astro:scripts/page-ssr.js"}";`;
96
+ const { fileUrl, fileId } = getFileInfo(id, config);
67
97
  if (!moduleExports.includes("url")) {
68
- const { fileUrl } = getFileInfo(id, config);
69
98
  code += `
70
99
  export const url = ${JSON.stringify(fileUrl)};`;
100
+ }
101
+ if (!moduleExports.includes("file")) {
102
+ code += `
103
+ export const file = ${JSON.stringify(fileId)};`;
71
104
  }
72
105
  if (command === "dev") {
73
106
  code += `
@@ -0,0 +1,6 @@
1
+ export interface MarkdownHeading {
2
+ depth: number;
3
+ slug: string;
4
+ text: string;
5
+ }
6
+ export default function rehypeCollectHeadings(): (tree: any) => void;
@@ -0,0 +1,47 @@
1
+ import Slugger from "github-slugger";
2
+ import { visit } from "unist-util-visit";
3
+ import { jsToTreeNode } from "./utils.js";
4
+ function rehypeCollectHeadings() {
5
+ const slugger = new Slugger();
6
+ return function(tree) {
7
+ const headings = [];
8
+ visit(tree, (node) => {
9
+ if (node.type !== "element")
10
+ return;
11
+ const { tagName } = node;
12
+ if (tagName[0] !== "h")
13
+ return;
14
+ const [_, level] = tagName.match(/h([0-6])/) ?? [];
15
+ if (!level)
16
+ return;
17
+ const depth = Number.parseInt(level);
18
+ let text = "";
19
+ visit(node, (child, __, parent) => {
20
+ if (child.type === "element" || parent == null) {
21
+ return;
22
+ }
23
+ if (child.type === "raw" && child.value.match(/^\n?<.*>\n?$/)) {
24
+ return;
25
+ }
26
+ if ((/* @__PURE__ */ new Set(["text", "raw", "mdxTextExpression"])).has(child.type)) {
27
+ text += child.value;
28
+ }
29
+ });
30
+ node.properties = node.properties || {};
31
+ if (typeof node.properties.id !== "string") {
32
+ let slug = slugger.slug(text);
33
+ if (slug.endsWith("-")) {
34
+ slug = slug.slice(0, -1);
35
+ }
36
+ node.properties.id = slug;
37
+ }
38
+ headings.push({ depth, slug: node.properties.id, text });
39
+ });
40
+ tree.children.unshift(
41
+ jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`)
42
+ );
43
+ };
44
+ }
45
+ export {
46
+ rehypeCollectHeadings as default
47
+ };
@@ -1,42 +1,10 @@
1
- import { addAstro } from "@astrojs/prism/internal";
2
- import Prism from "prismjs";
3
- import loadLanguages from "prismjs/components/index.js";
1
+ import { runHighlighterWithAstro } from "@astrojs/prism/dist/highlighter";
4
2
  import { visit } from "unist-util-visit";
5
- const languageMap = /* @__PURE__ */ new Map([["ts", "typescript"]]);
6
- function runHighlighter(lang, code) {
7
- let classLanguage = `language-${lang}`;
8
- if (lang == null) {
9
- lang = "plaintext";
10
- }
11
- const ensureLoaded = (language) => {
12
- if (language && !Prism.languages[language]) {
13
- loadLanguages([language]);
14
- }
15
- };
16
- if (languageMap.has(lang)) {
17
- ensureLoaded(languageMap.get(lang));
18
- } else if (lang === "astro") {
19
- ensureLoaded("typescript");
20
- addAstro(Prism);
21
- } else {
22
- ensureLoaded("markup-templating");
23
- ensureLoaded(lang);
24
- }
25
- if (lang && !Prism.languages[lang]) {
26
- console.warn(`Unable to load the language: ${lang}`);
27
- }
28
- const grammar = Prism.languages[lang];
29
- let html = code;
30
- if (grammar) {
31
- html = Prism.highlight(code, grammar, lang);
32
- }
33
- return { classLanguage, html };
34
- }
35
3
  function remarkPrism() {
36
4
  return (tree) => visit(tree, "code", (node) => {
37
5
  let { lang, value } = node;
38
6
  node.type = "html";
39
- let { html, classLanguage } = runHighlighter(lang, value);
7
+ let { html, classLanguage } = runHighlighterWithAstro(lang, value);
40
8
  let classes = [classLanguage];
41
9
  node.value = `<pre class="${classes.join(
42
10
  " "
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,18 @@
1
+ import type { Options as AcornOpts } from 'acorn';
1
2
  import type { AstroConfig } from 'astro';
2
- /** @see 'vite-plugin-utils' for source */
3
- export declare function getFileInfo(id: string, config: AstroConfig): {
3
+ import type { MdxjsEsm } from 'mdast-util-mdx';
4
+ interface FileInfo {
4
5
  fileId: string;
5
- fileUrl: string | undefined;
6
+ fileUrl: string;
7
+ }
8
+ /** @see 'vite-plugin-utils' for source */
9
+ export declare function getFileInfo(id: string, config: AstroConfig): FileInfo;
10
+ /**
11
+ * Match YAML exception handling from Astro core errors
12
+ * @see 'astro/src/core/errors.ts'
13
+ */
14
+ export declare function getFrontmatter(code: string, id: string): {
15
+ [key: string]: any;
6
16
  };
17
+ export declare function jsToTreeNode(jsString: string, acornOpts?: AcornOpts): MdxjsEsm;
18
+ export {};
package/dist/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { parse } from "acorn";
2
+ import matter from "gray-matter";
1
3
  function appendForwardSlash(path) {
2
4
  return path.endsWith("/") ? path : path + "/";
3
5
  }
@@ -5,13 +7,60 @@ function getFileInfo(id, config) {
5
7
  const sitePathname = appendForwardSlash(
6
8
  config.site ? new URL(config.base, config.site).pathname : config.base
7
9
  );
10
+ let url = void 0;
11
+ try {
12
+ url = new URL(`file://${id}`);
13
+ } catch {
14
+ }
8
15
  const fileId = id.split("?")[0];
9
- let fileUrl = fileId.includes("/pages/") ? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, "") : void 0;
16
+ let fileUrl;
17
+ const isPage = fileId.includes("/pages/");
18
+ if (isPage) {
19
+ fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, "");
20
+ } else if (url && url.pathname.startsWith(config.root.pathname)) {
21
+ fileUrl = url.pathname.slice(config.root.pathname.length);
22
+ } else {
23
+ fileUrl = fileId;
24
+ }
10
25
  if (fileUrl && config.trailingSlash === "always") {
11
26
  fileUrl = appendForwardSlash(fileUrl);
12
27
  }
13
28
  return { fileId, fileUrl };
14
29
  }
30
+ function getFrontmatter(code, id) {
31
+ try {
32
+ return matter(code).data;
33
+ } catch (e) {
34
+ if (e.name === "YAMLException") {
35
+ const err = e;
36
+ err.id = id;
37
+ err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column };
38
+ err.message = e.reason;
39
+ throw err;
40
+ } else {
41
+ throw e;
42
+ }
43
+ }
44
+ }
45
+ function jsToTreeNode(jsString, acornOpts = {
46
+ ecmaVersion: "latest",
47
+ sourceType: "module"
48
+ }) {
49
+ return {
50
+ type: "mdxjsEsm",
51
+ value: "",
52
+ data: {
53
+ estree: {
54
+ body: [],
55
+ ...parse(jsString, acornOpts),
56
+ type: "Program",
57
+ sourceType: "module"
58
+ }
59
+ }
60
+ };
61
+ }
15
62
  export {
16
- getFileInfo
63
+ getFileInfo,
64
+ getFrontmatter,
65
+ jsToTreeNode
17
66
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/mdx",
3
3
  "description": "Use MDX within Astro",
4
- "version": "0.3.0",
4
+ "version": "0.5.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -24,29 +24,33 @@
24
24
  "./package.json": "./package.json"
25
25
  },
26
26
  "dependencies": {
27
- "@astrojs/prism": "^0.6.1",
27
+ "@astrojs/prism": "^0.7.0",
28
28
  "@mdx-js/mdx": "^2.1.2",
29
29
  "@mdx-js/rollup": "^2.1.1",
30
+ "acorn": "^8.8.0",
30
31
  "es-module-lexer": "^0.10.5",
31
- "prismjs": "^1.28.0",
32
+ "github-slugger": "^1.4.0",
33
+ "gray-matter": "^4.0.3",
32
34
  "rehype-raw": "^6.1.1",
35
+ "remark-frontmatter": "^4.0.1",
33
36
  "remark-gfm": "^3.0.1",
37
+ "remark-mdx-frontmatter": "^2.0.2",
34
38
  "remark-shiki-twoslash": "^3.1.0",
35
39
  "remark-smartypants": "^2.0.0",
36
40
  "shiki": "^0.10.1",
37
- "unist-util-visit": "^4.1.0",
38
- "remark-frontmatter": "^4.0.1",
39
- "remark-mdx-frontmatter": "^2.0.2"
41
+ "unist-util-visit": "^4.1.0"
40
42
  },
41
43
  "devDependencies": {
42
44
  "@types/chai": "^4.3.1",
43
45
  "@types/mocha": "^9.1.1",
44
46
  "@types/yargs-parser": "^21.0.0",
45
- "astro": "1.0.0-rc.1",
47
+ "astro": "1.0.0-rc.4",
46
48
  "astro-scripts": "0.0.6",
47
49
  "chai": "^4.3.6",
48
50
  "linkedom": "^0.14.12",
51
+ "mdast-util-to-string": "^3.1.0",
49
52
  "mocha": "^9.2.2",
53
+ "reading-time": "^1.5.0",
50
54
  "remark-toc": "^8.0.1"
51
55
  },
52
56
  "engines": {
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { nodeTypes } from '@mdx-js/mdx';
1
+ import { compile as mdxCompile, nodeTypes } from '@mdx-js/mdx';
2
2
  import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
3
3
  import type { AstroIntegration } from 'astro';
4
4
  import { parse as parseESM } from 'es-module-lexer';
@@ -9,8 +9,11 @@ import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter';
9
9
  import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
10
10
  import remarkShikiTwoslash from 'remark-shiki-twoslash';
11
11
  import remarkSmartypants from 'remark-smartypants';
12
+ import { VFile } from 'vfile';
13
+ import type { Plugin as VitePlugin } from 'vite';
14
+ import rehypeCollectHeadings from './rehype-collect-headings.js';
12
15
  import remarkPrism from './remark-prism.js';
13
- import { getFileInfo } from './utils.js';
16
+ import { getFileInfo, getFrontmatter } from './utils.js';
14
17
 
15
18
  type WithExtends<T> = T | { extends: T };
16
19
 
@@ -26,6 +29,7 @@ type MdxOptions = {
26
29
  };
27
30
 
28
31
  const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
32
+ const DEFAULT_REHYPE_PLUGINS = [rehypeCollectHeadings];
29
33
 
30
34
  function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] {
31
35
  if (Array.isArray(config)) return config;
@@ -40,7 +44,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
40
44
  'astro:config:setup': ({ updateConfig, config, addPageExtension, command }: any) => {
41
45
  addPageExtension('.mdx');
42
46
  let remarkPlugins = handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS);
43
- let rehypePlugins = handleExtends(mdxOptions.rehypePlugins);
47
+ let rehypePlugins = handleExtends(mdxOptions.rehypePlugins, DEFAULT_REHYPE_PLUGINS);
44
48
 
45
49
  if (config.markdown.syntaxHighlight === 'shiki') {
46
50
  remarkPlugins.push([
@@ -48,7 +52,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
48
52
  // Workarounds tried:
49
53
  // - "import * as remarkShikiTwoslash"
50
54
  // - "import { default as remarkShikiTwoslash }"
51
- (remarkShikiTwoslash as any).default,
55
+ (remarkShikiTwoslash as any).default ?? remarkShikiTwoslash,
52
56
  config.markdown.shikiConfig,
53
57
  ]);
54
58
  rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
@@ -68,38 +72,67 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
68
72
  },
69
73
  ]);
70
74
 
75
+ const mdxPluginOpts: MdxRollupPluginOptions = {
76
+ remarkPlugins,
77
+ rehypePlugins,
78
+ jsx: true,
79
+ jsxImportSource: 'astro',
80
+ // Note: disable `.md` support
81
+ format: 'mdx',
82
+ mdExtensions: [],
83
+ };
84
+
71
85
  updateConfig({
72
86
  vite: {
73
87
  plugins: [
74
88
  {
75
89
  enforce: 'pre',
76
- ...mdxPlugin({
77
- remarkPlugins,
78
- rehypePlugins,
79
- jsx: true,
80
- jsxImportSource: 'astro',
81
- // Note: disable `.md` support
82
- format: 'mdx',
83
- mdExtensions: [],
84
- }),
90
+ ...mdxPlugin(mdxPluginOpts),
91
+ // Override transform to alter code before MDX compilation
92
+ // ex. inject layouts
93
+ async transform(code, id) {
94
+ if (!id.endsWith('mdx')) return;
95
+
96
+ // If user overrides our default YAML parser,
97
+ // do not attempt to parse the `layout` via gray-matter
98
+ if (!mdxOptions.frontmatterOptions?.parsers) {
99
+ const frontmatter = getFrontmatter(code, id);
100
+ if (frontmatter.layout) {
101
+ const { layout, ...content } = frontmatter;
102
+ code += `\nexport default async function({ children }) {\nconst Layout = (await import(${JSON.stringify(
103
+ frontmatter.layout
104
+ )})).default;\nreturn <Layout content={${JSON.stringify(
105
+ content
106
+ )}}>{children}</Layout> }`;
107
+ }
108
+ }
109
+
110
+ const compiled = await mdxCompile(
111
+ new VFile({ value: code, path: id }),
112
+ mdxPluginOpts
113
+ );
114
+
115
+ return {
116
+ code: String(compiled.value),
117
+ map: compiled.map,
118
+ };
119
+ },
85
120
  },
86
121
  {
87
- name: '@astrojs/mdx',
88
- transform(code: string, id: string) {
122
+ name: '@astrojs/mdx-postprocess',
123
+ // These transforms must happen *after* JSX runtime transformations
124
+ transform(code, id) {
89
125
  if (!id.endsWith('.mdx')) return;
90
126
  const [, moduleExports] = parseESM(code);
91
127
 
92
- // This adds support for injected "page-ssr" scripts in MDX files.
93
- // TODO: This should only be happening on page entrypoints, not all imported MDX.
94
- // TODO: This code is copy-pasted across all Astro/Vite plugins that deal with page
95
- // entrypoints (.astro, .md, .mdx). This should be handled in some centralized place,
96
- // or otherwise refactored to not require copy-paste handling logic.
97
- code += `\nimport "${'astro:scripts/page-ssr.js'}";`;
98
-
128
+ const { fileUrl, fileId } = getFileInfo(id, config);
99
129
  if (!moduleExports.includes('url')) {
100
- const { fileUrl } = getFileInfo(id, config);
101
130
  code += `\nexport const url = ${JSON.stringify(fileUrl)};`;
102
131
  }
132
+ if (!moduleExports.includes('file')) {
133
+ code += `\nexport const file = ${JSON.stringify(fileId)};`;
134
+ }
135
+
103
136
  if (command === 'dev') {
104
137
  // TODO: decline HMR updates until we have a stable approach
105
138
  code += `\nif (import.meta.hot) {
@@ -109,7 +142,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
109
142
  return code;
110
143
  },
111
144
  },
112
- ],
145
+ ] as VitePlugin[],
113
146
  },
114
147
  });
115
148
  },
@@ -0,0 +1,50 @@
1
+ import Slugger from 'github-slugger';
2
+ import { visit } from 'unist-util-visit';
3
+ import { jsToTreeNode } from './utils.js';
4
+
5
+ export interface MarkdownHeading {
6
+ depth: number;
7
+ slug: string;
8
+ text: string;
9
+ }
10
+
11
+ export default function rehypeCollectHeadings() {
12
+ const slugger = new Slugger();
13
+ return function (tree: any) {
14
+ const headings: MarkdownHeading[] = [];
15
+ visit(tree, (node) => {
16
+ if (node.type !== 'element') return;
17
+ const { tagName } = node;
18
+ if (tagName[0] !== 'h') return;
19
+ const [_, level] = tagName.match(/h([0-6])/) ?? [];
20
+ if (!level) return;
21
+ const depth = Number.parseInt(level);
22
+
23
+ let text = '';
24
+ visit(node, (child, __, parent) => {
25
+ if (child.type === 'element' || parent == null) {
26
+ return;
27
+ }
28
+ if (child.type === 'raw' && child.value.match(/^\n?<.*>\n?$/)) {
29
+ return;
30
+ }
31
+ if (new Set(['text', 'raw', 'mdxTextExpression']).has(child.type)) {
32
+ text += child.value;
33
+ }
34
+ });
35
+
36
+ node.properties = node.properties || {};
37
+ if (typeof node.properties.id !== 'string') {
38
+ let slug = slugger.slug(text);
39
+ if (slug.endsWith('-')) {
40
+ slug = slug.slice(0, -1);
41
+ }
42
+ node.properties.id = slug;
43
+ }
44
+ headings.push({ depth, slug: node.properties.id, text });
45
+ });
46
+ tree.children.unshift(
47
+ jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`)
48
+ );
49
+ };
50
+ }
@@ -1,48 +1,6 @@
1
- // TODO: discuss extracting this file to @astrojs/prism
2
- import { addAstro } from '@astrojs/prism/internal';
3
- import Prism from 'prismjs';
4
- import loadLanguages from 'prismjs/components/index.js';
1
+ import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter';
5
2
  import { visit } from 'unist-util-visit';
6
3
 
7
- const languageMap = new Map([['ts', 'typescript']]);
8
-
9
- function runHighlighter(lang: string, code: string) {
10
- let classLanguage = `language-${lang}`;
11
-
12
- if (lang == null) {
13
- lang = 'plaintext';
14
- }
15
-
16
- const ensureLoaded = (language: string) => {
17
- if (language && !Prism.languages[language]) {
18
- loadLanguages([language]);
19
- }
20
- };
21
-
22
- if (languageMap.has(lang)) {
23
- ensureLoaded(languageMap.get(lang)!);
24
- } else if (lang === 'astro') {
25
- ensureLoaded('typescript');
26
- addAstro(Prism);
27
- } else {
28
- ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
29
- ensureLoaded(lang);
30
- }
31
-
32
- if (lang && !Prism.languages[lang]) {
33
- // eslint-disable-next-line no-console
34
- console.warn(`Unable to load the language: ${lang}`);
35
- }
36
-
37
- const grammar = Prism.languages[lang];
38
- let html = code;
39
- if (grammar) {
40
- html = Prism.highlight(code, grammar, lang);
41
- }
42
-
43
- return { classLanguage, html };
44
- }
45
-
46
4
  /** */
47
5
  export default function remarkPrism() {
48
6
  return (tree: any) =>
@@ -50,7 +8,7 @@ export default function remarkPrism() {
50
8
  let { lang, value } = node;
51
9
  node.type = 'html';
52
10
 
53
- let { html, classLanguage } = runHighlighter(lang, value);
11
+ let { html, classLanguage } = runHighlighterWithAstro(lang, value);
54
12
  let classes = [classLanguage];
55
13
  node.value = `<pre class="${classes.join(
56
14
  ' '
package/src/utils.ts CHANGED
@@ -1,21 +1,85 @@
1
- import type { AstroConfig } from 'astro';
1
+ import type { Options as AcornOpts } from 'acorn';
2
+ import { parse } from 'acorn';
3
+ import type { AstroConfig, SSRError } from 'astro';
4
+ import type { MdxjsEsm } from 'mdast-util-mdx';
5
+
6
+ import matter from 'gray-matter';
2
7
 
3
8
  function appendForwardSlash(path: string) {
4
9
  return path.endsWith('/') ? path : path + '/';
5
10
  }
6
11
 
12
+ interface FileInfo {
13
+ fileId: string;
14
+ fileUrl: string;
15
+ }
16
+
7
17
  /** @see 'vite-plugin-utils' for source */
8
- export function getFileInfo(id: string, config: AstroConfig) {
18
+ export function getFileInfo(id: string, config: AstroConfig): FileInfo {
9
19
  const sitePathname = appendForwardSlash(
10
20
  config.site ? new URL(config.base, config.site).pathname : config.base
11
21
  );
12
22
 
23
+ // Try to grab the file's actual URL
24
+ let url: URL | undefined = undefined;
25
+ try {
26
+ url = new URL(`file://${id}`);
27
+ } catch {}
28
+
13
29
  const fileId = id.split('?')[0];
14
- let fileUrl = fileId.includes('/pages/')
15
- ? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, '')
16
- : undefined;
30
+ let fileUrl: string;
31
+ const isPage = fileId.includes('/pages/');
32
+ if (isPage) {
33
+ fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, '');
34
+ } else if (url && url.pathname.startsWith(config.root.pathname)) {
35
+ fileUrl = url.pathname.slice(config.root.pathname.length);
36
+ } else {
37
+ fileUrl = fileId;
38
+ }
39
+
17
40
  if (fileUrl && config.trailingSlash === 'always') {
18
41
  fileUrl = appendForwardSlash(fileUrl);
19
42
  }
20
43
  return { fileId, fileUrl };
21
44
  }
45
+
46
+ /**
47
+ * Match YAML exception handling from Astro core errors
48
+ * @see 'astro/src/core/errors.ts'
49
+ */
50
+ export function getFrontmatter(code: string, id: string) {
51
+ try {
52
+ return matter(code).data;
53
+ } catch (e: any) {
54
+ if (e.name === 'YAMLException') {
55
+ const err: SSRError = e;
56
+ err.id = id;
57
+ err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column };
58
+ err.message = e.reason;
59
+ throw err;
60
+ } else {
61
+ throw e;
62
+ }
63
+ }
64
+ }
65
+
66
+ export function jsToTreeNode(
67
+ jsString: string,
68
+ acornOpts: AcornOpts = {
69
+ ecmaVersion: 'latest',
70
+ sourceType: 'module',
71
+ }
72
+ ): MdxjsEsm {
73
+ return {
74
+ type: 'mdxjsEsm',
75
+ value: '',
76
+ data: {
77
+ estree: {
78
+ body: [],
79
+ ...parse(jsString, acornOpts),
80
+ type: 'Program',
81
+ sourceType: 'module',
82
+ },
83
+ },
84
+ };
85
+ }
@@ -0,0 +1,18 @@
1
+ ---
2
+ const { content = { title: "Didn't work" } } = Astro.props;
3
+ ---
4
+
5
+ <!DOCTYPE html>
6
+ <html lang="en">
7
+ <head>
8
+ <meta charset="UTF-8">
9
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
10
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
11
+ <title>{content.title}</title>
12
+ </head>
13
+ <body>
14
+ <h1>{content.title}</h1>
15
+ <p data-layout-rendered>Layout rendered!</p>
16
+ <slot />
17
+ </body>
18
+ </html>
@@ -1,6 +1,7 @@
1
1
  ---
2
2
  title: 'Using YAML frontmatter'
3
+ layout: '../layouts/Base.astro'
3
4
  illThrowIfIDontExist: "Oh no, that's scary!"
4
5
  ---
5
6
 
6
- # {frontmatter.illThrowIfIDontExist}
7
+ {frontmatter.illThrowIfIDontExist}
@@ -0,0 +1,11 @@
1
+ export async function get() {
2
+ const mdxPages = await import.meta.glob('./*.mdx', { eager: true });
3
+
4
+ return {
5
+ body: JSON.stringify({
6
+ headingsByPage: Object.fromEntries(
7
+ Object.entries(mdxPages ?? {}).map(([k, v]) => [k, v?.getHeadings()])
8
+ ),
9
+ }),
10
+ }
11
+ }
@@ -0,0 +1,8 @@
1
+ export const h2Title = "Section 1"
2
+ export const h3Title = "Subsection 1"
3
+
4
+ # Heading test with JSX expressions
5
+
6
+ ## {h2Title}
7
+
8
+ ### {h3Title}
@@ -0,0 +1,9 @@
1
+ # Heading test
2
+
3
+ ## Section 1
4
+
5
+ ### Subsection 1
6
+
7
+ ### Subsection 2
8
+
9
+ ## Section 2
@@ -0,0 +1,5 @@
1
+ ---
2
+ one: hello
3
+ slug: one
4
+ ---
5
+ First mdx file
@@ -0,0 +1,34 @@
1
+ ---
2
+ export const getStaticPaths = async () => {
3
+ const content = await Astro.glob('../content/*.mdx');
4
+
5
+ return content
6
+ .filter((page) => !page.frontmatter.draft) // skip drafts
7
+ .map(({ default: MdxContent, frontmatter, url, file }) => {
8
+ return {
9
+ params: { slug: frontmatter.slug || "index" },
10
+ props: {
11
+ MdxContent,
12
+ file,
13
+ frontmatter,
14
+ url
15
+ }
16
+ }
17
+ })
18
+ }
19
+
20
+ const { MdxContent, frontmatter, url, file } = Astro.props;
21
+ ---
22
+
23
+ <html>
24
+ <head>
25
+ <title>Page</title>
26
+ </head>
27
+ <body>
28
+ <MdxContent />
29
+
30
+ <div id="one">{frontmatter.one}</div>
31
+ <div id="url">{url}</div>
32
+ <div id="file">{file}</div>
33
+ </body>
34
+ </html>
@@ -0,0 +1,5 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ export default {
4
+ integrations: [mdx()]
5
+ }
@@ -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,7 @@
1
+ {
2
+ "name": "@test/mdx-page",
3
+ "dependencies": {
4
+ "astro": "workspace:*",
5
+ "@astrojs/mdx": "workspace:*"
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ import { readingTime } from './space-ipsum.mdx';
2
+
3
+ export function get() {
4
+ return {
5
+ body: JSON.stringify(readingTime),
6
+ }
7
+ }
@@ -0,0 +1,25 @@
1
+ # Space ipsum
2
+
3
+ For those who have seen the Earth from space, and for the hundreds and perhaps thousands more who will, the experience most certainly changes your perspective. The things that we share in our world are far more valuable than those which divide us.
4
+
5
+ It suddenly struck me that that tiny pea, pretty and blue, was the Earth. I put up my thumb and shut one eye, and my thumb blotted out the planet Earth. I didn’t feel like a giant. I felt very, very small.
6
+
7
+ Science has not yet mastered prophecy. We predict too much for the next year and yet far too little for the next 10.
8
+
9
+ ## Section 2
10
+
11
+ We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win.
12
+
13
+ There can be no thought of finishing for ‘aiming for the stars.’ Both figuratively and literally, it is a task to occupy the generations. And no matter how much progress one makes, there is always the thrill of just beginning.
14
+
15
+ As I stand out here in the wonders of the unknown at Hadley, I sort of realize there’s a fundamental truth to our nature, Man must explore . . . and this is exploration at its greatest.
16
+
17
+ ## Section 3
18
+
19
+ Never in all their history have men been able truly to conceive of the world as one: a single sphere, a globe, having the qualities of a globe, a round earth in which all the directions eventually meet, in which there is no center because every point, or none, is center — an equal earth which all men occupy as equals. The airman’s earth, if free men make it, will be truly round: a globe in practice, not in theory.
20
+
21
+ To be the first to enter the cosmos, to engage, single-handed, in an unprecedented duel with nature—could one dream of anything more?
22
+
23
+ There can be no thought of finishing for ‘aiming for the stars.’ Both figuratively and literally, it is a task to occupy the generations. And no matter how much progress one makes, there is always the thrill of just beginning.
24
+
25
+ We are all connected; To each other, biologically. To the earth, chemically. To the rest of the universe atomically.
@@ -1,6 +1,7 @@
1
1
  import mdx from '@astrojs/mdx';
2
2
 
3
3
  import { expect } from 'chai';
4
+ import { parseHTML } from 'linkedom';
4
5
  import { loadFixture } from '../../../astro/test/test-utils.js';
5
6
 
6
7
  const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter/', import.meta.url);
@@ -26,6 +27,36 @@ describe('MDX frontmatter', () => {
26
27
  expect(titles).to.include('Using YAML frontmatter');
27
28
  });
28
29
 
30
+ it('renders layout from "layout" frontmatter property', async () => {
31
+ const fixture = await loadFixture({
32
+ root: FIXTURE_ROOT,
33
+ integrations: [mdx()],
34
+ });
35
+ await fixture.build();
36
+
37
+ const html = await fixture.readFile('/index.html');
38
+ const { document } = parseHTML(html);
39
+
40
+ const layoutParagraph = document.querySelector('[data-layout-rendered]');
41
+
42
+ expect(layoutParagraph).to.not.be.null;
43
+ });
44
+
45
+ it('passes frontmatter to layout via "content" prop', async () => {
46
+ const fixture = await loadFixture({
47
+ root: FIXTURE_ROOT,
48
+ integrations: [mdx()],
49
+ });
50
+ await fixture.build();
51
+
52
+ const html = await fixture.readFile('/index.html');
53
+ const { document } = parseHTML(html);
54
+
55
+ const h1 = document.querySelector('h1');
56
+
57
+ expect(h1.textContent).to.equal('Using YAML frontmatter');
58
+ });
59
+
29
60
  it('extracts frontmatter to "customFrontmatter" export when configured', async () => {
30
61
  const fixture = await loadFixture({
31
62
  root: new URL('./fixtures/mdx-custom-frontmatter-name/', import.meta.url),
@@ -0,0 +1,60 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ import { expect } from 'chai';
4
+ import { parseHTML } from 'linkedom';
5
+ import { loadFixture } from '../../../astro/test/test-utils.js';
6
+
7
+ describe('MDX getHeadings', () => {
8
+ let fixture;
9
+
10
+ before(async () => {
11
+ fixture = await loadFixture({
12
+ root: new URL('./fixtures/mdx-get-headings/', import.meta.url),
13
+ integrations: [mdx()],
14
+ });
15
+
16
+ await fixture.build();
17
+ });
18
+
19
+ it('adds anchor IDs to headings', async () => {
20
+ const html = await fixture.readFile('/test/index.html');
21
+ const { document } = parseHTML(html);
22
+
23
+ const h2Ids = document.querySelectorAll('h2').map((el) => el?.id);
24
+ const h3Ids = document.querySelectorAll('h3').map((el) => el?.id);
25
+ expect(document.querySelector('h1').id).to.equal('heading-test');
26
+ expect(h2Ids).to.contain('section-1');
27
+ expect(h2Ids).to.contain('section-2');
28
+ expect(h3Ids).to.contain('subsection-1');
29
+ expect(h3Ids).to.contain('subsection-2');
30
+ });
31
+
32
+ it('generates correct getHeadings() export', async () => {
33
+ const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
34
+ // TODO: make this a snapshot test :)
35
+ expect(JSON.stringify(headingsByPage['./test.mdx'])).to.equal(
36
+ JSON.stringify([
37
+ { depth: 1, slug: 'heading-test', text: 'Heading test' },
38
+ { depth: 2, slug: 'section-1', text: 'Section 1' },
39
+ { depth: 3, slug: 'subsection-1', text: 'Subsection 1' },
40
+ { depth: 3, slug: 'subsection-2', text: 'Subsection 2' },
41
+ { depth: 2, slug: 'section-2', text: 'Section 2' },
42
+ ])
43
+ );
44
+ });
45
+
46
+ it('generates correct getHeadings() export for JSX expressions', async () => {
47
+ const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
48
+ expect(JSON.stringify(headingsByPage['./test-with-jsx-expressions.mdx'])).to.equal(
49
+ JSON.stringify([
50
+ {
51
+ depth: 1,
52
+ slug: 'heading-test-with-jsx-expressions',
53
+ text: 'Heading test with JSX expressions',
54
+ },
55
+ { depth: 2, slug: 'h2title', text: 'h2Title' },
56
+ { depth: 3, slug: 'h3title', text: 'h3Title' },
57
+ ])
58
+ );
59
+ });
60
+ });
@@ -0,0 +1,32 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ import { expect } from 'chai';
4
+ import { loadFixture } from '../../../astro/test/test-utils.js';
5
+ import * as cheerio from 'cheerio';
6
+
7
+ const FIXTURE_ROOT = new URL('./fixtures/mdx-get-static-paths', import.meta.url);
8
+
9
+ describe('getStaticPaths', () => {
10
+ /** @type {import('astro/test/test-utils').Fixture} */
11
+ let fixture;
12
+ before(async () => {
13
+ fixture = await loadFixture({
14
+ root: FIXTURE_ROOT,
15
+ integrations: [mdx()],
16
+ });
17
+ await fixture.build();
18
+ });
19
+
20
+ it('Provides file and url', async () => {
21
+ const html = await fixture.readFile('/one/index.html');
22
+
23
+ const $ = cheerio.load(html);
24
+ expect($('p').text()).to.equal('First mdx file');
25
+ expect($('#one').text()).to.equal('hello', 'Frontmatter included');
26
+ expect($('#url').text()).to.equal('/src/content/1.mdx', 'url is included');
27
+ expect($('#file').text()).to.contain(
28
+ 'fixtures/mdx-get-static-paths/src/content/1.mdx',
29
+ 'file is included'
30
+ );
31
+ });
32
+ });
@@ -10,7 +10,6 @@ describe('MDX Page', () => {
10
10
  before(async () => {
11
11
  fixture = await loadFixture({
12
12
  root: new URL('./fixtures/mdx-page/', import.meta.url),
13
- integrations: [mdx()],
14
13
  });
15
14
  });
16
15
 
@@ -0,0 +1,81 @@
1
+ import mdx from '@astrojs/mdx';
2
+ import { jsToTreeNode } from '../dist/utils.js';
3
+
4
+ import { expect } from 'chai';
5
+ import { parseHTML } from 'linkedom';
6
+ import getReadingTime from 'reading-time';
7
+ import { toString } from 'mdast-util-to-string';
8
+
9
+ import { loadFixture } from '../../../astro/test/test-utils.js';
10
+
11
+ export function rehypeReadingTime() {
12
+ return function (tree) {
13
+ const readingTime = getReadingTime(toString(tree));
14
+ tree.children.unshift(
15
+ jsToTreeNode(`export const readingTime = ${JSON.stringify(readingTime)}`)
16
+ );
17
+ };
18
+ }
19
+
20
+ const FIXTURE_ROOT = new URL('./fixtures/mdx-rehype-plugins/', import.meta.url);
21
+
22
+ describe('MDX rehype plugins', () => {
23
+ describe('without "extends"', () => {
24
+ let fixture;
25
+ before(async () => {
26
+ fixture = await loadFixture({
27
+ root: FIXTURE_ROOT,
28
+ integrations: [
29
+ mdx({
30
+ rehypePlugins: [rehypeReadingTime],
31
+ }),
32
+ ],
33
+ });
34
+ await fixture.build();
35
+ });
36
+
37
+ it('removes default getHeadings', async () => {
38
+ const html = await fixture.readFile('/space-ipsum/index.html');
39
+ const { document } = parseHTML(html);
40
+
41
+ const headings = [...document.querySelectorAll('h1, h2')];
42
+ expect(headings.length).to.be.greaterThan(0);
43
+ for (const heading of headings) {
44
+ expect(heading.id).to.be.empty;
45
+ }
46
+ });
47
+
48
+ it('supports custom rehype plugins - reading time', async () => {
49
+ const readingTime = JSON.parse(await fixture.readFile('/reading-time.json'));
50
+
51
+ expect(readingTime).to.not.be.null;
52
+ expect(readingTime.text).to.match(/^\d+ min read/);
53
+ });
54
+ });
55
+
56
+ describe('with "extends"', () => {
57
+ let fixture;
58
+ before(async () => {
59
+ fixture = await loadFixture({
60
+ root: FIXTURE_ROOT,
61
+ integrations: [
62
+ mdx({
63
+ rehypePlugins: { extends: [rehypeReadingTime] },
64
+ }),
65
+ ],
66
+ });
67
+ await fixture.build();
68
+ });
69
+
70
+ it('preserves default getHeadings', async () => {
71
+ const html = await fixture.readFile('/space-ipsum/index.html');
72
+ const { document } = parseHTML(html);
73
+
74
+ const headings = [...document.querySelectorAll('h1, h2')];
75
+ expect(headings.length).to.be.greaterThan(0);
76
+ for (const heading of headings) {
77
+ expect(heading.id).to.not.be.empty;
78
+ }
79
+ });
80
+ });
81
+ });