@astrojs/mdx 0.11.0 → 0.11.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/.turbo/turbo-build.log +2 -2
- package/CHANGELOG.md +14 -0
- package/README.md +59 -8
- package/dist/index.d.ts +13 -1
- package/dist/index.js +21 -9
- package/dist/plugins.d.ts +11 -0
- package/dist/plugins.js +214 -0
- package/dist/utils.d.ts +0 -16
- package/dist/utils.js +0 -83
- package/package.json +5 -3
- package/src/index.ts +41 -9
- package/src/plugins.ts +273 -0
- package/src/utils.ts +0 -111
- package/test/fixtures/mdx-vite-env-vars/astro.config.mjs +9 -0
- package/test/fixtures/mdx-vite-env-vars/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-vite-env-vars/package.json +7 -0
- package/test/fixtures/mdx-vite-env-vars/src/pages/frontmatter.json.js +7 -0
- package/test/fixtures/mdx-vite-env-vars/src/pages/vite-env-vars.mdx +38 -0
- package/test/mdx-plugins.test.js +11 -0
- package/test/mdx-vite-env-vars.test.js +54 -0
- package/dist/astro-data-utils.d.ts +0 -9
- package/dist/astro-data-utils.js +0 -86
- package/src/astro-data-utils.ts +0 -103
package/dist/astro-data-utils.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { jsToTreeNode } from "./utils.js";
|
|
2
|
-
function remarkInitializeAstroData() {
|
|
3
|
-
return function(tree, vfile) {
|
|
4
|
-
if (!vfile.data.astro) {
|
|
5
|
-
vfile.data.astro = { frontmatter: {} };
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
const EXPORT_NAME = "frontmatter";
|
|
10
|
-
function rehypeApplyFrontmatterExport(pageFrontmatter) {
|
|
11
|
-
return function(tree, vfile) {
|
|
12
|
-
const { frontmatter: injectedFrontmatter } = safelyGetAstroData(vfile.data);
|
|
13
|
-
const frontmatter = { ...injectedFrontmatter, ...pageFrontmatter };
|
|
14
|
-
const exportNodes = [
|
|
15
|
-
jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`)
|
|
16
|
-
];
|
|
17
|
-
if (frontmatter.layout) {
|
|
18
|
-
exportNodes.unshift(
|
|
19
|
-
jsToTreeNode(
|
|
20
|
-
`import { jsx as layoutJsx } from 'astro/jsx-runtime';
|
|
21
|
-
|
|
22
|
-
export default async function ({ children }) {
|
|
23
|
-
const Layout = (await import(${JSON.stringify(frontmatter.layout)})).default;
|
|
24
|
-
const { layout, ...content } = frontmatter;
|
|
25
|
-
content.file = file;
|
|
26
|
-
content.url = url;
|
|
27
|
-
content.astro = {};
|
|
28
|
-
Object.defineProperty(content.astro, 'headings', {
|
|
29
|
-
get() {
|
|
30
|
-
throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
Object.defineProperty(content.astro, 'html', {
|
|
34
|
-
get() {
|
|
35
|
-
throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
Object.defineProperty(content.astro, 'source', {
|
|
39
|
-
get() {
|
|
40
|
-
throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
return layoutJsx(Layout, {
|
|
44
|
-
file,
|
|
45
|
-
url,
|
|
46
|
-
content,
|
|
47
|
-
frontmatter: content,
|
|
48
|
-
headings: getHeadings(),
|
|
49
|
-
'server:root': true,
|
|
50
|
-
children,
|
|
51
|
-
});
|
|
52
|
-
};`
|
|
53
|
-
)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
tree.children = exportNodes.concat(tree.children);
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function isValidAstroData(obj) {
|
|
60
|
-
if (typeof obj === "object" && obj !== null && obj.hasOwnProperty("frontmatter")) {
|
|
61
|
-
const { frontmatter } = obj;
|
|
62
|
-
try {
|
|
63
|
-
JSON.stringify(frontmatter);
|
|
64
|
-
} catch {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
return typeof frontmatter === "object" && frontmatter !== null;
|
|
68
|
-
}
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
function safelyGetAstroData(vfileData) {
|
|
72
|
-
const { astro } = vfileData;
|
|
73
|
-
if (!astro)
|
|
74
|
-
return { frontmatter: {} };
|
|
75
|
-
if (!isValidAstroData(astro)) {
|
|
76
|
-
throw Error(
|
|
77
|
-
`[MDX] A remark or rehype plugin tried to add invalid frontmatter. Ensure "astro.frontmatter" is a JSON object!`
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
return astro;
|
|
81
|
-
}
|
|
82
|
-
export {
|
|
83
|
-
rehypeApplyFrontmatterExport,
|
|
84
|
-
remarkInitializeAstroData,
|
|
85
|
-
safelyGetAstroData
|
|
86
|
-
};
|
package/src/astro-data-utils.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import type { MarkdownAstroData } from 'astro';
|
|
2
|
-
import type { Data, VFile } from 'vfile';
|
|
3
|
-
import { jsToTreeNode } from './utils.js';
|
|
4
|
-
|
|
5
|
-
export function remarkInitializeAstroData() {
|
|
6
|
-
return function (tree: any, vfile: VFile) {
|
|
7
|
-
if (!vfile.data.astro) {
|
|
8
|
-
vfile.data.astro = { frontmatter: {} };
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const EXPORT_NAME = 'frontmatter';
|
|
14
|
-
|
|
15
|
-
export function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any>) {
|
|
16
|
-
return function (tree: any, vfile: VFile) {
|
|
17
|
-
const { frontmatter: injectedFrontmatter } = safelyGetAstroData(vfile.data);
|
|
18
|
-
const frontmatter = { ...injectedFrontmatter, ...pageFrontmatter };
|
|
19
|
-
const exportNodes = [
|
|
20
|
-
jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`),
|
|
21
|
-
];
|
|
22
|
-
if (frontmatter.layout) {
|
|
23
|
-
// NOTE(bholmesdev) 08-22-2022
|
|
24
|
-
// Using an async layout import (i.e. `const Layout = (await import...)`)
|
|
25
|
-
// Preserves the dev server import cache when globbing a large set of MDX files
|
|
26
|
-
// Full explanation: 'https://github.com/withastro/astro/pull/4428'
|
|
27
|
-
exportNodes.unshift(
|
|
28
|
-
jsToTreeNode(
|
|
29
|
-
/** @see 'vite-plugin-markdown' for layout props reference */
|
|
30
|
-
`import { jsx as layoutJsx } from 'astro/jsx-runtime';
|
|
31
|
-
|
|
32
|
-
export default async function ({ children }) {
|
|
33
|
-
const Layout = (await import(${JSON.stringify(frontmatter.layout)})).default;
|
|
34
|
-
const { layout, ...content } = frontmatter;
|
|
35
|
-
content.file = file;
|
|
36
|
-
content.url = url;
|
|
37
|
-
content.astro = {};
|
|
38
|
-
Object.defineProperty(content.astro, 'headings', {
|
|
39
|
-
get() {
|
|
40
|
-
throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
Object.defineProperty(content.astro, 'html', {
|
|
44
|
-
get() {
|
|
45
|
-
throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(content.astro, 'source', {
|
|
49
|
-
get() {
|
|
50
|
-
throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return layoutJsx(Layout, {
|
|
54
|
-
file,
|
|
55
|
-
url,
|
|
56
|
-
content,
|
|
57
|
-
frontmatter: content,
|
|
58
|
-
headings: getHeadings(),
|
|
59
|
-
'server:root': true,
|
|
60
|
-
children,
|
|
61
|
-
});
|
|
62
|
-
};`
|
|
63
|
-
)
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
tree.children = exportNodes.concat(tree.children);
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Copied from markdown utils
|
|
72
|
-
* @see "vite-plugin-utils"
|
|
73
|
-
*/
|
|
74
|
-
function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
|
|
75
|
-
if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) {
|
|
76
|
-
const { frontmatter } = obj as any;
|
|
77
|
-
try {
|
|
78
|
-
// ensure frontmatter is JSON-serializable
|
|
79
|
-
JSON.stringify(frontmatter);
|
|
80
|
-
} catch {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
return typeof frontmatter === 'object' && frontmatter !== null;
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Copied from markdown utils
|
|
90
|
-
* @see "vite-plugin-utils"
|
|
91
|
-
*/
|
|
92
|
-
export function safelyGetAstroData(vfileData: Data): MarkdownAstroData {
|
|
93
|
-
const { astro } = vfileData;
|
|
94
|
-
|
|
95
|
-
if (!astro) return { frontmatter: {} };
|
|
96
|
-
if (!isValidAstroData(astro)) {
|
|
97
|
-
throw Error(
|
|
98
|
-
`[MDX] A remark or rehype plugin tried to add invalid frontmatter. Ensure "astro.frontmatter" is a JSON object!`
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return astro;
|
|
103
|
-
}
|