@astrojs/mdx 0.11.1 → 0.11.3

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.
@@ -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;
@@ -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
- };
@@ -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
- }