@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/src/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { compile as mdxCompile } from '@mdx-js/mdx';
|
|
2
|
+
import { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
|
2
3
|
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
3
4
|
import type { AstroIntegration } from 'astro';
|
|
4
5
|
import { parse as parseESM } from 'es-module-lexer';
|
|
5
6
|
import { blue, bold } from 'kleur/colors';
|
|
7
|
+
import fs from 'node:fs/promises';
|
|
6
8
|
import { VFile } from 'vfile';
|
|
7
9
|
import type { Plugin as VitePlugin } from 'vite';
|
|
8
|
-
import { rehypeApplyFrontmatterExport } from './astro-data-utils.js';
|
|
9
|
-
import type { MdxOptions } from './utils.js';
|
|
10
10
|
import {
|
|
11
|
-
getFileInfo,
|
|
12
11
|
getRehypePlugins,
|
|
13
12
|
getRemarkPlugins,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from './
|
|
13
|
+
recmaInjectImportMetaEnvPlugin,
|
|
14
|
+
rehypeApplyFrontmatterExport,
|
|
15
|
+
} from './plugins.js';
|
|
16
|
+
import { getFileInfo, handleExtendsNotSupported, parseFrontmatter } from './utils.js';
|
|
17
17
|
|
|
18
18
|
const RAW_CONTENT_ERROR =
|
|
19
19
|
'MDX does not support rawContent()! If you need to read the Markdown contents to calculate values (ex. reading time), we suggest injecting frontmatter via remark plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins';
|
|
@@ -21,6 +21,19 @@ const RAW_CONTENT_ERROR =
|
|
|
21
21
|
const COMPILED_CONTENT_ERROR =
|
|
22
22
|
'MDX does not support compiledContent()! If you need to read the HTML contents to calculate values (ex. reading time), we suggest injecting frontmatter via rehype plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins';
|
|
23
23
|
|
|
24
|
+
export type MdxOptions = {
|
|
25
|
+
remarkPlugins?: PluggableList;
|
|
26
|
+
rehypePlugins?: PluggableList;
|
|
27
|
+
/**
|
|
28
|
+
* Choose which remark and rehype plugins to inherit, if any.
|
|
29
|
+
*
|
|
30
|
+
* - "markdown" (default) - inherit your project’s markdown plugin config ([see Markdown docs](https://docs.astro.build/en/guides/markdown-content/#configuring-markdown))
|
|
31
|
+
* - "astroDefaults" - inherit Astro’s default plugins only ([see defaults](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins))
|
|
32
|
+
* - false - do not inherit any plugins
|
|
33
|
+
*/
|
|
34
|
+
extendPlugins?: 'markdown' | 'astroDefaults' | false;
|
|
35
|
+
};
|
|
36
|
+
|
|
24
37
|
export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
25
38
|
return {
|
|
26
39
|
name: '@astrojs/mdx',
|
|
@@ -58,17 +71,28 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
58
71
|
mdExtensions: [],
|
|
59
72
|
};
|
|
60
73
|
|
|
74
|
+
let importMetaEnv: Record<string, any> = {
|
|
75
|
+
SITE: config.site,
|
|
76
|
+
};
|
|
77
|
+
|
|
61
78
|
updateConfig({
|
|
62
79
|
vite: {
|
|
63
80
|
plugins: [
|
|
64
81
|
{
|
|
65
82
|
enforce: 'pre',
|
|
66
83
|
...mdxPlugin(mdxPluginOpts),
|
|
84
|
+
configResolved(resolved) {
|
|
85
|
+
importMetaEnv = { ...importMetaEnv, ...resolved.env };
|
|
86
|
+
},
|
|
67
87
|
// Override transform to alter code before MDX compilation
|
|
68
88
|
// ex. inject layouts
|
|
69
|
-
async transform(
|
|
89
|
+
async transform(_, id) {
|
|
70
90
|
if (!id.endsWith('mdx')) return;
|
|
71
91
|
|
|
92
|
+
// Read code from file manually to prevent Vite from parsing `import.meta.env` expressions
|
|
93
|
+
const { fileId } = getFileInfo(id, config);
|
|
94
|
+
const code = await fs.readFile(fileId, 'utf-8');
|
|
95
|
+
|
|
72
96
|
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
|
73
97
|
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
|
74
98
|
...mdxPluginOpts,
|
|
@@ -76,10 +100,11 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
76
100
|
...(mdxPluginOpts.rehypePlugins ?? []),
|
|
77
101
|
() => rehypeApplyFrontmatterExport(frontmatter),
|
|
78
102
|
],
|
|
103
|
+
recmaPlugins: [() => recmaInjectImportMetaEnvPlugin({ importMetaEnv })],
|
|
79
104
|
});
|
|
80
105
|
|
|
81
106
|
return {
|
|
82
|
-
code: String(compiled.value),
|
|
107
|
+
code: escapeViteEnvReferences(String(compiled.value)),
|
|
83
108
|
map: compiled.map,
|
|
84
109
|
};
|
|
85
110
|
},
|
|
@@ -123,7 +148,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
123
148
|
import.meta.hot.decline();
|
|
124
149
|
}`;
|
|
125
150
|
}
|
|
126
|
-
return code;
|
|
151
|
+
return escapeViteEnvReferences(code);
|
|
127
152
|
},
|
|
128
153
|
},
|
|
129
154
|
] as VitePlugin[],
|
|
@@ -133,3 +158,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
133
158
|
},
|
|
134
159
|
};
|
|
135
160
|
}
|
|
161
|
+
|
|
162
|
+
// Converts the first dot in `import.meta.env` to its Unicode escape sequence,
|
|
163
|
+
// which prevents Vite from replacing strings like `import.meta.env.SITE`
|
|
164
|
+
// in our JS representation of loaded Markdown files
|
|
165
|
+
function escapeViteEnvReferences(code: string) {
|
|
166
|
+
return code.replace(/import\.meta\.env/g, 'import\\u002Emeta.env');
|
|
167
|
+
}
|
package/src/plugins.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { nodeTypes } from '@mdx-js/mdx';
|
|
2
|
+
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
|
3
|
+
import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
4
|
+
import type { AstroConfig, MarkdownAstroData } from 'astro';
|
|
5
|
+
import type { Literal, MemberExpression } from 'estree';
|
|
6
|
+
import { visit as estreeVisit } from 'estree-util-visit';
|
|
7
|
+
import { bold, yellow } from 'kleur/colors';
|
|
8
|
+
import rehypeRaw from 'rehype-raw';
|
|
9
|
+
import remarkGfm from 'remark-gfm';
|
|
10
|
+
import remarkSmartypants from 'remark-smartypants';
|
|
11
|
+
import type { Data, VFile } from 'vfile';
|
|
12
|
+
import { MdxOptions } from './index.js';
|
|
13
|
+
import rehypeCollectHeadings from './rehype-collect-headings.js';
|
|
14
|
+
import remarkPrism from './remark-prism.js';
|
|
15
|
+
import remarkShiki from './remark-shiki.js';
|
|
16
|
+
import { jsToTreeNode } from './utils.js';
|
|
17
|
+
|
|
18
|
+
export function recmaInjectImportMetaEnvPlugin({
|
|
19
|
+
importMetaEnv,
|
|
20
|
+
}: {
|
|
21
|
+
importMetaEnv: Record<string, any>;
|
|
22
|
+
}) {
|
|
23
|
+
return (tree: any) => {
|
|
24
|
+
estreeVisit(tree, (node) => {
|
|
25
|
+
if (node.type === 'MemberExpression') {
|
|
26
|
+
// attempt to get "import.meta.env" variable name
|
|
27
|
+
const envVarName = getImportMetaEnvVariableName(node as MemberExpression);
|
|
28
|
+
if (typeof envVarName === 'string') {
|
|
29
|
+
// clear object keys to replace with envVarLiteral
|
|
30
|
+
for (const key in node) {
|
|
31
|
+
delete (node as any)[key];
|
|
32
|
+
}
|
|
33
|
+
const envVarLiteral: Literal = {
|
|
34
|
+
type: 'Literal',
|
|
35
|
+
value: importMetaEnv[envVarName],
|
|
36
|
+
raw: JSON.stringify(importMetaEnv[envVarName]),
|
|
37
|
+
};
|
|
38
|
+
Object.assign(node, envVarLiteral);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function remarkInitializeAstroData() {
|
|
46
|
+
return function (tree: any, vfile: VFile) {
|
|
47
|
+
if (!vfile.data.astro) {
|
|
48
|
+
vfile.data.astro = { frontmatter: {} };
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const EXPORT_NAME = 'frontmatter';
|
|
54
|
+
|
|
55
|
+
export function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any>) {
|
|
56
|
+
return function (tree: any, vfile: VFile) {
|
|
57
|
+
const { frontmatter: injectedFrontmatter } = safelyGetAstroData(vfile.data);
|
|
58
|
+
const frontmatter = { ...injectedFrontmatter, ...pageFrontmatter };
|
|
59
|
+
const exportNodes = [
|
|
60
|
+
jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`),
|
|
61
|
+
];
|
|
62
|
+
if (frontmatter.layout) {
|
|
63
|
+
// NOTE(bholmesdev) 08-22-2022
|
|
64
|
+
// Using an async layout import (i.e. `const Layout = (await import...)`)
|
|
65
|
+
// Preserves the dev server import cache when globbing a large set of MDX files
|
|
66
|
+
// Full explanation: 'https://github.com/withastro/astro/pull/4428'
|
|
67
|
+
exportNodes.unshift(
|
|
68
|
+
jsToTreeNode(
|
|
69
|
+
/** @see 'vite-plugin-markdown' for layout props reference */
|
|
70
|
+
`import { jsx as layoutJsx } from 'astro/jsx-runtime';
|
|
71
|
+
|
|
72
|
+
export default async function ({ children }) {
|
|
73
|
+
const Layout = (await import(${JSON.stringify(frontmatter.layout)})).default;
|
|
74
|
+
const { layout, ...content } = frontmatter;
|
|
75
|
+
content.file = file;
|
|
76
|
+
content.url = url;
|
|
77
|
+
content.astro = {};
|
|
78
|
+
Object.defineProperty(content.astro, 'headings', {
|
|
79
|
+
get() {
|
|
80
|
+
throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(content.astro, 'html', {
|
|
84
|
+
get() {
|
|
85
|
+
throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
Object.defineProperty(content.astro, 'source', {
|
|
89
|
+
get() {
|
|
90
|
+
throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return layoutJsx(Layout, {
|
|
94
|
+
file,
|
|
95
|
+
url,
|
|
96
|
+
content,
|
|
97
|
+
frontmatter: content,
|
|
98
|
+
headings: getHeadings(),
|
|
99
|
+
'server:root': true,
|
|
100
|
+
children,
|
|
101
|
+
});
|
|
102
|
+
};`
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
tree.children = exportNodes.concat(tree.children);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const DEFAULT_REMARK_PLUGINS: PluggableList = [remarkGfm, remarkSmartypants];
|
|
111
|
+
const DEFAULT_REHYPE_PLUGINS: PluggableList = [];
|
|
112
|
+
|
|
113
|
+
export async function getRemarkPlugins(
|
|
114
|
+
mdxOptions: MdxOptions,
|
|
115
|
+
config: AstroConfig
|
|
116
|
+
): Promise<MdxRollupPluginOptions['remarkPlugins']> {
|
|
117
|
+
let remarkPlugins: PluggableList = [
|
|
118
|
+
// Set "vfile.data.astro" for plugins to inject frontmatter
|
|
119
|
+
remarkInitializeAstroData,
|
|
120
|
+
];
|
|
121
|
+
switch (mdxOptions.extendPlugins) {
|
|
122
|
+
case false:
|
|
123
|
+
break;
|
|
124
|
+
case 'astroDefaults':
|
|
125
|
+
remarkPlugins = [...remarkPlugins, ...DEFAULT_REMARK_PLUGINS];
|
|
126
|
+
break;
|
|
127
|
+
default:
|
|
128
|
+
remarkPlugins = [
|
|
129
|
+
...remarkPlugins,
|
|
130
|
+
...(markdownShouldExtendDefaultPlugins(config) ? DEFAULT_REMARK_PLUGINS : []),
|
|
131
|
+
...ignoreStringPlugins(config.markdown.remarkPlugins ?? []),
|
|
132
|
+
];
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
if (config.markdown.syntaxHighlight === 'shiki') {
|
|
136
|
+
remarkPlugins.push([await remarkShiki(config.markdown.shikiConfig)]);
|
|
137
|
+
}
|
|
138
|
+
if (config.markdown.syntaxHighlight === 'prism') {
|
|
139
|
+
remarkPlugins.push(remarkPrism);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
remarkPlugins = [...remarkPlugins, ...(mdxOptions.remarkPlugins ?? [])];
|
|
143
|
+
return remarkPlugins;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getRehypePlugins(
|
|
147
|
+
mdxOptions: MdxOptions,
|
|
148
|
+
config: AstroConfig
|
|
149
|
+
): MdxRollupPluginOptions['rehypePlugins'] {
|
|
150
|
+
let rehypePlugins: PluggableList = [
|
|
151
|
+
// getHeadings() is guaranteed by TS, so we can't allow user to override
|
|
152
|
+
rehypeCollectHeadings,
|
|
153
|
+
// rehypeRaw allows custom syntax highlighters to work without added config
|
|
154
|
+
[rehypeRaw, { passThrough: nodeTypes }] as any,
|
|
155
|
+
];
|
|
156
|
+
switch (mdxOptions.extendPlugins) {
|
|
157
|
+
case false:
|
|
158
|
+
break;
|
|
159
|
+
case 'astroDefaults':
|
|
160
|
+
rehypePlugins = [...rehypePlugins, ...DEFAULT_REHYPE_PLUGINS];
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
rehypePlugins = [
|
|
164
|
+
...rehypePlugins,
|
|
165
|
+
...(markdownShouldExtendDefaultPlugins(config) ? DEFAULT_REHYPE_PLUGINS : []),
|
|
166
|
+
...ignoreStringPlugins(config.markdown.rehypePlugins ?? []),
|
|
167
|
+
];
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
rehypePlugins = [...rehypePlugins, ...(mdxOptions.rehypePlugins ?? [])];
|
|
172
|
+
return rehypePlugins;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function markdownShouldExtendDefaultPlugins(config: AstroConfig): boolean {
|
|
176
|
+
return (
|
|
177
|
+
config.markdown.extendDefaultPlugins ||
|
|
178
|
+
(config.markdown.remarkPlugins.length === 0 && config.markdown.rehypePlugins.length === 0)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function ignoreStringPlugins(plugins: any[]) {
|
|
183
|
+
let validPlugins: PluggableList = [];
|
|
184
|
+
let hasInvalidPlugin = false;
|
|
185
|
+
for (const plugin of plugins) {
|
|
186
|
+
if (typeof plugin === 'string') {
|
|
187
|
+
console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
|
|
188
|
+
hasInvalidPlugin = true;
|
|
189
|
+
} else if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
|
|
190
|
+
console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
|
|
191
|
+
hasInvalidPlugin = true;
|
|
192
|
+
} else {
|
|
193
|
+
validPlugins.push(plugin);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (hasInvalidPlugin) {
|
|
197
|
+
console.warn(
|
|
198
|
+
`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`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return validPlugins;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Copied from markdown utils
|
|
206
|
+
* @see "vite-plugin-utils"
|
|
207
|
+
*/
|
|
208
|
+
function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
|
|
209
|
+
if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) {
|
|
210
|
+
const { frontmatter } = obj as any;
|
|
211
|
+
try {
|
|
212
|
+
// ensure frontmatter is JSON-serializable
|
|
213
|
+
JSON.stringify(frontmatter);
|
|
214
|
+
} catch {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
return typeof frontmatter === 'object' && frontmatter !== null;
|
|
218
|
+
}
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Copied from markdown utils
|
|
224
|
+
* @see "vite-plugin-utils"
|
|
225
|
+
*/
|
|
226
|
+
function safelyGetAstroData(vfileData: Data): MarkdownAstroData {
|
|
227
|
+
const { astro } = vfileData;
|
|
228
|
+
|
|
229
|
+
if (!astro) return { frontmatter: {} };
|
|
230
|
+
if (!isValidAstroData(astro)) {
|
|
231
|
+
throw Error(
|
|
232
|
+
`[MDX] A remark or rehype plugin tried to add invalid frontmatter. Ensure "astro.frontmatter" is a JSON object!`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return astro;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Check if estree entry is "import.meta.env.VARIABLE"
|
|
241
|
+
* If it is, return the variable name (i.e. "VARIABLE")
|
|
242
|
+
*/
|
|
243
|
+
function getImportMetaEnvVariableName(node: MemberExpression): string | Error {
|
|
244
|
+
try {
|
|
245
|
+
// check for ".[ANYTHING]"
|
|
246
|
+
if (node.object.type !== 'MemberExpression' || node.property.type !== 'Identifier')
|
|
247
|
+
return new Error();
|
|
248
|
+
|
|
249
|
+
const nestedExpression = node.object;
|
|
250
|
+
// check for ".env"
|
|
251
|
+
if (nestedExpression.property.type !== 'Identifier' || nestedExpression.property.name !== 'env')
|
|
252
|
+
return new Error();
|
|
253
|
+
|
|
254
|
+
const envExpression = nestedExpression.object;
|
|
255
|
+
// check for ".meta"
|
|
256
|
+
if (
|
|
257
|
+
envExpression.type !== 'MetaProperty' ||
|
|
258
|
+
envExpression.property.type !== 'Identifier' ||
|
|
259
|
+
envExpression.property.name !== 'meta'
|
|
260
|
+
)
|
|
261
|
+
return new Error();
|
|
262
|
+
|
|
263
|
+
// check for "import"
|
|
264
|
+
if (envExpression.meta.name !== 'import') return new Error();
|
|
265
|
+
|
|
266
|
+
return node.property.name;
|
|
267
|
+
} catch (e) {
|
|
268
|
+
if (e instanceof Error) {
|
|
269
|
+
return e;
|
|
270
|
+
}
|
|
271
|
+
return new Error('Unknown parsing error');
|
|
272
|
+
}
|
|
273
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,32 +1,8 @@
|
|
|
1
|
-
import { nodeTypes } from '@mdx-js/mdx';
|
|
2
|
-
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
|
3
|
-
import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
4
1
|
import type { Options as AcornOpts } from 'acorn';
|
|
5
2
|
import { parse } from 'acorn';
|
|
6
3
|
import type { AstroConfig, SSRError } from 'astro';
|
|
7
4
|
import matter from 'gray-matter';
|
|
8
|
-
import { bold, yellow } from 'kleur/colors';
|
|
9
5
|
import type { MdxjsEsm } from 'mdast-util-mdx';
|
|
10
|
-
import rehypeRaw from 'rehype-raw';
|
|
11
|
-
import remarkGfm from 'remark-gfm';
|
|
12
|
-
import remarkSmartypants from 'remark-smartypants';
|
|
13
|
-
import { remarkInitializeAstroData } from './astro-data-utils.js';
|
|
14
|
-
import rehypeCollectHeadings from './rehype-collect-headings.js';
|
|
15
|
-
import remarkPrism from './remark-prism.js';
|
|
16
|
-
import remarkShiki from './remark-shiki.js';
|
|
17
|
-
|
|
18
|
-
export type MdxOptions = {
|
|
19
|
-
remarkPlugins?: PluggableList;
|
|
20
|
-
rehypePlugins?: PluggableList;
|
|
21
|
-
/**
|
|
22
|
-
* Choose which remark and rehype plugins to inherit, if any.
|
|
23
|
-
*
|
|
24
|
-
* - "markdown" (default) - inherit your project’s markdown plugin config ([see Markdown docs](https://docs.astro.build/en/guides/markdown-content/#configuring-markdown))
|
|
25
|
-
* - "astroDefaults" - inherit Astro’s default plugins only ([see defaults](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins))
|
|
26
|
-
* - false - do not inherit any plugins
|
|
27
|
-
*/
|
|
28
|
-
extendPlugins?: 'markdown' | 'astroDefaults' | false;
|
|
29
|
-
};
|
|
30
6
|
|
|
31
7
|
function appendForwardSlash(path: string) {
|
|
32
8
|
return path.endsWith('/') ? path : path + '/';
|
|
@@ -37,9 +13,6 @@ interface FileInfo {
|
|
|
37
13
|
fileUrl: string;
|
|
38
14
|
}
|
|
39
15
|
|
|
40
|
-
const DEFAULT_REMARK_PLUGINS: PluggableList = [remarkGfm, remarkSmartypants];
|
|
41
|
-
const DEFAULT_REHYPE_PLUGINS: PluggableList = [];
|
|
42
|
-
|
|
43
16
|
/** @see 'vite-plugin-utils' for source */
|
|
44
17
|
export function getFileInfo(id: string, config: AstroConfig): FileInfo {
|
|
45
18
|
const sitePathname = appendForwardSlash(
|
|
@@ -110,90 +83,6 @@ export function jsToTreeNode(
|
|
|
110
83
|
};
|
|
111
84
|
}
|
|
112
85
|
|
|
113
|
-
export async function getRemarkPlugins(
|
|
114
|
-
mdxOptions: MdxOptions,
|
|
115
|
-
config: AstroConfig
|
|
116
|
-
): Promise<MdxRollupPluginOptions['remarkPlugins']> {
|
|
117
|
-
let remarkPlugins: PluggableList = [
|
|
118
|
-
// Set "vfile.data.astro" for plugins to inject frontmatter
|
|
119
|
-
remarkInitializeAstroData,
|
|
120
|
-
];
|
|
121
|
-
switch (mdxOptions.extendPlugins) {
|
|
122
|
-
case false:
|
|
123
|
-
break;
|
|
124
|
-
case 'astroDefaults':
|
|
125
|
-
remarkPlugins = [...remarkPlugins, ...DEFAULT_REMARK_PLUGINS];
|
|
126
|
-
break;
|
|
127
|
-
default:
|
|
128
|
-
remarkPlugins = [
|
|
129
|
-
...remarkPlugins,
|
|
130
|
-
...(config.markdown.extendDefaultPlugins ? DEFAULT_REMARK_PLUGINS : []),
|
|
131
|
-
...ignoreStringPlugins(config.markdown.remarkPlugins ?? []),
|
|
132
|
-
];
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
if (config.markdown.syntaxHighlight === 'shiki') {
|
|
136
|
-
remarkPlugins.push([await remarkShiki(config.markdown.shikiConfig)]);
|
|
137
|
-
}
|
|
138
|
-
if (config.markdown.syntaxHighlight === 'prism') {
|
|
139
|
-
remarkPlugins.push(remarkPrism);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
remarkPlugins = [...remarkPlugins, ...(mdxOptions.remarkPlugins ?? [])];
|
|
143
|
-
return remarkPlugins;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function getRehypePlugins(
|
|
147
|
-
mdxOptions: MdxOptions,
|
|
148
|
-
config: AstroConfig
|
|
149
|
-
): MdxRollupPluginOptions['rehypePlugins'] {
|
|
150
|
-
let rehypePlugins: PluggableList = [
|
|
151
|
-
// getHeadings() is guaranteed by TS, so we can't allow user to override
|
|
152
|
-
rehypeCollectHeadings,
|
|
153
|
-
// rehypeRaw allows custom syntax highlighters to work without added config
|
|
154
|
-
[rehypeRaw, { passThrough: nodeTypes }] as any,
|
|
155
|
-
];
|
|
156
|
-
switch (mdxOptions.extendPlugins) {
|
|
157
|
-
case false:
|
|
158
|
-
break;
|
|
159
|
-
case 'astroDefaults':
|
|
160
|
-
rehypePlugins = [...rehypePlugins, ...DEFAULT_REHYPE_PLUGINS];
|
|
161
|
-
break;
|
|
162
|
-
default:
|
|
163
|
-
rehypePlugins = [
|
|
164
|
-
...rehypePlugins,
|
|
165
|
-
...(config.markdown.extendDefaultPlugins ? DEFAULT_REHYPE_PLUGINS : []),
|
|
166
|
-
...ignoreStringPlugins(config.markdown.rehypePlugins ?? []),
|
|
167
|
-
];
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
rehypePlugins = [...rehypePlugins, ...(mdxOptions.rehypePlugins ?? [])];
|
|
172
|
-
return rehypePlugins;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function ignoreStringPlugins(plugins: any[]) {
|
|
176
|
-
let validPlugins: PluggableList = [];
|
|
177
|
-
let hasInvalidPlugin = false;
|
|
178
|
-
for (const plugin of plugins) {
|
|
179
|
-
if (typeof plugin === 'string') {
|
|
180
|
-
console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
|
|
181
|
-
hasInvalidPlugin = true;
|
|
182
|
-
} else if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
|
|
183
|
-
console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
|
|
184
|
-
hasInvalidPlugin = true;
|
|
185
|
-
} else {
|
|
186
|
-
validPlugins.push(plugin);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (hasInvalidPlugin) {
|
|
190
|
-
console.warn(
|
|
191
|
-
`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`
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
return validPlugins;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
86
|
// TODO: remove for 1.0
|
|
198
87
|
export function handleExtendsNotSupported(pluginConfig: any) {
|
|
199
88
|
if (
|
|
@@ -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,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Let's talk about my import.meta.env.SITE
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
export const modeWorks =
|
|
6
|
+
import.meta.env.MODE === 'production' ? 'MODE works' : 'MODE does not work!';
|
|
7
|
+
|
|
8
|
+
# About my import.meta.env.SITE
|
|
9
|
+
|
|
10
|
+
My `import.meta.env.SITE` is so cool, I can put env variables in code!
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
const site = import.meta.env.SITE;
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## But I can use import.meta.env properly too
|
|
17
|
+
|
|
18
|
+
<div data-env-site>
|
|
19
|
+
|
|
20
|
+
I can compute my site, for example: {new URL('/blog/cool-post', import.meta.env.SITE)}
|
|
21
|
+
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
<div data-env-variable-exports>
|
|
25
|
+
|
|
26
|
+
I can also use `import.meta.env` in variable exports: {modeWorks}
|
|
27
|
+
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
I can also use vars as HTML attributes:
|
|
31
|
+
|
|
32
|
+
<div
|
|
33
|
+
data-env-dump
|
|
34
|
+
data-env-prod={import.meta.env.PROD}
|
|
35
|
+
data-env-dev={import.meta.env.DEV}
|
|
36
|
+
data-env-base-url={import.meta.env.BASE_URL}
|
|
37
|
+
data-env-mode={import.meta.env.MODE}
|
|
38
|
+
></div>
|
package/test/mdx-plugins.test.js
CHANGED
|
@@ -24,6 +24,17 @@ describe('MDX plugins', () => {
|
|
|
24
24
|
expect(selectTocLink(document)).to.not.be.null;
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
+
it('Applies GFM by default', async () => {
|
|
28
|
+
const fixture = await buildFixture({
|
|
29
|
+
integrations: [mdx()],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const html = await fixture.readFile(FILE);
|
|
33
|
+
const { document } = parseHTML(html);
|
|
34
|
+
|
|
35
|
+
expect(selectGfmLink(document)).to.not.be.null;
|
|
36
|
+
});
|
|
37
|
+
|
|
27
38
|
it('supports custom rehype plugins', async () => {
|
|
28
39
|
const fixture = await buildFixture({
|
|
29
40
|
integrations: [
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { parseHTML } from 'linkedom';
|
|
3
|
+
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
4
|
+
|
|
5
|
+
describe('MDX - Vite env vars', () => {
|
|
6
|
+
let fixture;
|
|
7
|
+
before(async () => {
|
|
8
|
+
fixture = await loadFixture({
|
|
9
|
+
root: new URL('./fixtures/mdx-vite-env-vars/', import.meta.url),
|
|
10
|
+
});
|
|
11
|
+
await fixture.build();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('Avoids transforming `import.meta.env` outside JSX expressions', async () => {
|
|
15
|
+
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
16
|
+
const { document } = parseHTML(html);
|
|
17
|
+
|
|
18
|
+
expect(document.querySelector('h1')?.innerHTML).to.contain('import.meta.env.SITE');
|
|
19
|
+
expect(document.querySelector('code')?.innerHTML).to.contain('import.meta.env.SITE');
|
|
20
|
+
expect(document.querySelector('pre')?.innerHTML).to.contain('import.meta.env.SITE');
|
|
21
|
+
});
|
|
22
|
+
it('Allows referencing `import.meta.env` in frontmatter', async () => {
|
|
23
|
+
const { title = '' } = JSON.parse(await fixture.readFile('/frontmatter.json'));
|
|
24
|
+
expect(title).to.contain('import.meta.env.SITE');
|
|
25
|
+
});
|
|
26
|
+
it('Transforms `import.meta.env` in {JSX expressions}', async () => {
|
|
27
|
+
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
28
|
+
const { document } = parseHTML(html);
|
|
29
|
+
|
|
30
|
+
expect(document.querySelector('[data-env-site]')?.innerHTML).to.contain(
|
|
31
|
+
'https://mdx-is-neat.com/blog/cool-post'
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
it('Transforms `import.meta.env` in variable exports', async () => {
|
|
35
|
+
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
36
|
+
const { document } = parseHTML(html);
|
|
37
|
+
|
|
38
|
+
expect(document.querySelector('[data-env-variable-exports]')?.innerHTML).to.contain(
|
|
39
|
+
'MODE works'
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
it('Transforms `import.meta.env` in HTML attributes', async () => {
|
|
43
|
+
const html = await fixture.readFile('/vite-env-vars/index.html');
|
|
44
|
+
const { document } = parseHTML(html);
|
|
45
|
+
|
|
46
|
+
const dataAttrDump = document.querySelector('[data-env-dump]');
|
|
47
|
+
expect(dataAttrDump).to.not.be.null;
|
|
48
|
+
|
|
49
|
+
expect(dataAttrDump.getAttribute('data-env-prod')).to.not.be.null;
|
|
50
|
+
expect(dataAttrDump.getAttribute('data-env-dev')).to.be.null;
|
|
51
|
+
expect(dataAttrDump.getAttribute('data-env-base-url')).to.equal('/');
|
|
52
|
+
expect(dataAttrDump.getAttribute('data-env-mode')).to.equal('production');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { MarkdownAstroData } from 'astro';
|
|
2
|
-
import type { Data, VFile } from 'vfile';
|
|
3
|
-
export declare function remarkInitializeAstroData(): (tree: any, vfile: VFile) => void;
|
|
4
|
-
export declare function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any>): (tree: any, vfile: VFile) => void;
|
|
5
|
-
/**
|
|
6
|
-
* Copied from markdown utils
|
|
7
|
-
* @see "vite-plugin-utils"
|
|
8
|
-
*/
|
|
9
|
-
export declare function safelyGetAstroData(vfileData: Data): MarkdownAstroData;
|