@docusaurus/mdx-loader 2.0.0-beta.1decd6f80 → 2.0.0-beta.20

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 (77) hide show
  1. package/lib/index.d.ts +33 -0
  2. package/lib/index.d.ts.map +1 -0
  3. package/lib/index.js +11 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/loader.d.ts +39 -0
  6. package/lib/loader.d.ts.map +1 -0
  7. package/lib/loader.js +194 -0
  8. package/lib/loader.js.map +1 -0
  9. package/lib/remark/headings/index.d.ts +9 -0
  10. package/lib/remark/headings/index.d.ts.map +1 -0
  11. package/lib/remark/headings/index.js +58 -0
  12. package/lib/remark/headings/index.js.map +1 -0
  13. package/lib/remark/toc/index.d.ts +13 -0
  14. package/lib/remark/toc/index.d.ts.map +1 -0
  15. package/lib/remark/toc/index.js +81 -0
  16. package/lib/remark/toc/index.js.map +1 -0
  17. package/lib/remark/transformImage/index.d.ts +14 -0
  18. package/lib/remark/transformImage/index.d.ts.map +1 -0
  19. package/lib/remark/transformImage/index.js +109 -0
  20. package/lib/remark/transformImage/index.js.map +1 -0
  21. package/lib/remark/transformLinks/index.d.ts +14 -0
  22. package/lib/remark/transformLinks/index.d.ts.map +1 -0
  23. package/lib/remark/transformLinks/index.js +102 -0
  24. package/lib/remark/transformLinks/index.js.map +1 -0
  25. package/lib/remark/unwrapMdxCodeBlocks/index.d.ts +9 -0
  26. package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +1 -0
  27. package/lib/remark/unwrapMdxCodeBlocks/index.js +29 -0
  28. package/lib/remark/unwrapMdxCodeBlocks/index.js.map +1 -0
  29. package/lib/remark/utils/index.d.ts +11 -0
  30. package/lib/remark/utils/index.d.ts.map +1 -0
  31. package/lib/remark/utils/index.js +38 -0
  32. package/lib/remark/utils/index.js.map +1 -0
  33. package/package.json +23 -20
  34. package/src/deps.d.ts +31 -0
  35. package/src/index.ts +35 -0
  36. package/src/loader.ts +253 -0
  37. package/src/remark/headings/{index.js → index.ts} +22 -26
  38. package/src/remark/toc/index.ts +102 -0
  39. package/src/remark/transformImage/index.ts +167 -0
  40. package/src/remark/transformLinks/index.ts +152 -0
  41. package/src/remark/unwrapMdxCodeBlocks/{index.js → index.ts} +14 -15
  42. package/src/remark/utils/index.ts +36 -0
  43. package/src/index.d.ts +0 -19
  44. package/src/index.js +0 -104
  45. package/src/remark/headings/__tests__/index.test.js +0 -315
  46. package/src/remark/toc/__tests__/__snapshots__/index.test.js.snap +0 -85
  47. package/src/remark/toc/__tests__/fixtures/empty-headings.md +0 -5
  48. package/src/remark/toc/__tests__/fixtures/inline-code.md +0 -9
  49. package/src/remark/toc/__tests__/fixtures/insert-below-imports.md +0 -11
  50. package/src/remark/toc/__tests__/fixtures/just-content.md +0 -15
  51. package/src/remark/toc/__tests__/fixtures/name-exist.md +0 -7
  52. package/src/remark/toc/__tests__/fixtures/non-text-content.md +0 -9
  53. package/src/remark/toc/__tests__/index.test.js +0 -212
  54. package/src/remark/toc/index.js +0 -79
  55. package/src/remark/toc/search.js +0 -73
  56. package/src/remark/transformImage/__tests__/__snapshots__/index.test.js.snap +0 -31
  57. package/src/remark/transformImage/__tests__/fixtures/fail.md +0 -1
  58. package/src/remark/transformImage/__tests__/fixtures/img.md +0 -17
  59. package/src/remark/transformImage/__tests__/fixtures/img.png +0 -0
  60. package/src/remark/transformImage/__tests__/fixtures/noUrl.md +0 -1
  61. package/src/remark/transformImage/__tests__/fixtures/pathname.md +0 -1
  62. package/src/remark/transformImage/__tests__/index.test.js +0 -59
  63. package/src/remark/transformImage/index.js +0 -106
  64. package/src/remark/transformLinks/__tests__/__snapshots__/index.test.js.snap +0 -41
  65. package/src/remark/transformLinks/__tests__/fixtures/asset.md +0 -29
  66. package/src/remark/transformLinks/__tests__/fixtures/asset.pdf +0 -0
  67. package/src/remark/transformLinks/__tests__/fixtures/noUrl.md +0 -1
  68. package/src/remark/transformLinks/__tests__/fixtures/pathname.md +0 -1
  69. package/src/remark/transformLinks/__tests__/fixtures/static/staticAsset.pdf +0 -0
  70. package/src/remark/transformLinks/__tests__/fixtures/static/staticAssetImage.png +0 -0
  71. package/src/remark/transformLinks/__tests__/index.test.js +0 -44
  72. package/src/remark/transformLinks/index.js +0 -143
  73. package/src/remark/unwrapMdxCodeBlocks/__tests__/__snapshots__/index.test.js.snap +0 -819
  74. package/src/remark/unwrapMdxCodeBlocks/__tests__/fixtures/has-mdx-code-blocks.mdx +0 -95
  75. package/src/remark/unwrapMdxCodeBlocks/__tests__/index.test.js +0 -38
  76. package/src/remark/utils/index.js +0 -39
  77. package/tsconfig.json +0 -9
package/src/index.js DELETED
@@ -1,104 +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
- const {readFile} = require('fs-extra');
9
- const mdx = require('@mdx-js/mdx');
10
- const emoji = require('remark-emoji');
11
- const {
12
- parseFrontMatter,
13
- parseMarkdownContentTitle,
14
- } = require('@docusaurus/utils');
15
- const stringifyObject = require('stringify-object');
16
- const headings = require('./remark/headings');
17
- const toc = require('./remark/toc');
18
- const unwrapMdxCodeBlocks = require('./remark/unwrapMdxCodeBlocks');
19
- const transformImage = require('./remark/transformImage');
20
- const transformLinks = require('./remark/transformLinks');
21
-
22
- const DEFAULT_OPTIONS = {
23
- rehypePlugins: [],
24
- remarkPlugins: [unwrapMdxCodeBlocks, emoji, headings, toc],
25
- };
26
-
27
- module.exports = async function docusaurusMdxLoader(fileString) {
28
- const callback = this.async();
29
-
30
- const reqOptions = this.getOptions() || {};
31
-
32
- const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);
33
-
34
- // By default, will remove the markdown title from the content
35
- const {content} = parseMarkdownContentTitle(contentWithTitle, {
36
- keepContentTitle: reqOptions.keepContentTitle,
37
- });
38
-
39
- const hasFrontMatter = Object.keys(frontMatter).length > 0;
40
-
41
- const options = {
42
- ...reqOptions,
43
- remarkPlugins: [
44
- ...(reqOptions.beforeDefaultRemarkPlugins || []),
45
- ...DEFAULT_OPTIONS.remarkPlugins,
46
- [
47
- transformImage,
48
- {staticDir: reqOptions.staticDir, filePath: this.resourcePath},
49
- ],
50
- [
51
- transformLinks,
52
- {staticDir: reqOptions.staticDir, filePath: this.resourcePath},
53
- ],
54
- ...(reqOptions.remarkPlugins || []),
55
- ],
56
- rehypePlugins: [
57
- ...(reqOptions.beforeDefaultRehypePlugins || []),
58
- ...DEFAULT_OPTIONS.rehypePlugins,
59
-
60
- ...(reqOptions.rehypePlugins || []),
61
- ],
62
- filepath: this.resourcePath,
63
- };
64
- let result;
65
-
66
- try {
67
- result = await mdx(content, options);
68
- } catch (err) {
69
- return callback(err);
70
- }
71
-
72
- let exportStr = `export const frontMatter = ${stringifyObject(frontMatter)};`;
73
-
74
- // Read metadata for this MDX and export it.
75
- if (options.metadataPath && typeof options.metadataPath === 'function') {
76
- const metadataPath = options.metadataPath(this.resourcePath);
77
-
78
- if (metadataPath) {
79
- // Add as dependency of this loader result so that we can
80
- // recompile if metadata is changed.
81
- this.addDependency(metadataPath);
82
- const metadata = await readFile(metadataPath, 'utf8');
83
- exportStr += `\nexport const metadata = ${metadata};`;
84
- }
85
- }
86
-
87
- if (
88
- options.forbidFrontMatter &&
89
- typeof options.forbidFrontMatter === 'function'
90
- ) {
91
- if (options.forbidFrontMatter(this.resourcePath) && hasFrontMatter) {
92
- return callback(new Error(`Front matter is forbidden in this file`));
93
- }
94
- }
95
- const code = `
96
- import React from 'react';
97
- import { mdx } from '@mdx-js/react';
98
-
99
- ${exportStr}
100
- ${result}
101
- `;
102
-
103
- return callback(null, code);
104
- };
@@ -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()`