@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
@@ -1,85 +0,0 @@
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 with Astro Markdown remark-rehype config', () => {
8
- it('Renders footnotes with values from the default configuration', async () => {
9
- const fixture = await loadFixture({
10
- root: new URL('./fixtures/mdx-astro-markdown-remarkRehype/', import.meta.url),
11
- integrations: [mdx()],
12
- markdown: {
13
- remarkRehype: {
14
- footnoteLabel: 'Catatan kaki',
15
- footnoteBackLabel: 'Kembali ke konten',
16
- },
17
- },
18
- });
19
-
20
- await fixture.build();
21
- const html = await fixture.readFile('/index.html');
22
- const { document } = parseHTML(html);
23
-
24
- expect(document.querySelector('#footnote-label').textContent).to.equal('Catatan kaki');
25
- expect(document.querySelector('.data-footnote-backref').getAttribute('aria-label')).to.equal(
26
- 'Kembali ke konten'
27
- );
28
- });
29
-
30
- it('Renders footnotes with values from custom configuration extending the default', async () => {
31
- const fixture = await loadFixture({
32
- root: new URL('./fixtures/mdx-astro-markdown-remarkRehype/', import.meta.url),
33
- integrations: [
34
- mdx({
35
- remarkRehype: {
36
- footnoteLabel: 'Catatan kaki',
37
- footnoteBackLabel: 'Kembali ke konten',
38
- },
39
- }),
40
- ],
41
- markdown: {
42
- remarkRehype: {
43
- footnoteBackLabel: 'Replace me',
44
- },
45
- },
46
- });
47
-
48
- await fixture.build();
49
- const html = await fixture.readFile('/index.html');
50
- const { document } = parseHTML(html);
51
-
52
- expect(document.querySelector('#footnote-label').textContent).to.equal('Catatan kaki');
53
- expect(document.querySelector('.data-footnote-backref').getAttribute('aria-label')).to.equal(
54
- 'Kembali ke konten'
55
- );
56
- });
57
-
58
- it('Renders footnotes with values from custom configuration without extending the default', async () => {
59
- const fixture = await loadFixture({
60
- root: new URL('./fixtures/mdx-astro-markdown-remarkRehype/', import.meta.url),
61
- integrations: [
62
- mdx({
63
- extendPlugins: 'astroDefaults',
64
- remarkRehype: {
65
- footnoteLabel: 'Catatan kaki',
66
- },
67
- }),
68
- ],
69
- markdown: {
70
- remarkRehype: {
71
- footnoteBackLabel: 'Kembali ke konten',
72
- },
73
- },
74
- });
75
-
76
- await fixture.build();
77
- const html = await fixture.readFile('/index.html');
78
- const { document } = parseHTML(html);
79
-
80
- expect(document.querySelector('#footnote-label').textContent).to.equal('Catatan kaki');
81
- expect(document.querySelector('.data-footnote-backref').getAttribute('aria-label')).to.equal(
82
- 'Back to content'
83
- );
84
- });
85
- });
@@ -1,191 +0,0 @@
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 Component', () => {
8
- let fixture;
9
-
10
- before(async () => {
11
- fixture = await loadFixture({
12
- root: new URL('./fixtures/mdx-component/', import.meta.url),
13
- integrations: [mdx()],
14
- });
15
- });
16
-
17
- describe('build', () => {
18
- before(async () => {
19
- await fixture.build();
20
- });
21
-
22
- it('supports top-level imports', async () => {
23
- const html = await fixture.readFile('/index.html');
24
- const { document } = parseHTML(html);
25
-
26
- const h1 = document.querySelector('h1');
27
- const foo = document.querySelector('#foo');
28
-
29
- expect(h1.textContent).to.equal('Hello component!');
30
- expect(foo.textContent).to.equal('bar');
31
- });
32
-
33
- it('supports glob imports - <Component.default />', async () => {
34
- const html = await fixture.readFile('/glob/index.html');
35
- const { document } = parseHTML(html);
36
-
37
- const h1 = document.querySelector('[data-default-export] h1');
38
- const foo = document.querySelector('[data-default-export] #foo');
39
-
40
- expect(h1.textContent).to.equal('Hello component!');
41
- expect(foo.textContent).to.equal('bar');
42
- });
43
-
44
- it('supports glob imports - <Content />', async () => {
45
- const html = await fixture.readFile('/glob/index.html');
46
- const { document } = parseHTML(html);
47
-
48
- const h1 = document.querySelector('[data-content-export] h1');
49
- const foo = document.querySelector('[data-content-export] #foo');
50
-
51
- expect(h1.textContent).to.equal('Hello component!');
52
- expect(foo.textContent).to.equal('bar');
53
- });
54
-
55
- describe('with <Fragment>', () => {
56
- it('supports top-level imports', async () => {
57
- const html = await fixture.readFile('/w-fragment/index.html');
58
- const { document } = parseHTML(html);
59
-
60
- const h1 = document.querySelector('h1');
61
- const p = document.querySelector('p');
62
-
63
- expect(h1.textContent).to.equal('MDX containing <Fragment />');
64
- expect(p.textContent).to.equal('bar');
65
- });
66
-
67
- it('supports glob imports - <Component.default />', async () => {
68
- const html = await fixture.readFile('/glob/index.html');
69
- const { document } = parseHTML(html);
70
-
71
- const h = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] h1');
72
- const p = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] p');
73
-
74
- expect(h.textContent).to.equal('MDX containing <Fragment />');
75
- expect(p.textContent).to.equal('bar');
76
- });
77
-
78
- it('supports glob imports - <Content />', async () => {
79
- const html = await fixture.readFile('/glob/index.html');
80
- const { document } = parseHTML(html);
81
-
82
- const h = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] h1');
83
- const p = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] p');
84
-
85
- expect(h.textContent).to.equal('MDX containing <Fragment />');
86
- expect(p.textContent).to.equal('bar');
87
- });
88
- });
89
- });
90
-
91
- describe('dev', () => {
92
- let devServer;
93
-
94
- before(async () => {
95
- devServer = await fixture.startDevServer();
96
- });
97
-
98
- after(async () => {
99
- await devServer.stop();
100
- });
101
-
102
- it('supports top-level imports', async () => {
103
- const res = await fixture.fetch('/');
104
-
105
- expect(res.status).to.equal(200);
106
-
107
- const html = await res.text();
108
- const { document } = parseHTML(html);
109
-
110
- const h1 = document.querySelector('h1');
111
- const foo = document.querySelector('#foo');
112
-
113
- expect(h1.textContent).to.equal('Hello component!');
114
- expect(foo.textContent).to.equal('bar');
115
- });
116
-
117
- it('supports glob imports - <Component.default />', async () => {
118
- const res = await fixture.fetch('/glob');
119
-
120
- expect(res.status).to.equal(200);
121
-
122
- const html = await res.text();
123
- const { document } = parseHTML(html);
124
-
125
- const h1 = document.querySelector('[data-default-export] h1');
126
- const foo = document.querySelector('[data-default-export] #foo');
127
-
128
- expect(h1.textContent).to.equal('Hello component!');
129
- expect(foo.textContent).to.equal('bar');
130
- });
131
-
132
- it('supports glob imports - <Content />', async () => {
133
- const res = await fixture.fetch('/glob');
134
-
135
- expect(res.status).to.equal(200);
136
-
137
- const html = await res.text();
138
- const { document } = parseHTML(html);
139
-
140
- const h1 = document.querySelector('[data-content-export] h1');
141
- const foo = document.querySelector('[data-content-export] #foo');
142
-
143
- expect(h1.textContent).to.equal('Hello component!');
144
- expect(foo.textContent).to.equal('bar');
145
- });
146
-
147
- describe('with <Fragment>', () => {
148
- it('supports top-level imports', async () => {
149
- const res = await fixture.fetch('/w-fragment');
150
- expect(res.status).to.equal(200);
151
-
152
- const html = await res.text();
153
- const { document } = parseHTML(html);
154
-
155
- const h1 = document.querySelector('h1');
156
- const p = document.querySelector('p');
157
-
158
- expect(h1.textContent).to.equal('MDX containing <Fragment />');
159
- expect(p.textContent).to.equal('bar');
160
- });
161
-
162
- it('supports glob imports - <Component.default />', async () => {
163
- const res = await fixture.fetch('/glob');
164
- expect(res.status).to.equal(200);
165
-
166
- const html = await res.text();
167
- const { document } = parseHTML(html);
168
-
169
- const h = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] h1');
170
- const p = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] p');
171
-
172
- expect(h.textContent).to.equal('MDX containing <Fragment />');
173
- expect(p.textContent).to.equal('bar');
174
- });
175
-
176
- it('supports glob imports - <Content />', async () => {
177
- const res = await fixture.fetch('/glob');
178
- expect(res.status).to.equal(200);
179
-
180
- const html = await res.text();
181
- const { document } = parseHTML(html);
182
-
183
- const h = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] h1');
184
- const p = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] p');
185
-
186
- expect(h.textContent).to.equal('MDX containing <Fragment />');
187
- expect(p.textContent).to.equal('bar');
188
- });
189
- });
190
- });
191
- });
@@ -1,32 +0,0 @@
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
- const FIXTURE_ROOT = new URL('./fixtures/mdx-escape/', import.meta.url);
8
-
9
- describe('MDX frontmatter', () => {
10
- let fixture;
11
- before(async () => {
12
- fixture = await loadFixture({
13
- root: FIXTURE_ROOT,
14
- integrations: [mdx()],
15
- });
16
- await fixture.build();
17
- });
18
-
19
- it('does not have unescaped HTML at top-level', async () => {
20
- const html = await fixture.readFile('/index.html');
21
- const { document } = parseHTML(html);
22
-
23
- expect(document.body.textContent).to.not.include('<em');
24
- });
25
-
26
- it('does not have unescaped HTML inside html tags', async () => {
27
- const html = await fixture.readFile('/html-tag/index.html');
28
- const { document } = parseHTML(html);
29
-
30
- expect(document.body.textContent).to.not.include('<em');
31
- });
32
- });
@@ -1,53 +0,0 @@
1
- import { expect } from 'chai';
2
- import { parseHTML } from 'linkedom';
3
- import { loadFixture } from '../../../astro/test/test-utils.js';
4
-
5
- const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter-injection/', import.meta.url);
6
-
7
- describe('MDX frontmatter injection', () => {
8
- let fixture;
9
-
10
- before(async () => {
11
- fixture = await loadFixture({
12
- root: FIXTURE_ROOT,
13
- });
14
- await fixture.build();
15
- });
16
-
17
- it('remark supports custom vfile data - get title', async () => {
18
- const frontmatterByPage = JSON.parse(await fixture.readFile('/glob.json'));
19
- const titles = frontmatterByPage.map((frontmatter = {}) => frontmatter.title);
20
- expect(titles).to.contain('Page 1');
21
- expect(titles).to.contain('Page 2');
22
- });
23
-
24
- it('rehype supports custom vfile data - reading time', async () => {
25
- const frontmatterByPage = JSON.parse(await fixture.readFile('/glob.json'));
26
- const readingTimes = frontmatterByPage.map(
27
- (frontmatter = {}) => frontmatter.injectedReadingTime
28
- );
29
- expect(readingTimes.length).to.be.greaterThan(0);
30
- for (let readingTime of readingTimes) {
31
- expect(readingTime).to.not.be.null;
32
- expect(readingTime.text).match(/^\d+ min read/);
33
- }
34
- });
35
-
36
- it('allow user frontmatter mutation', async () => {
37
- const frontmatterByPage = JSON.parse(await fixture.readFile('/glob.json'));
38
- const descriptions = frontmatterByPage.map((frontmatter = {}) => frontmatter.description);
39
- expect(descriptions).to.contain('Processed by remarkDescription plugin: Page 1 description');
40
- expect(descriptions).to.contain('Processed by remarkDescription plugin: Page 2 description');
41
- });
42
-
43
- it('passes injected frontmatter to layouts', async () => {
44
- const html1 = await fixture.readFile('/page-1/index.html');
45
- const html2 = await fixture.readFile('/page-2/index.html');
46
-
47
- const title1 = parseHTML(html1).document.querySelector('title');
48
- const title2 = parseHTML(html2).document.querySelector('title');
49
-
50
- expect(title1.innerHTML).to.equal('Page 1');
51
- expect(title2.innerHTML).to.equal('Page 2');
52
- });
53
- });
@@ -1,77 +0,0 @@
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
- const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter/', import.meta.url);
8
-
9
- describe('MDX frontmatter', () => {
10
- let fixture;
11
- before(async () => {
12
- fixture = await loadFixture({
13
- root: FIXTURE_ROOT,
14
- integrations: [mdx()],
15
- });
16
- await fixture.build();
17
- });
18
- it('builds when "frontmatter.property" is in JSX expression', async () => {
19
- expect(true).to.equal(true);
20
- });
21
-
22
- it('extracts frontmatter to "frontmatter" export', async () => {
23
- const { titles } = JSON.parse(await fixture.readFile('/glob.json'));
24
- expect(titles).to.include('Using YAML frontmatter');
25
- });
26
-
27
- it('renders layout from "layout" frontmatter property', async () => {
28
- const html = await fixture.readFile('/index.html');
29
- const { document } = parseHTML(html);
30
-
31
- const layoutParagraph = document.querySelector('[data-layout-rendered]');
32
-
33
- expect(layoutParagraph).to.not.be.null;
34
- });
35
-
36
- it('passes frontmatter to layout via "content" and "frontmatter" props', async () => {
37
- const html = await fixture.readFile('/index.html');
38
- const { document } = parseHTML(html);
39
-
40
- const contentTitle = document.querySelector('[data-content-title]');
41
- const frontmatterTitle = document.querySelector('[data-frontmatter-title]');
42
-
43
- expect(contentTitle.textContent).to.equal('Using YAML frontmatter');
44
- expect(frontmatterTitle.textContent).to.equal('Using YAML frontmatter');
45
- });
46
-
47
- it('passes headings to layout via "headings" prop', async () => {
48
- const html = await fixture.readFile('/with-headings/index.html');
49
- const { document } = parseHTML(html);
50
-
51
- const headingSlugs = [...document.querySelectorAll('[data-headings] > li')].map(
52
- (el) => el.textContent
53
- );
54
-
55
- expect(headingSlugs.length).to.be.greaterThan(0);
56
- expect(headingSlugs).to.contain('section-1');
57
- expect(headingSlugs).to.contain('section-2');
58
- });
59
-
60
- it('passes "file" and "url" to layout', async () => {
61
- const html = await fixture.readFile('/with-headings/index.html');
62
- const { document } = parseHTML(html);
63
-
64
- const frontmatterFile = document.querySelector('[data-frontmatter-file]')?.textContent;
65
- const frontmatterUrl = document.querySelector('[data-frontmatter-url]')?.textContent;
66
- const file = document.querySelector('[data-file]')?.textContent;
67
- const url = document.querySelector('[data-url]')?.textContent;
68
-
69
- expect(frontmatterFile?.endsWith('with-headings.mdx')).to.equal(
70
- true,
71
- '"file" prop does not end with correct path or is undefined'
72
- );
73
- expect(frontmatterUrl).to.equal('/with-headings');
74
- expect(file).to.equal(frontmatterFile);
75
- expect(url).to.equal(frontmatterUrl);
76
- });
77
- });
@@ -1,151 +0,0 @@
1
- import { rehypeHeadingIds } from '@astrojs/markdown-remark';
2
- import mdx from '@astrojs/mdx';
3
- import { visit } from 'unist-util-visit';
4
-
5
- import { expect } from 'chai';
6
- import { parseHTML } from 'linkedom';
7
- import { loadFixture } from '../../../astro/test/test-utils.js';
8
-
9
- describe('MDX getHeadings', () => {
10
- let fixture;
11
-
12
- before(async () => {
13
- fixture = await loadFixture({
14
- root: new URL('./fixtures/mdx-get-headings/', import.meta.url),
15
- integrations: [mdx()],
16
- });
17
-
18
- await fixture.build();
19
- });
20
-
21
- it('adds anchor IDs to headings', async () => {
22
- const html = await fixture.readFile('/test/index.html');
23
- const { document } = parseHTML(html);
24
-
25
- const h2Ids = document.querySelectorAll('h2').map((el) => el?.id);
26
- const h3Ids = document.querySelectorAll('h3').map((el) => el?.id);
27
- expect(document.querySelector('h1').id).to.equal('heading-test');
28
- expect(h2Ids).to.contain('section-1');
29
- expect(h2Ids).to.contain('section-2');
30
- expect(h3Ids).to.contain('subsection-1');
31
- expect(h3Ids).to.contain('subsection-2');
32
- });
33
-
34
- it('generates correct getHeadings() export', async () => {
35
- const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
36
- // TODO: make this a snapshot test :)
37
- expect(JSON.stringify(headingsByPage['./test.mdx'])).to.equal(
38
- JSON.stringify([
39
- { depth: 1, slug: 'heading-test', text: 'Heading test' },
40
- { depth: 2, slug: 'section-1', text: 'Section 1' },
41
- { depth: 3, slug: 'subsection-1', text: 'Subsection 1' },
42
- { depth: 3, slug: 'subsection-2', text: 'Subsection 2' },
43
- { depth: 2, slug: 'section-2', text: 'Section 2' },
44
- ])
45
- );
46
- });
47
-
48
- it('generates correct getHeadings() export for JSX expressions', async () => {
49
- const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
50
- expect(JSON.stringify(headingsByPage['./test-with-jsx-expressions.mdx'])).to.equal(
51
- JSON.stringify([
52
- {
53
- depth: 1,
54
- slug: 'heading-test-with-jsx-expressions',
55
- text: 'Heading test with JSX expressions',
56
- },
57
- { depth: 2, slug: 'h2title', text: 'h2Title' },
58
- { depth: 3, slug: 'h3title', text: 'h3Title' },
59
- ])
60
- );
61
- });
62
- });
63
-
64
- describe('MDX heading IDs can be customized by user plugins', () => {
65
- let fixture;
66
-
67
- before(async () => {
68
- fixture = await loadFixture({
69
- root: new URL('./fixtures/mdx-get-headings/', import.meta.url),
70
- integrations: [mdx()],
71
- markdown: {
72
- rehypePlugins: [
73
- () => (tree) => {
74
- let count = 0;
75
- visit(tree, 'element', (node, index, parent) => {
76
- if (!/^h\d$/.test(node.tagName)) return;
77
- if (!node.properties?.id) {
78
- node.properties = { ...node.properties, id: String(count++) };
79
- }
80
- });
81
- },
82
- ],
83
- },
84
- });
85
-
86
- await fixture.build();
87
- });
88
-
89
- it('adds user-specified IDs to HTML output', async () => {
90
- const html = await fixture.readFile('/test/index.html');
91
- const { document } = parseHTML(html);
92
-
93
- const h1 = document.querySelector('h1');
94
- expect(h1?.textContent).to.equal('Heading test');
95
- expect(h1?.getAttribute('id')).to.equal('0');
96
-
97
- const headingIDs = document.querySelectorAll('h1,h2,h3').map((el) => el.id);
98
- expect(JSON.stringify(headingIDs)).to.equal(
99
- JSON.stringify(Array.from({ length: headingIDs.length }, (_, idx) => String(idx)))
100
- );
101
- });
102
-
103
- it('generates correct getHeadings() export', async () => {
104
- const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
105
- expect(JSON.stringify(headingsByPage['./test.mdx'])).to.equal(
106
- JSON.stringify([
107
- { depth: 1, slug: '0', text: 'Heading test' },
108
- { depth: 2, slug: '1', text: 'Section 1' },
109
- { depth: 3, slug: '2', text: 'Subsection 1' },
110
- { depth: 3, slug: '3', text: 'Subsection 2' },
111
- { depth: 2, slug: '4', text: 'Section 2' },
112
- ])
113
- );
114
- });
115
- });
116
-
117
- describe('MDX heading IDs can be injected before user plugins', () => {
118
- let fixture;
119
-
120
- before(async () => {
121
- fixture = await loadFixture({
122
- root: new URL('./fixtures/mdx-get-headings/', import.meta.url),
123
- integrations: [
124
- mdx({
125
- rehypePlugins: [
126
- rehypeHeadingIds,
127
- () => (tree) => {
128
- visit(tree, 'element', (node, index, parent) => {
129
- if (!/^h\d$/.test(node.tagName)) return;
130
- if (node.properties?.id) {
131
- node.children.push({ type: 'text', value: ' ' + node.properties.id });
132
- }
133
- });
134
- },
135
- ],
136
- }),
137
- ],
138
- });
139
-
140
- await fixture.build();
141
- });
142
-
143
- it('adds user-specified IDs to HTML output', async () => {
144
- const html = await fixture.readFile('/test/index.html');
145
- const { document } = parseHTML(html);
146
-
147
- const h1 = document.querySelector('h1');
148
- expect(h1?.textContent).to.equal('Heading test heading-test');
149
- expect(h1?.id).to.equal('heading-test');
150
- });
151
- });
@@ -1,32 +0,0 @@
1
- import mdx from '@astrojs/mdx';
2
-
3
- import { expect } from 'chai';
4
- import { loadFixture } from '../../../astro/test/test-utils.js';
5
- import * as cheerio from 'cheerio';
6
-
7
- const FIXTURE_ROOT = new URL('./fixtures/mdx-get-static-paths', import.meta.url);
8
-
9
- describe('getStaticPaths', () => {
10
- /** @type {import('astro/test/test-utils').Fixture} */
11
- let fixture;
12
- before(async () => {
13
- fixture = await loadFixture({
14
- root: FIXTURE_ROOT,
15
- integrations: [mdx()],
16
- });
17
- await fixture.build();
18
- });
19
-
20
- it('Provides file and url', async () => {
21
- const html = await fixture.readFile('/one/index.html');
22
-
23
- const $ = cheerio.load(html);
24
- expect($('p').text()).to.equal('First mdx file');
25
- expect($('#one').text()).to.equal('hello', 'Frontmatter included');
26
- expect($('#url').text()).to.equal('/src/content/1.mdx', 'url is included');
27
- expect($('#file').text()).to.contain(
28
- 'fixtures/mdx-get-static-paths/src/content/1.mdx',
29
- 'file is included'
30
- );
31
- });
32
- });
@@ -1,30 +0,0 @@
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 Infinite Loop', () => {
7
- let fixture;
8
-
9
- before(async () => {
10
- fixture = await loadFixture({
11
- root: new URL('./fixtures/mdx-infinite-loop/', import.meta.url),
12
- integrations: [mdx()],
13
- });
14
- });
15
-
16
- describe('build', () => {
17
- let err;
18
- before(async () => {
19
- try {
20
- await fixture.build();
21
- } catch (e) {
22
- err = e;
23
- }
24
- });
25
-
26
- it('does not hang forever if an error is thrown', async () => {
27
- expect(!!err).to.be.true;
28
- });
29
- });
30
- });