@astrojs/mdx 0.0.0-imgcache-20220929145446
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 +5 -0
- package/CHANGELOG.md +209 -0
- package/LICENSE +61 -0
- package/README.md +527 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +131 -0
- package/dist/plugins.d.ts +11 -0
- package/dist/plugins.js +214 -0
- package/dist/rehype-collect-headings.d.ts +6 -0
- package/dist/rehype-collect-headings.js +47 -0
- package/dist/remark-prism.d.ts +2 -0
- package/dist/remark-prism.js +17 -0
- package/dist/remark-shiki.d.ts +3 -0
- package/dist/remark-shiki.js +69 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.js +74 -0
- package/package.json +71 -0
- package/src/index.ts +167 -0
- package/src/plugins.ts +273 -0
- package/src/rehype-collect-headings.ts +50 -0
- package/src/remark-prism.ts +18 -0
- package/src/remark-shiki.ts +95 -0
- package/src/utils.ts +97 -0
- package/test/fixtures/mdx-component/src/components/Test.mdx +3 -0
- package/test/fixtures/mdx-component/src/pages/glob.astro +11 -0
- package/test/fixtures/mdx-component/src/pages/index.astro +5 -0
- package/test/fixtures/mdx-escape/src/components/Em.astro +7 -0
- package/test/fixtures/mdx-escape/src/components/P.astro +1 -0
- package/test/fixtures/mdx-escape/src/components/Title.astro +1 -0
- package/test/fixtures/mdx-escape/src/pages/html-tag.mdx +5 -0
- package/test/fixtures/mdx-escape/src/pages/index.mdx +13 -0
- package/test/fixtures/mdx-frontmatter/src/layouts/Base.astro +38 -0
- package/test/fixtures/mdx-frontmatter/src/pages/glob.json.js +9 -0
- package/test/fixtures/mdx-frontmatter/src/pages/index.mdx +10 -0
- package/test/fixtures/mdx-frontmatter/src/pages/with-headings.mdx +7 -0
- package/test/fixtures/mdx-frontmatter-injection/astro.config.mjs +12 -0
- package/test/fixtures/mdx-frontmatter-injection/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-frontmatter-injection/package.json +12 -0
- package/test/fixtures/mdx-frontmatter-injection/src/layouts/Base.astro +17 -0
- package/test/fixtures/mdx-frontmatter-injection/src/markdown-plugins.mjs +20 -0
- package/test/fixtures/mdx-frontmatter-injection/src/pages/glob.json.js +6 -0
- package/test/fixtures/mdx-frontmatter-injection/src/pages/page-1.mdx +7 -0
- package/test/fixtures/mdx-frontmatter-injection/src/pages/page-2.mdx +23 -0
- package/test/fixtures/mdx-frontmatter-injection/src/pages/with-overrides.mdx +7 -0
- package/test/fixtures/mdx-get-headings/src/pages/pages.json.js +11 -0
- package/test/fixtures/mdx-get-headings/src/pages/test-with-jsx-expressions.mdx +8 -0
- package/test/fixtures/mdx-get-headings/src/pages/test.mdx +9 -0
- package/test/fixtures/mdx-get-static-paths/src/content/1.mdx +5 -0
- package/test/fixtures/mdx-get-static-paths/src/pages/[slug].astro +34 -0
- package/test/fixtures/mdx-namespace/astro.config.mjs +6 -0
- package/test/fixtures/mdx-namespace/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-namespace/package.json +10 -0
- package/test/fixtures/mdx-namespace/src/components/Component.jsx +6 -0
- package/test/fixtures/mdx-namespace/src/pages/object.mdx +3 -0
- package/test/fixtures/mdx-namespace/src/pages/star.mdx +3 -0
- package/test/fixtures/mdx-page/astro.config.ts +5 -0
- package/test/fixtures/mdx-page/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-page/package.json +9 -0
- package/test/fixtures/mdx-page/src/pages/index.mdx +3 -0
- package/test/fixtures/mdx-page/src/styles.css +3 -0
- package/test/fixtures/mdx-plugins/src/pages/with-plugins.mdx +19 -0
- package/test/fixtures/mdx-plus-react/astro.config.mjs +6 -0
- package/test/fixtures/mdx-plus-react/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-plus-react/package.json +10 -0
- package/test/fixtures/mdx-plus-react/src/components/Component.jsx +5 -0
- package/test/fixtures/mdx-plus-react/src/pages/index.astro +11 -0
- package/test/fixtures/mdx-syntax-hightlighting/src/pages/index.mdx +9 -0
- package/test/fixtures/mdx-url-export/src/pages/pages.json.js +9 -0
- package/test/fixtures/mdx-url-export/src/pages/test-1.mdx +1 -0
- package/test/fixtures/mdx-url-export/src/pages/test-2.mdx +1 -0
- package/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx +3 -0
- 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-component.test.js +112 -0
- package/test/mdx-escape.test.js +32 -0
- package/test/mdx-frontmatter-injection.test.js +56 -0
- package/test/mdx-frontmatter.test.js +77 -0
- package/test/mdx-get-headings.test.js +60 -0
- package/test/mdx-get-static-paths.test.js +32 -0
- package/test/mdx-namespace.test.js +83 -0
- package/test/mdx-page.test.js +64 -0
- package/test/mdx-plugins.test.js +211 -0
- package/test/mdx-plus-react.test.js +25 -0
- package/test/mdx-syntax-highlighting.test.js +91 -0
- package/test/mdx-url-export.test.js +28 -0
- package/test/mdx-vite-env-vars.test.js +54 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,211 @@
|
|
|
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
|
+
import remarkToc from 'remark-toc';
|
|
7
|
+
|
|
8
|
+
const FIXTURE_ROOT = new URL('./fixtures/mdx-plugins/', import.meta.url);
|
|
9
|
+
const FILE = '/with-plugins/index.html';
|
|
10
|
+
|
|
11
|
+
describe('MDX plugins', () => {
|
|
12
|
+
it('supports custom remark plugins - TOC', async () => {
|
|
13
|
+
const fixture = await buildFixture({
|
|
14
|
+
integrations: [
|
|
15
|
+
mdx({
|
|
16
|
+
remarkPlugins: [remarkToc],
|
|
17
|
+
}),
|
|
18
|
+
],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const html = await fixture.readFile(FILE);
|
|
22
|
+
const { document } = parseHTML(html);
|
|
23
|
+
|
|
24
|
+
expect(selectTocLink(document)).to.not.be.null;
|
|
25
|
+
});
|
|
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
|
+
|
|
38
|
+
it('supports custom rehype plugins', async () => {
|
|
39
|
+
const fixture = await buildFixture({
|
|
40
|
+
integrations: [
|
|
41
|
+
mdx({
|
|
42
|
+
rehypePlugins: [rehypeExamplePlugin],
|
|
43
|
+
}),
|
|
44
|
+
],
|
|
45
|
+
});
|
|
46
|
+
const html = await fixture.readFile(FILE);
|
|
47
|
+
const { document } = parseHTML(html);
|
|
48
|
+
|
|
49
|
+
expect(selectRehypeExample(document)).to.not.be.null;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('extends markdown config by default', async () => {
|
|
53
|
+
const fixture = await buildFixture({
|
|
54
|
+
markdown: {
|
|
55
|
+
remarkPlugins: [remarkExamplePlugin],
|
|
56
|
+
rehypePlugins: [rehypeExamplePlugin],
|
|
57
|
+
},
|
|
58
|
+
integrations: [mdx()],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const html = await fixture.readFile(FILE);
|
|
62
|
+
const { document } = parseHTML(html);
|
|
63
|
+
|
|
64
|
+
expect(selectRemarkExample(document)).to.not.be.null;
|
|
65
|
+
expect(selectRehypeExample(document)).to.not.be.null;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('ignores string-based plugins in markdown config', async () => {
|
|
69
|
+
const fixture = await buildFixture({
|
|
70
|
+
markdown: {
|
|
71
|
+
remarkPlugins: [['remark-toc']],
|
|
72
|
+
},
|
|
73
|
+
integrations: [mdx()],
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const html = await fixture.readFile(FILE);
|
|
77
|
+
const { document } = parseHTML(html);
|
|
78
|
+
|
|
79
|
+
expect(selectTocLink(document)).to.be.null;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('respects "extendDefaultPlugins" when extending markdown', async () => {
|
|
83
|
+
const fixture = await buildFixture({
|
|
84
|
+
markdown: {
|
|
85
|
+
remarkPlugins: [remarkExamplePlugin],
|
|
86
|
+
rehypePlugins: [rehypeExamplePlugin],
|
|
87
|
+
extendDefaultPlugins: true,
|
|
88
|
+
},
|
|
89
|
+
integrations: [mdx()],
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const html = await fixture.readFile(FILE);
|
|
93
|
+
const { document } = parseHTML(html);
|
|
94
|
+
|
|
95
|
+
expect(selectRemarkExample(document)).to.not.be.null;
|
|
96
|
+
expect(selectRehypeExample(document)).to.not.be.null;
|
|
97
|
+
expect(selectGfmLink(document)).to.not.be.null;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('extends markdown config with extendPlugins: "markdown"', async () => {
|
|
101
|
+
const fixture = await buildFixture({
|
|
102
|
+
markdown: {
|
|
103
|
+
remarkPlugins: [remarkExamplePlugin],
|
|
104
|
+
rehypePlugins: [rehypeExamplePlugin],
|
|
105
|
+
},
|
|
106
|
+
integrations: [
|
|
107
|
+
mdx({
|
|
108
|
+
extendPlugins: 'markdown',
|
|
109
|
+
remarkPlugins: [remarkToc],
|
|
110
|
+
}),
|
|
111
|
+
],
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const html = await fixture.readFile(FILE);
|
|
115
|
+
const { document } = parseHTML(html);
|
|
116
|
+
|
|
117
|
+
expect(selectRemarkExample(document)).to.not.be.null;
|
|
118
|
+
expect(selectRehypeExample(document)).to.not.be.null;
|
|
119
|
+
expect(selectTocLink(document)).to.not.be.null;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('extends default plugins with extendPlugins: "astroDefaults"', async () => {
|
|
123
|
+
const fixture = await buildFixture({
|
|
124
|
+
markdown: {
|
|
125
|
+
// should NOT be applied to MDX
|
|
126
|
+
remarkPlugins: [remarkToc],
|
|
127
|
+
},
|
|
128
|
+
integrations: [
|
|
129
|
+
mdx({
|
|
130
|
+
remarkPlugins: [remarkExamplePlugin],
|
|
131
|
+
rehypePlugins: [rehypeExamplePlugin],
|
|
132
|
+
extendPlugins: 'astroDefaults',
|
|
133
|
+
}),
|
|
134
|
+
],
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const html = await fixture.readFile(FILE);
|
|
138
|
+
const { document } = parseHTML(html);
|
|
139
|
+
|
|
140
|
+
expect(selectGfmLink(document)).to.not.be.null;
|
|
141
|
+
// remark and rehype plugins still respected
|
|
142
|
+
expect(selectRemarkExample(document)).to.not.be.null;
|
|
143
|
+
expect(selectRehypeExample(document)).to.not.be.null;
|
|
144
|
+
// Does NOT inherit TOC from markdown config
|
|
145
|
+
expect(selectTocLink(document)).to.be.null;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('does not extend default plugins with extendPlugins: false', async () => {
|
|
149
|
+
const fixture = await buildFixture({
|
|
150
|
+
markdown: {
|
|
151
|
+
remarkPlugins: [remarkExamplePlugin],
|
|
152
|
+
},
|
|
153
|
+
integrations: [
|
|
154
|
+
mdx({
|
|
155
|
+
remarkPlugins: [],
|
|
156
|
+
extendPlugins: false,
|
|
157
|
+
}),
|
|
158
|
+
],
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const html = await fixture.readFile(FILE);
|
|
162
|
+
const { document } = parseHTML(html);
|
|
163
|
+
|
|
164
|
+
expect(selectGfmLink(document)).to.be.null;
|
|
165
|
+
expect(selectRemarkExample(document)).to.be.null;
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
async function buildFixture(config) {
|
|
170
|
+
const fixture = await loadFixture({
|
|
171
|
+
root: FIXTURE_ROOT,
|
|
172
|
+
...config,
|
|
173
|
+
});
|
|
174
|
+
await fixture.build();
|
|
175
|
+
return fixture;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function remarkExamplePlugin() {
|
|
179
|
+
return (tree) => {
|
|
180
|
+
tree.children.push({
|
|
181
|
+
type: 'html',
|
|
182
|
+
value: '<div data-remark-plugin-works="true"></div>',
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function rehypeExamplePlugin() {
|
|
188
|
+
return (tree) => {
|
|
189
|
+
tree.children.push({
|
|
190
|
+
type: 'element',
|
|
191
|
+
tagName: 'div',
|
|
192
|
+
properties: { 'data-rehype-plugin-works': 'true' },
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function selectTocLink(document) {
|
|
198
|
+
return document.querySelector('ul a[href="#section-1"]');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function selectGfmLink(document) {
|
|
202
|
+
return document.querySelector('a[href="https://handle-me-gfm.com"]');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function selectRemarkExample(document) {
|
|
206
|
+
return document.querySelector('div[data-remark-plugin-works]');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function selectRehypeExample(document) {
|
|
210
|
+
return document.querySelector('div[data-rehype-plugin-works]');
|
|
211
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 and React', () => {
|
|
8
|
+
let fixture;
|
|
9
|
+
|
|
10
|
+
before(async () => {
|
|
11
|
+
fixture = await loadFixture({
|
|
12
|
+
root: new URL('./fixtures/mdx-plus-react/', import.meta.url),
|
|
13
|
+
});
|
|
14
|
+
await fixture.build();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('can be used in the same project', async () => {
|
|
18
|
+
const html = await fixture.readFile('/index.html');
|
|
19
|
+
const { document } = parseHTML(html);
|
|
20
|
+
|
|
21
|
+
const p = document.querySelector('p');
|
|
22
|
+
|
|
23
|
+
expect(p.textContent).to.equal('Hello world');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
import shikiTwoslash from 'remark-shiki-twoslash';
|
|
7
|
+
|
|
8
|
+
const FIXTURE_ROOT = new URL('./fixtures/mdx-syntax-hightlighting/', import.meta.url);
|
|
9
|
+
|
|
10
|
+
describe('MDX syntax highlighting', () => {
|
|
11
|
+
describe('shiki', () => {
|
|
12
|
+
it('works', async () => {
|
|
13
|
+
const fixture = await loadFixture({
|
|
14
|
+
root: FIXTURE_ROOT,
|
|
15
|
+
markdown: {
|
|
16
|
+
syntaxHighlight: 'shiki',
|
|
17
|
+
},
|
|
18
|
+
integrations: [mdx()],
|
|
19
|
+
});
|
|
20
|
+
await fixture.build();
|
|
21
|
+
|
|
22
|
+
const html = await fixture.readFile('/index.html');
|
|
23
|
+
const { document } = parseHTML(html);
|
|
24
|
+
|
|
25
|
+
const shikiCodeBlock = document.querySelector('pre.astro-code');
|
|
26
|
+
expect(shikiCodeBlock).to.not.be.null;
|
|
27
|
+
expect(shikiCodeBlock.getAttribute('style')).to.contain('background-color:#0d1117');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('respects markdown.shikiConfig.theme', async () => {
|
|
31
|
+
const fixture = await loadFixture({
|
|
32
|
+
root: FIXTURE_ROOT,
|
|
33
|
+
markdown: {
|
|
34
|
+
syntaxHighlight: 'shiki',
|
|
35
|
+
shikiConfig: {
|
|
36
|
+
theme: 'dracula',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
integrations: [mdx()],
|
|
40
|
+
});
|
|
41
|
+
await fixture.build();
|
|
42
|
+
|
|
43
|
+
const html = await fixture.readFile('/index.html');
|
|
44
|
+
const { document } = parseHTML(html);
|
|
45
|
+
|
|
46
|
+
const shikiCodeBlock = document.querySelector('pre.astro-code');
|
|
47
|
+
expect(shikiCodeBlock).to.not.be.null;
|
|
48
|
+
expect(shikiCodeBlock.getAttribute('style')).to.contain('background-color:#282A36');
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('prism', () => {
|
|
53
|
+
it('works', async () => {
|
|
54
|
+
const fixture = await loadFixture({
|
|
55
|
+
root: FIXTURE_ROOT,
|
|
56
|
+
markdown: {
|
|
57
|
+
syntaxHighlight: 'prism',
|
|
58
|
+
},
|
|
59
|
+
integrations: [mdx()],
|
|
60
|
+
});
|
|
61
|
+
await fixture.build();
|
|
62
|
+
|
|
63
|
+
const html = await fixture.readFile('/index.html');
|
|
64
|
+
const { document } = parseHTML(html);
|
|
65
|
+
|
|
66
|
+
const prismCodeBlock = document.querySelector('pre.language-astro');
|
|
67
|
+
expect(prismCodeBlock).to.not.be.null;
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('supports custom highlighter - shiki-twoslash', async () => {
|
|
72
|
+
const fixture = await loadFixture({
|
|
73
|
+
root: FIXTURE_ROOT,
|
|
74
|
+
markdown: {
|
|
75
|
+
syntaxHighlight: false,
|
|
76
|
+
},
|
|
77
|
+
integrations: [
|
|
78
|
+
mdx({
|
|
79
|
+
remarkPlugins: [shikiTwoslash.default ?? shikiTwoslash],
|
|
80
|
+
}),
|
|
81
|
+
],
|
|
82
|
+
});
|
|
83
|
+
await fixture.build();
|
|
84
|
+
|
|
85
|
+
const html = await fixture.readFile('/index.html');
|
|
86
|
+
const { document } = parseHTML(html);
|
|
87
|
+
|
|
88
|
+
const twoslashCodeBlock = document.querySelector('pre.shiki');
|
|
89
|
+
expect(twoslashCodeBlock).to.not.be.null;
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import mdx from '@astrojs/mdx';
|
|
2
|
+
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
5
|
+
|
|
6
|
+
describe('MDX url export', () => {
|
|
7
|
+
let fixture;
|
|
8
|
+
|
|
9
|
+
before(async () => {
|
|
10
|
+
fixture = await loadFixture({
|
|
11
|
+
root: new URL('./fixtures/mdx-url-export/', import.meta.url),
|
|
12
|
+
integrations: [mdx()],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
await fixture.build();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('generates correct urls in glob result', async () => {
|
|
19
|
+
const { urls } = JSON.parse(await fixture.readFile('/pages.json'));
|
|
20
|
+
expect(urls).to.include('/test-1');
|
|
21
|
+
expect(urls).to.include('/test-2');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('respects "export url" overrides in glob result', async () => {
|
|
25
|
+
const { urls } = JSON.parse(await fixture.readFile('/pages.json'));
|
|
26
|
+
expect(urls).to.include('/AH!');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -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
|
+
});
|