@docusaurus/mdx-loader 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17

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 (72) hide show
  1. package/lib/index.d.ts +24 -0
  2. package/lib/index.d.ts.map +1 -0
  3. package/lib/index.js +178 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/remark/headings/index.d.ts +9 -0
  6. package/lib/remark/headings/index.d.ts.map +1 -0
  7. package/lib/remark/headings/index.js +60 -0
  8. package/lib/remark/headings/index.js.map +1 -0
  9. package/lib/remark/toc/index.d.ts +13 -0
  10. package/lib/remark/toc/index.d.ts.map +1 -0
  11. package/lib/remark/toc/index.js +81 -0
  12. package/lib/remark/toc/index.js.map +1 -0
  13. package/lib/remark/transformImage/index.d.ts +14 -0
  14. package/lib/remark/transformImage/index.d.ts.map +1 -0
  15. package/lib/remark/transformImage/index.js +109 -0
  16. package/lib/remark/transformImage/index.js.map +1 -0
  17. package/lib/remark/transformLinks/index.d.ts +14 -0
  18. package/lib/remark/transformLinks/index.d.ts.map +1 -0
  19. package/lib/remark/transformLinks/index.js +100 -0
  20. package/lib/remark/transformLinks/index.js.map +1 -0
  21. package/lib/remark/unwrapMdxCodeBlocks/index.d.ts +9 -0
  22. package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +1 -0
  23. package/lib/remark/unwrapMdxCodeBlocks/index.js +29 -0
  24. package/lib/remark/unwrapMdxCodeBlocks/index.js.map +1 -0
  25. package/lib/remark/utils/index.d.ts +11 -0
  26. package/lib/remark/utils/index.d.ts.map +1 -0
  27. package/lib/remark/utils/index.js +38 -0
  28. package/lib/remark/utils/index.js.map +1 -0
  29. package/package.json +19 -16
  30. package/src/deps.d.ts +30 -0
  31. package/src/index.ts +225 -0
  32. package/src/{index.d.ts → mdx-loader.d.ts} +7 -8
  33. package/src/remark/headings/{index.js → index.ts} +22 -23
  34. package/src/remark/toc/index.ts +102 -0
  35. package/src/remark/transformImage/index.ts +167 -0
  36. package/src/remark/transformLinks/index.ts +150 -0
  37. package/src/remark/unwrapMdxCodeBlocks/{index.js → index.ts} +14 -15
  38. package/src/remark/utils/index.ts +37 -0
  39. package/src/index.js +0 -107
  40. package/src/remark/headings/__tests__/index.test.js +0 -315
  41. package/src/remark/toc/__tests__/__snapshots__/index.test.js.snap +0 -85
  42. package/src/remark/toc/__tests__/fixtures/empty-headings.md +0 -5
  43. package/src/remark/toc/__tests__/fixtures/inline-code.md +0 -9
  44. package/src/remark/toc/__tests__/fixtures/insert-below-imports.md +0 -11
  45. package/src/remark/toc/__tests__/fixtures/just-content.md +0 -15
  46. package/src/remark/toc/__tests__/fixtures/name-exist.md +0 -7
  47. package/src/remark/toc/__tests__/fixtures/non-text-content.md +0 -9
  48. package/src/remark/toc/__tests__/index.test.js +0 -212
  49. package/src/remark/toc/index.js +0 -79
  50. package/src/remark/toc/search.js +0 -73
  51. package/src/remark/transformImage/__tests__/__snapshots__/index.test.js.snap +0 -31
  52. package/src/remark/transformImage/__tests__/fixtures/fail.md +0 -1
  53. package/src/remark/transformImage/__tests__/fixtures/img.md +0 -17
  54. package/src/remark/transformImage/__tests__/fixtures/img.png +0 -0
  55. package/src/remark/transformImage/__tests__/fixtures/noUrl.md +0 -1
  56. package/src/remark/transformImage/__tests__/fixtures/pathname.md +0 -1
  57. package/src/remark/transformImage/__tests__/index.test.js +0 -59
  58. package/src/remark/transformImage/index.js +0 -106
  59. package/src/remark/transformLinks/__tests__/__snapshots__/index.test.js.snap +0 -41
  60. package/src/remark/transformLinks/__tests__/fixtures/asset.md +0 -29
  61. package/src/remark/transformLinks/__tests__/fixtures/asset.pdf +0 -0
  62. package/src/remark/transformLinks/__tests__/fixtures/noUrl.md +0 -1
  63. package/src/remark/transformLinks/__tests__/fixtures/pathname.md +0 -1
  64. package/src/remark/transformLinks/__tests__/fixtures/static/staticAsset.pdf +0 -0
  65. package/src/remark/transformLinks/__tests__/fixtures/static/staticAssetImage.png +0 -0
  66. package/src/remark/transformLinks/__tests__/index.test.js +0 -44
  67. package/src/remark/transformLinks/index.js +0 -143
  68. package/src/remark/unwrapMdxCodeBlocks/__tests__/__snapshots__/index.test.js.snap +0 -819
  69. package/src/remark/unwrapMdxCodeBlocks/__tests__/fixtures/has-mdx-code-blocks.mdx +0 -95
  70. package/src/remark/unwrapMdxCodeBlocks/__tests__/index.test.js +0 -38
  71. package/src/remark/utils/index.js +0 -39
  72. package/tsconfig.json +0 -9
@@ -1,315 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- /* Based on remark-slug (https://github.com/remarkjs/remark-slug) and gatsby-remark-autolink-headers (https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-autolink-headers) */
9
-
10
- /* eslint-disable no-param-reassign */
11
-
12
- import remark from 'remark';
13
- import u from 'unist-builder';
14
- import removePosition from 'unist-util-remove-position';
15
- import toString from 'mdast-util-to-string';
16
- import visit from 'unist-util-visit';
17
- import slug from '../index';
18
-
19
- function process(doc, plugins = []) {
20
- const processor = remark().use({plugins: [...plugins, slug]});
21
- return removePosition(processor.runSync(processor.parse(doc)), true);
22
- }
23
-
24
- function heading(label, id) {
25
- return u(
26
- 'heading',
27
- {depth: 2, data: {id, hProperties: {id}}},
28
- label ? [u('text', label)] : [],
29
- );
30
- }
31
-
32
- describe('headings plugin', () => {
33
- test('should patch `id`s and `data.hProperties.id', () => {
34
- const result = process('# Normal\n\n## Table of Contents\n\n# Baz\n');
35
- const expected = u('root', [
36
- u(
37
- 'heading',
38
- {depth: 1, data: {hProperties: {id: 'normal'}, id: 'normal'}},
39
- [u('text', 'Normal')],
40
- ),
41
- u(
42
- 'heading',
43
- {
44
- depth: 2,
45
- data: {
46
- hProperties: {id: 'table-of-contents'},
47
- id: 'table-of-contents',
48
- },
49
- },
50
- [u('text', 'Table of Contents')],
51
- ),
52
- u('heading', {depth: 1, data: {hProperties: {id: 'baz'}, id: 'baz'}}, [
53
- u('text', 'Baz'),
54
- ]),
55
- ]);
56
-
57
- expect(result).toEqual(expected);
58
- });
59
-
60
- test('should not overwrite `data` on headings', () => {
61
- const result = process('# Normal\n', [
62
- function () {
63
- function transform(tree) {
64
- tree.children[0].data = {foo: 'bar'};
65
- }
66
- return transform;
67
- },
68
- ]);
69
- const expected = u('root', [
70
- u(
71
- 'heading',
72
- {
73
- depth: 1,
74
- data: {foo: 'bar', hProperties: {id: 'normal'}, id: 'normal'},
75
- },
76
- [u('text', 'Normal')],
77
- ),
78
- ]);
79
-
80
- expect(result).toEqual(expected);
81
- });
82
-
83
- test('should not overwrite `data.hProperties` on headings', () => {
84
- const result = process('# Normal\n', [
85
- function () {
86
- function transform(tree) {
87
- tree.children[0].data = {hProperties: {className: ['foo']}};
88
- }
89
- return transform;
90
- },
91
- ]);
92
- const expected = u('root', [
93
- u(
94
- 'heading',
95
- {
96
- depth: 1,
97
- data: {hProperties: {className: ['foo'], id: 'normal'}, id: 'normal'},
98
- },
99
- [u('text', 'Normal')],
100
- ),
101
- ]);
102
-
103
- expect(result).toEqual(expected);
104
- });
105
-
106
- test('should generate `id`s and `hProperties.id`s, based on `hProperties.id` if they exist', () => {
107
- const result = process(
108
- [
109
- '## Something',
110
- '## Something here',
111
- '## Something there',
112
- '## Something also',
113
- ].join('\n\n'),
114
- [
115
- function () {
116
- function transform(tree) {
117
- tree.children[1].data = {hProperties: {id: 'here'}};
118
- tree.children[3].data = {hProperties: {id: 'something'}};
119
- }
120
- return transform;
121
- },
122
- ],
123
- );
124
- const expected = u('root', [
125
- u(
126
- 'heading',
127
- {
128
- depth: 2,
129
- data: {hProperties: {id: 'something'}, id: 'something'},
130
- },
131
- [u('text', 'Something')],
132
- ),
133
- u(
134
- 'heading',
135
- {
136
- depth: 2,
137
- data: {hProperties: {id: 'here'}, id: 'here'},
138
- },
139
- [u('text', 'Something here')],
140
- ),
141
- u(
142
- 'heading',
143
- {
144
- depth: 2,
145
- data: {hProperties: {id: 'something-there'}, id: 'something-there'},
146
- },
147
- [u('text', 'Something there')],
148
- ),
149
- u(
150
- 'heading',
151
- {
152
- depth: 2,
153
- data: {hProperties: {id: 'something-1'}, id: 'something-1'},
154
- },
155
- [u('text', 'Something also')],
156
- ),
157
- ]);
158
-
159
- expect(result).toEqual(expected);
160
- });
161
-
162
- test('should create GitHub-style headings ids', () => {
163
- const result = process(
164
- [
165
- '## I ♥ unicode',
166
- '',
167
- '## Dash-dash',
168
- '',
169
- '## en–dash',
170
- '',
171
- '## em–dash',
172
- '',
173
- '## 😄 unicode emoji',
174
- '',
175
- '## 😄-😄 unicode emoji',
176
- '',
177
- '## 😄_😄 unicode emoji',
178
- '',
179
- '##',
180
- '',
181
- '## ',
182
- '',
183
- '## Initial spaces',
184
- '',
185
- '## Final spaces ',
186
- '',
187
- '## Duplicate',
188
- '',
189
- '## Duplicate',
190
- '',
191
- '## :ok: No underscore',
192
- '',
193
- '## :ok_hand: Single',
194
- '',
195
- '## :ok_hand::hatched_chick: Two in a row with no spaces',
196
- '',
197
- '## :ok_hand: :hatched_chick: Two in a row',
198
- '',
199
- ].join('\n'),
200
- );
201
- const expected = u('root', [
202
- heading('I ♥ unicode', 'i--unicode'),
203
- heading('Dash-dash', 'dash-dash'),
204
- heading('en–dash', 'endash'),
205
- heading('em–dash', 'emdash'),
206
- heading('😄 unicode emoji', '-unicode-emoji'),
207
- heading('😄-😄 unicode emoji', '--unicode-emoji'),
208
- heading('😄_😄 unicode emoji', '_-unicode-emoji'),
209
- heading(null, ''),
210
- heading(null, '-1'),
211
- heading('Initial spaces', 'initial-spaces'),
212
- heading('Final spaces', 'final-spaces'),
213
- heading('Duplicate', 'duplicate'),
214
- heading('Duplicate', 'duplicate-1'),
215
- heading(':ok: No underscore', 'ok-no-underscore'),
216
- heading(':ok_hand: Single', 'ok_hand-single'),
217
- heading(
218
- ':ok_hand::hatched_chick: Two in a row with no spaces',
219
- 'ok_handhatched_chick-two-in-a-row-with-no-spaces',
220
- ),
221
- heading(
222
- ':ok_hand: :hatched_chick: Two in a row',
223
- 'ok_hand-hatched_chick-two-in-a-row',
224
- ),
225
- ]);
226
-
227
- expect(result).toEqual(expected);
228
- });
229
-
230
- test('should generate id from only text contents of headings if they contains HTML tags', () => {
231
- const result = process('# <span class="normal-header">Normal</span>\n');
232
- const expected = u('root', [
233
- u(
234
- 'heading',
235
- {
236
- depth: 1,
237
- data: {hProperties: {id: 'normal'}, id: 'normal'},
238
- },
239
- [
240
- u('html', '<span class="normal-header">'),
241
- u('text', 'Normal'),
242
- u('html', '</span>'),
243
- ],
244
- ),
245
- ]);
246
-
247
- expect(result).toEqual(expected);
248
- });
249
-
250
- test('should create custom headings ids', () => {
251
- const result = process(`
252
- # Heading One {#custom_h1}
253
-
254
- ## Heading Two {#custom-heading-two}
255
-
256
- # With *Bold* {#custom-withbold}
257
-
258
- # With *Bold* hello{#custom-withbold-hello}
259
-
260
- # With *Bold* hello2 {#custom-withbold-hello2}
261
-
262
- # Snake-cased ID {#this_is_custom_id}
263
-
264
- # No custom ID
265
-
266
- # {#id-only}
267
-
268
- # {#text-after} custom ID
269
- `);
270
-
271
- const headers = [];
272
- visit(result, 'heading', (node) => {
273
- headers.push({text: toString(node), id: node.data.id});
274
- });
275
-
276
- expect(headers).toEqual([
277
- {
278
- id: 'custom_h1',
279
- text: 'Heading One',
280
- },
281
- {
282
- id: 'custom-heading-two',
283
- text: 'Heading Two',
284
- },
285
- {
286
- id: 'custom-withbold',
287
- text: 'With Bold',
288
- },
289
- {
290
- id: 'custom-withbold-hello',
291
- text: 'With Bold hello',
292
- },
293
- {
294
- id: 'custom-withbold-hello2',
295
- text: 'With Bold hello2',
296
- },
297
- {
298
- id: 'this_is_custom_id',
299
- text: 'Snake-cased ID',
300
- },
301
- {
302
- id: 'no-custom-id',
303
- text: 'No custom ID',
304
- },
305
- {
306
- id: 'id-only',
307
- text: '',
308
- },
309
- {
310
- id: 'text-after-custom-id',
311
- text: '{#text-after} custom ID',
312
- },
313
- ]);
314
- });
315
- });
@@ -1,85 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`inline code should be escaped 1`] = `
4
- "export const toc = [
5
- {
6
- value: '<code>&lt;Head /&gt;</code>',
7
- id: 'head-',
8
- children: [
9
- {
10
- value: '<code>&lt;Head&gt;Test&lt;/Head&gt;</code>',
11
- id: 'headtesthead',
12
- children: []
13
- }
14
- ]
15
- },
16
- {
17
- value: '<code>&lt;div /&gt;</code>',
18
- id: 'div-',
19
- children: []
20
- },
21
- {
22
- value: '<code>&lt;div&gt; Test &lt;/div&gt;</code>',
23
- id: 'div-test-div',
24
- children: []
25
- },
26
- {
27
- value: '<code>&lt;div&gt;&lt;i&gt;Test&lt;/i&gt;&lt;/div&gt;</code>',
28
- id: 'divitestidiv',
29
- children: []
30
- }
31
- ];
32
-
33
- ## \`<Head />\`
34
-
35
- ### \`<Head>Test</Head>\`
36
-
37
- ## \`<div />\`
38
-
39
- ## \`<div> Test </div>\`
40
-
41
- ## \`<div><i>Test</i></div>\`
42
- "
43
- `;
44
-
45
- exports[`non text phrasing content 1`] = `
46
- "export const toc = [
47
- {
48
- value: '<em>Emphasis</em>',
49
- id: 'emphasis',
50
- children: [
51
- {
52
- value: '<strong>Importance</strong>',
53
- id: 'importance',
54
- children: []
55
- }
56
- ]
57
- },
58
- {
59
- value: '<del>Strikethrough</del>',
60
- id: 'strikethrough',
61
- children: []
62
- },
63
- {
64
- value: '<i>HTML</i>',
65
- id: 'html',
66
- children: []
67
- },
68
- {
69
- value: '<code>inline.code()</code>',
70
- id: 'inlinecode',
71
- children: []
72
- }
73
- ];
74
-
75
- ## _Emphasis_
76
-
77
- ### **Importance**
78
-
79
- ## ~~Strikethrough~~
80
-
81
- ## <i>HTML</i>
82
-
83
- ## \`inline.code()\`
84
- "
85
- `;
@@ -1,5 +0,0 @@
1
- # Ignore this
2
-
3
- ##
4
-
5
- ## ![](an-image.svg)
@@ -1,9 +0,0 @@
1
- ## `<Head />`
2
-
3
- ### `<Head>Test</Head>`
4
-
5
- ## `<div />`
6
-
7
- ## `<div> Test </div>`
8
-
9
- ## `<div><i>Test</i></div>`
@@ -1,11 +0,0 @@
1
- import something from 'something';
2
-
3
- import somethingElse from 'something-else';
4
-
5
- ## Title
6
-
7
- ## Test
8
-
9
- ### Again
10
-
11
- Content.
@@ -1,15 +0,0 @@
1
- ### Endi
2
-
3
- ```md
4
- ## This is ignored
5
- ```
6
-
7
- ## Endi
8
-
9
- Lorem ipsum
10
-
11
- ### Yangshun
12
-
13
- Some content here
14
-
15
- ## I ♥ unicode.
@@ -1,7 +0,0 @@
1
- export const toc = ['replaceMe'];
2
-
3
- ## Thanos
4
-
5
- ## Tony Stark
6
-
7
- ### Avengers
@@ -1,9 +0,0 @@
1
- ## _Emphasis_
2
-
3
- ### **Importance**
4
-
5
- ## ~~Strikethrough~~
6
-
7
- ## <i>HTML</i>
8
-
9
- ## `inline.code()`
@@ -1,212 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import {join} from 'path';
9
- import remark from 'remark';
10
- import mdx from 'remark-mdx';
11
- import vfile from 'to-vfile';
12
- import plugin from '../index';
13
- import headings from '../../headings/index';
14
-
15
- const processFixture = async (name, options) => {
16
- const path = join(__dirname, 'fixtures', `${name}.md`);
17
- const file = await vfile.read(path);
18
- const result = await remark()
19
- .use(headings)
20
- .use(mdx)
21
- .use(plugin, options)
22
- .process(file);
23
-
24
- return result.toString();
25
- };
26
-
27
- test('non text phrasing content', async () => {
28
- const result = await processFixture('non-text-content');
29
- expect(result).toMatchSnapshot();
30
- });
31
-
32
- test('inline code should be escaped', async () => {
33
- const result = await processFixture('inline-code');
34
- expect(result).toMatchSnapshot();
35
- });
36
-
37
- test('text content', async () => {
38
- const result = await processFixture('just-content');
39
- expect(result).toMatchInlineSnapshot(`
40
- "export const toc = [
41
- {
42
- value: 'Endi',
43
- id: 'endi',
44
- children: []
45
- },
46
- {
47
- value: 'Endi',
48
- id: 'endi-1',
49
- children: [
50
- {
51
- value: 'Yangshun',
52
- id: 'yangshun',
53
- children: []
54
- }
55
- ]
56
- },
57
- {
58
- value: 'I ♥ unicode.',
59
- id: 'i--unicode',
60
- children: []
61
- }
62
- ];
63
-
64
- ### Endi
65
-
66
- \`\`\`md
67
- ## This is ignored
68
- \`\`\`
69
-
70
- ## Endi
71
-
72
- Lorem ipsum
73
-
74
- ### Yangshun
75
-
76
- Some content here
77
-
78
- ## I ♥ unicode.
79
- "
80
- `);
81
- });
82
-
83
- test('should export even with existing name', async () => {
84
- const result = await processFixture('name-exist');
85
- expect(result).toMatchInlineSnapshot(`
86
- "export const toc = [
87
- {
88
- value: 'Thanos',
89
- id: 'thanos',
90
- children: []
91
- },
92
- {
93
- value: 'Tony Stark',
94
- id: 'tony-stark',
95
- children: [
96
- {
97
- value: 'Avengers',
98
- id: 'avengers',
99
- children: []
100
- }
101
- ]
102
- }
103
- ];
104
-
105
- ## Thanos
106
-
107
- ## Tony Stark
108
-
109
- ### Avengers
110
- "
111
- `);
112
- });
113
-
114
- test('should export with custom name', async () => {
115
- const options = {
116
- name: 'customName',
117
- };
118
- const result = await processFixture('just-content', options);
119
- expect(result).toMatchInlineSnapshot(`
120
- "export const customName = [
121
- {
122
- value: 'Endi',
123
- id: 'endi',
124
- children: []
125
- },
126
- {
127
- value: 'Endi',
128
- id: 'endi-1',
129
- children: [
130
- {
131
- value: 'Yangshun',
132
- id: 'yangshun',
133
- children: []
134
- }
135
- ]
136
- },
137
- {
138
- value: 'I ♥ unicode.',
139
- id: 'i--unicode',
140
- children: []
141
- }
142
- ];
143
-
144
- ### Endi
145
-
146
- \`\`\`md
147
- ## This is ignored
148
- \`\`\`
149
-
150
- ## Endi
151
-
152
- Lorem ipsum
153
-
154
- ### Yangshun
155
-
156
- Some content here
157
-
158
- ## I ♥ unicode.
159
- "
160
- `);
161
- });
162
-
163
- test('should insert below imports', async () => {
164
- const result = await processFixture('insert-below-imports');
165
- expect(result).toMatchInlineSnapshot(`
166
- "import something from 'something';
167
-
168
- import somethingElse from 'something-else';
169
-
170
- export const toc = [
171
- {
172
- value: 'Title',
173
- id: 'title',
174
- children: []
175
- },
176
- {
177
- value: 'Test',
178
- id: 'test',
179
- children: [
180
- {
181
- value: 'Again',
182
- id: 'again',
183
- children: []
184
- }
185
- ]
186
- }
187
- ];
188
-
189
- ## Title
190
-
191
- ## Test
192
-
193
- ### Again
194
-
195
- Content.
196
- "
197
- `);
198
- });
199
-
200
- test('empty headings', async () => {
201
- const result = await processFixture('empty-headings');
202
- expect(result).toMatchInlineSnapshot(`
203
- "export const toc = [];
204
-
205
- # Ignore this
206
-
207
- ##
208
-
209
- ## ![](an-image.svg)
210
- "
211
- `);
212
- });