@astrojs/mdx 1.0.0-beta.2 → 1.0.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 (106) hide show
  1. package/README.md +83 -19
  2. package/dist/index.d.ts +4 -2
  3. package/dist/index.js +83 -35
  4. package/dist/plugins.d.ts +4 -5
  5. package/dist/plugins.js +36 -80
  6. package/dist/rehype-collect-headings.d.ts +1 -1
  7. package/dist/rehype-meta-string.js +2 -3
  8. package/dist/rehype-optimize-static.d.ts +11 -0
  9. package/dist/rehype-optimize-static.js +62 -0
  10. package/dist/remark-images-to-component.d.ts +2 -0
  11. package/dist/remark-images-to-component.js +83 -0
  12. package/dist/utils.d.ts +2 -1
  13. package/dist/utils.js +23 -15
  14. package/package.json +39 -30
  15. package/template/content-module-types.d.ts +9 -0
  16. package/.turbo/turbo-build.log +0 -5
  17. package/CHANGELOG.md +0 -469
  18. package/src/index.ts +0 -201
  19. package/src/plugins.ts +0 -246
  20. package/src/rehype-collect-headings.ts +0 -11
  21. package/src/rehype-meta-string.ts +0 -17
  22. package/src/remark-prism.ts +0 -18
  23. package/src/remark-shiki.ts +0 -95
  24. package/src/utils.ts +0 -102
  25. package/test/fixtures/mdx-astro-markdown-remarkRehype/src/pages/index.mdx +0 -5
  26. package/test/fixtures/mdx-component/src/components/Test.mdx +0 -3
  27. package/test/fixtures/mdx-component/src/components/WithFragment.mdx +0 -3
  28. package/test/fixtures/mdx-component/src/pages/glob.astro +0 -20
  29. package/test/fixtures/mdx-component/src/pages/index.astro +0 -5
  30. package/test/fixtures/mdx-component/src/pages/w-fragment.astro +0 -5
  31. package/test/fixtures/mdx-escape/src/components/Em.astro +0 -7
  32. package/test/fixtures/mdx-escape/src/components/P.astro +0 -1
  33. package/test/fixtures/mdx-escape/src/components/Title.astro +0 -1
  34. package/test/fixtures/mdx-escape/src/pages/html-tag.mdx +0 -5
  35. package/test/fixtures/mdx-escape/src/pages/index.mdx +0 -13
  36. package/test/fixtures/mdx-frontmatter/src/layouts/Base.astro +0 -38
  37. package/test/fixtures/mdx-frontmatter/src/pages/glob.json.js +0 -9
  38. package/test/fixtures/mdx-frontmatter/src/pages/index.mdx +0 -10
  39. package/test/fixtures/mdx-frontmatter/src/pages/with-headings.mdx +0 -7
  40. package/test/fixtures/mdx-frontmatter-injection/astro.config.mjs +0 -12
  41. package/test/fixtures/mdx-frontmatter-injection/node_modules/.bin/astro +0 -17
  42. package/test/fixtures/mdx-frontmatter-injection/package.json +0 -12
  43. package/test/fixtures/mdx-frontmatter-injection/src/layouts/Base.astro +0 -17
  44. package/test/fixtures/mdx-frontmatter-injection/src/markdown-plugins.mjs +0 -27
  45. package/test/fixtures/mdx-frontmatter-injection/src/pages/glob.json.js +0 -6
  46. package/test/fixtures/mdx-frontmatter-injection/src/pages/page-1.mdx +0 -8
  47. package/test/fixtures/mdx-frontmatter-injection/src/pages/page-2.mdx +0 -24
  48. package/test/fixtures/mdx-get-headings/src/pages/pages.json.js +0 -11
  49. package/test/fixtures/mdx-get-headings/src/pages/test-with-jsx-expressions.mdx +0 -8
  50. package/test/fixtures/mdx-get-headings/src/pages/test.mdx +0 -9
  51. package/test/fixtures/mdx-get-static-paths/src/content/1.mdx +0 -5
  52. package/test/fixtures/mdx-get-static-paths/src/pages/[slug].astro +0 -34
  53. package/test/fixtures/mdx-infinite-loop/astro.config.ts +0 -6
  54. package/test/fixtures/mdx-infinite-loop/node_modules/.bin/astro +0 -17
  55. package/test/fixtures/mdx-infinite-loop/package.json +0 -10
  56. package/test/fixtures/mdx-infinite-loop/src/components/Test.js +0 -3
  57. package/test/fixtures/mdx-infinite-loop/src/pages/doc.mdx +0 -6
  58. package/test/fixtures/mdx-infinite-loop/src/pages/index.astro +0 -5
  59. package/test/fixtures/mdx-namespace/astro.config.mjs +0 -6
  60. package/test/fixtures/mdx-namespace/node_modules/.bin/astro +0 -17
  61. package/test/fixtures/mdx-namespace/package.json +0 -10
  62. package/test/fixtures/mdx-namespace/src/components/Component.jsx +0 -6
  63. package/test/fixtures/mdx-namespace/src/pages/object.mdx +0 -3
  64. package/test/fixtures/mdx-namespace/src/pages/star.mdx +0 -3
  65. package/test/fixtures/mdx-page/astro.config.ts +0 -5
  66. package/test/fixtures/mdx-page/node_modules/.bin/astro +0 -17
  67. package/test/fixtures/mdx-page/package.json +0 -9
  68. package/test/fixtures/mdx-page/src/pages/index.mdx +0 -3
  69. package/test/fixtures/mdx-page/src/styles.css +0 -3
  70. package/test/fixtures/mdx-plugins/src/pages/with-plugins.mdx +0 -25
  71. package/test/fixtures/mdx-plus-react/astro.config.mjs +0 -6
  72. package/test/fixtures/mdx-plus-react/node_modules/.bin/astro +0 -17
  73. package/test/fixtures/mdx-plus-react/package.json +0 -10
  74. package/test/fixtures/mdx-plus-react/src/components/Component.jsx +0 -5
  75. package/test/fixtures/mdx-plus-react/src/pages/index.astro +0 -11
  76. package/test/fixtures/mdx-slots/src/components/Slotted.astro +0 -4
  77. package/test/fixtures/mdx-slots/src/components/Test.mdx +0 -15
  78. package/test/fixtures/mdx-slots/src/pages/glob.astro +0 -11
  79. package/test/fixtures/mdx-slots/src/pages/index.astro +0 -5
  80. package/test/fixtures/mdx-syntax-hightlighting/src/pages/index.mdx +0 -9
  81. package/test/fixtures/mdx-url-export/src/pages/pages.json.js +0 -9
  82. package/test/fixtures/mdx-url-export/src/pages/test-1.mdx +0 -1
  83. package/test/fixtures/mdx-url-export/src/pages/test-2.mdx +0 -1
  84. package/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx +0 -3
  85. package/test/fixtures/mdx-vite-env-vars/astro.config.mjs +0 -9
  86. package/test/fixtures/mdx-vite-env-vars/node_modules/.bin/astro +0 -17
  87. package/test/fixtures/mdx-vite-env-vars/package.json +0 -7
  88. package/test/fixtures/mdx-vite-env-vars/src/pages/frontmatter.json.js +0 -7
  89. package/test/fixtures/mdx-vite-env-vars/src/pages/vite-env-vars.mdx +0 -38
  90. package/test/mdx-astro-markdown-remarkRehype.test.js +0 -85
  91. package/test/mdx-component.test.js +0 -191
  92. package/test/mdx-escape.test.js +0 -32
  93. package/test/mdx-frontmatter-injection.test.js +0 -53
  94. package/test/mdx-frontmatter.test.js +0 -77
  95. package/test/mdx-get-headings.test.js +0 -151
  96. package/test/mdx-get-static-paths.test.js +0 -32
  97. package/test/mdx-infinite-loop.test.js +0 -30
  98. package/test/mdx-namespace.test.js +0 -83
  99. package/test/mdx-page.test.js +0 -64
  100. package/test/mdx-plugins.test.js +0 -250
  101. package/test/mdx-plus-react.test.js +0 -25
  102. package/test/mdx-slots.js +0 -124
  103. package/test/mdx-syntax-highlighting.test.js +0 -145
  104. package/test/mdx-url-export.test.js +0 -28
  105. package/test/mdx-vite-env-vars.test.js +0 -54
  106. package/tsconfig.json +0 -10
@@ -0,0 +1,62 @@
1
+ import { visit } from "estree-util-visit";
2
+ import { toHtml } from "hast-util-to-html";
3
+ const exportConstComponentsRe = /export\s+const\s+components\s*=/;
4
+ function rehypeOptimizeStatic(options) {
5
+ return (tree) => {
6
+ const customComponentNames = new Set(options?.customComponentNames);
7
+ for (const child of tree.children) {
8
+ if (child.type === "mdxjsEsm" && exportConstComponentsRe.test(child.value)) {
9
+ const objectPropertyNodes = child.data.estree.body[0]?.declarations?.[0]?.init?.properties;
10
+ if (objectPropertyNodes) {
11
+ for (const objectPropertyNode of objectPropertyNodes) {
12
+ const componentName = objectPropertyNode.key?.name ?? objectPropertyNode.key?.value;
13
+ if (componentName) {
14
+ customComponentNames.add(componentName);
15
+ }
16
+ }
17
+ }
18
+ }
19
+ }
20
+ const allPossibleElements = /* @__PURE__ */ new Set();
21
+ const elementStack = [];
22
+ visit(tree, {
23
+ enter(node) {
24
+ const isCustomComponent = node.tagName && customComponentNames.has(node.tagName);
25
+ if (node.type.startsWith("mdx") || isCustomComponent) {
26
+ for (const el of elementStack) {
27
+ allPossibleElements.delete(el);
28
+ }
29
+ elementStack.length = 0;
30
+ }
31
+ if (node.type === "element" || node.type === "mdxJsxFlowElement") {
32
+ elementStack.push(node);
33
+ allPossibleElements.add(node);
34
+ }
35
+ },
36
+ leave(node, _, __, parents) {
37
+ if (node.type === "element" || node.type === "mdxJsxFlowElement") {
38
+ elementStack.pop();
39
+ const parent = parents[parents.length - 1];
40
+ if (allPossibleElements.has(parent)) {
41
+ allPossibleElements.delete(node);
42
+ }
43
+ }
44
+ }
45
+ });
46
+ for (const el of allPossibleElements) {
47
+ if (el.type === "mdxJsxFlowElement") {
48
+ el.attributes.push({
49
+ type: "mdxJsxAttribute",
50
+ name: "set:html",
51
+ value: toHtml(el.children)
52
+ });
53
+ } else {
54
+ el.properties["set:html"] = toHtml(el.children);
55
+ }
56
+ el.children = [];
57
+ }
58
+ };
59
+ }
60
+ export {
61
+ rehypeOptimizeStatic
62
+ };
@@ -0,0 +1,2 @@
1
+ import type { MarkdownVFile } from '@astrojs/markdown-remark';
2
+ export declare function remarkImageToComponent(): (tree: any, file: MarkdownVFile) => void;
@@ -0,0 +1,83 @@
1
+ import { visit } from "unist-util-visit";
2
+ import { jsToTreeNode } from "./utils.js";
3
+ function remarkImageToComponent() {
4
+ return function(tree, file) {
5
+ if (!file.data.imagePaths)
6
+ return;
7
+ const importsStatements = [];
8
+ const importedImages = /* @__PURE__ */ new Map();
9
+ visit(tree, "image", (node, index, parent) => {
10
+ if (file.data.imagePaths?.has(node.url)) {
11
+ let importName = importedImages.get(node.url);
12
+ if (!importName) {
13
+ importName = `__${importedImages.size}_${node.url.replace(/\W/g, "_")}__`;
14
+ importsStatements.push({
15
+ type: "mdxjsEsm",
16
+ value: "",
17
+ data: {
18
+ estree: {
19
+ type: "Program",
20
+ sourceType: "module",
21
+ body: [
22
+ {
23
+ type: "ImportDeclaration",
24
+ source: { type: "Literal", value: node.url, raw: JSON.stringify(node.url) },
25
+ specifiers: [
26
+ {
27
+ type: "ImportDefaultSpecifier",
28
+ local: { type: "Identifier", name: importName }
29
+ }
30
+ ]
31
+ }
32
+ ]
33
+ }
34
+ }
35
+ });
36
+ importedImages.set(node.url, importName);
37
+ }
38
+ const componentElement = {
39
+ name: "__AstroImage__",
40
+ type: "mdxJsxFlowElement",
41
+ attributes: [
42
+ {
43
+ name: "src",
44
+ type: "mdxJsxAttribute",
45
+ value: {
46
+ type: "mdxJsxAttributeValueExpression",
47
+ value: importName,
48
+ data: {
49
+ estree: {
50
+ type: "Program",
51
+ sourceType: "module",
52
+ comments: [],
53
+ body: [
54
+ {
55
+ type: "ExpressionStatement",
56
+ expression: { type: "Identifier", name: importName }
57
+ }
58
+ ]
59
+ }
60
+ }
61
+ }
62
+ },
63
+ { name: "alt", type: "mdxJsxAttribute", value: node.alt || "" }
64
+ ],
65
+ children: []
66
+ };
67
+ if (node.title) {
68
+ componentElement.attributes.push({
69
+ type: "mdxJsxAttribute",
70
+ name: "title",
71
+ value: node.title
72
+ });
73
+ }
74
+ parent.children.splice(index, 1, componentElement);
75
+ }
76
+ });
77
+ tree.children.unshift(...importsStatements);
78
+ tree.children.unshift(jsToTreeNode(`import { Image as __AstroImage__ } from "astro:assets";`));
79
+ };
80
+ }
81
+ export {
82
+ remarkImageToComponent
83
+ };
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
1
2
  import type { Options as AcornOpts } from 'acorn';
2
3
  import type { AstroConfig } from 'astro';
3
4
  import matter from 'gray-matter';
@@ -14,5 +15,5 @@ export declare function getFileInfo(id: string, config: AstroConfig): FileInfo;
14
15
  */
15
16
  export declare function parseFrontmatter(code: string, id: string): matter.GrayMatterFile<string>;
16
17
  export declare function jsToTreeNode(jsString: string, acornOpts?: AcornOpts): MdxjsEsm;
17
- export declare function isRelativePath(path: string): boolean;
18
+ export declare function ignoreStringPlugins(plugins: any[]): PluggableList;
18
19
  export {};
package/dist/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { parse } from "acorn";
2
2
  import matter from "gray-matter";
3
+ import { bold, yellow } from "kleur/colors";
3
4
  function appendForwardSlash(path) {
4
5
  return path.endsWith("/") ? path : path + "/";
5
6
  }
@@ -17,7 +18,7 @@ function getFileInfo(id, config) {
17
18
  const isPage = fileId.includes("/pages/");
18
19
  if (isPage) {
19
20
  fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, "");
20
- } else if (url && url.pathname.startsWith(config.root.pathname)) {
21
+ } else if (url?.pathname.startsWith(config.root.pathname)) {
21
22
  fileUrl = url.pathname.slice(config.root.pathname.length);
22
23
  } else {
23
24
  fileUrl = fileId;
@@ -59,23 +60,30 @@ function jsToTreeNode(jsString, acornOpts = {
59
60
  }
60
61
  };
61
62
  }
62
- function isRelativePath(path) {
63
- return startsWithDotDotSlash(path) || startsWithDotSlash(path);
64
- }
65
- function startsWithDotDotSlash(path) {
66
- const c1 = path[0];
67
- const c2 = path[1];
68
- const c3 = path[2];
69
- return c1 === "." && c2 === "." && c3 === "/";
70
- }
71
- function startsWithDotSlash(path) {
72
- const c1 = path[0];
73
- const c2 = path[1];
74
- return c1 === "." && c2 === "/";
63
+ function ignoreStringPlugins(plugins) {
64
+ let validPlugins = [];
65
+ let hasInvalidPlugin = false;
66
+ for (const plugin of plugins) {
67
+ if (typeof plugin === "string") {
68
+ console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
69
+ hasInvalidPlugin = true;
70
+ } else if (Array.isArray(plugin) && typeof plugin[0] === "string") {
71
+ console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
72
+ hasInvalidPlugin = true;
73
+ } else {
74
+ validPlugins.push(plugin);
75
+ }
76
+ }
77
+ if (hasInvalidPlugin) {
78
+ console.warn(
79
+ `To inherit Markdown plugins in MDX, please use explicit imports in your config instead of "strings." See Markdown docs: https://docs.astro.build/en/guides/markdown-content/#markdown-plugins`
80
+ );
81
+ }
82
+ return validPlugins;
75
83
  }
76
84
  export {
77
85
  getFileInfo,
78
- isRelativePath,
86
+ ignoreStringPlugins,
79
87
  jsToTreeNode,
80
88
  parseFrontmatter
81
89
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/mdx",
3
- "description": "Use MDX within Astro",
4
- "version": "1.0.0-beta.2",
3
+ "description": "Add support for MDX pages in your Astro site",
4
+ "version": "1.0.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -22,49 +22,58 @@
22
22
  ".": "./dist/index.js",
23
23
  "./package.json": "./package.json"
24
24
  },
25
+ "files": [
26
+ "dist",
27
+ "template"
28
+ ],
25
29
  "dependencies": {
26
- "@astrojs/markdown-remark": "^2.0.0-beta.2",
27
- "@astrojs/prism": "^2.0.0-beta.0",
28
- "@mdx-js/mdx": "^2.1.2",
29
- "@mdx-js/rollup": "^2.1.1",
30
- "acorn": "^8.8.0",
31
- "es-module-lexer": "^0.10.5",
32
- "estree-util-visit": "^1.2.0",
33
- "github-slugger": "^1.4.0",
30
+ "@mdx-js/mdx": "^2.3.0",
31
+ "acorn": "^8.10.0",
32
+ "es-module-lexer": "^1.3.0",
33
+ "estree-util-visit": "^1.2.1",
34
+ "github-slugger": "^2.0.0",
34
35
  "gray-matter": "^4.0.3",
36
+ "hast-util-to-html": "^8.0.4",
35
37
  "kleur": "^4.1.4",
36
38
  "rehype-raw": "^6.1.1",
37
39
  "remark-frontmatter": "^4.0.1",
38
40
  "remark-gfm": "^3.0.1",
39
41
  "remark-smartypants": "^2.0.0",
40
- "shiki": "^0.11.1",
41
- "unist-util-visit": "^4.1.0",
42
- "vfile": "^5.3.2"
42
+ "shiki": "^0.14.3",
43
+ "source-map": "^0.7.4",
44
+ "unist-util-visit": "^4.1.2",
45
+ "vfile": "^5.3.7",
46
+ "@astrojs/markdown-remark": "3.0.0",
47
+ "@astrojs/prism": "3.0.0"
48
+ },
49
+ "peerDependencies": {
50
+ "astro": "^3.0.0"
43
51
  },
44
52
  "devDependencies": {
45
- "@types/chai": "^4.3.1",
46
- "@types/estree": "^1.0.0",
47
- "@types/github-slugger": "^1.3.0",
48
- "@types/mdast": "^3.0.10",
49
- "@types/mocha": "^9.1.1",
53
+ "@types/chai": "^4.3.5",
54
+ "@types/estree": "^1.0.1",
55
+ "@types/mdast": "^3.0.12",
56
+ "@types/mocha": "^10.0.1",
50
57
  "@types/yargs-parser": "^21.0.0",
51
- "astro": "2.0.0-beta.2",
52
- "astro-scripts": "0.0.10-beta.0",
53
- "chai": "^4.3.6",
54
- "cheerio": "^1.0.0-rc.11",
55
- "linkedom": "^0.14.12",
56
- "mdast-util-mdx": "^2.0.0",
57
- "mdast-util-to-string": "^3.1.0",
58
- "mocha": "^9.2.2",
58
+ "chai": "^4.3.7",
59
+ "cheerio": "1.0.0-rc.12",
60
+ "linkedom": "^0.15.1",
61
+ "mdast-util-mdx": "^2.0.1",
62
+ "mdast-util-to-string": "^3.2.0",
63
+ "mocha": "^10.2.0",
59
64
  "reading-time": "^1.5.0",
60
- "rehype-pretty-code": "^0.4.0",
65
+ "rehype-mathjax": "^4.0.3",
66
+ "rehype-pretty-code": "^0.10.0",
67
+ "remark-math": "^5.1.1",
61
68
  "remark-rehype": "^10.1.0",
62
- "remark-shiki-twoslash": "^3.1.0",
69
+ "remark-shiki-twoslash": "^3.1.3",
63
70
  "remark-toc": "^8.0.1",
64
- "vite": "^4.0.3"
71
+ "vite": "^4.4.9",
72
+ "astro": "3.0.0",
73
+ "astro-scripts": "0.0.14"
65
74
  },
66
75
  "engines": {
67
- "node": ">=16.12.0"
76
+ "node": ">=18.14.1"
68
77
  },
69
78
  "scripts": {
70
79
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",
@@ -0,0 +1,9 @@
1
+ declare module 'astro:content' {
2
+ interface Render {
3
+ '.mdx': Promise<{
4
+ Content: import('astro').MarkdownInstance<{}>['Content'];
5
+ headings: import('astro').MarkdownHeading[];
6
+ remarkPluginFrontmatter: Record<string, any>;
7
+ }>;
8
+ }
9
+ }
@@ -1,5 +0,0 @@
1
- @astrojs/mdx:build: cache hit, replaying output e3662492a73790a4
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@1.0.0-beta.2 build /home/runner/work/astro/astro/packages/integrations/mdx
4
- @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
- @astrojs/mdx:build: