@docusaurus/mdx-loader 2.0.0-beta.12faed89d → 2.0.0-beta.13
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/lib/.tsbuildinfo +1 -0
- package/lib/index.d.ts +23 -0
- package/lib/index.js +167 -0
- package/lib/remark/headings/index.d.ts +9 -0
- package/lib/remark/headings/index.js +60 -0
- package/lib/remark/toc/index.d.ts +12 -0
- package/lib/remark/toc/index.js +67 -0
- package/lib/remark/toc/search.d.ts +13 -0
- package/lib/remark/toc/search.js +66 -0
- package/lib/remark/transformImage/index.d.ts +13 -0
- package/lib/remark/transformImage/index.js +90 -0
- package/lib/remark/transformLinks/index.d.ts +14 -0
- package/lib/remark/transformLinks/index.js +106 -0
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts +9 -0
- package/lib/remark/unwrapMdxCodeBlocks/index.js +28 -0
- package/lib/remark/utils/index.d.ts +10 -0
- package/lib/remark/utils/index.js +39 -0
- package/package.json +16 -12
- package/src/deps.d.ts +29 -0
- package/src/index.ts +216 -0
- package/src/{index.d.ts → mdx-loader.d.ts} +7 -8
- package/src/remark/headings/__tests__/{index.test.js → index.test.ts} +0 -2
- package/src/remark/headings/{index.js → index.ts} +21 -16
- package/src/remark/toc/__tests__/__snapshots__/{index.test.js.snap → index.test.ts.snap} +20 -10
- package/src/remark/toc/__tests__/{index.test.js → index.test.ts} +29 -15
- package/src/remark/toc/__tests__/search.test.ts +182 -0
- package/src/remark/toc/{index.js → index.ts} +24 -16
- package/src/remark/toc/search.ts +82 -0
- package/src/remark/transformImage/__tests__/{fixtures → __fixtures__}/fail.md +0 -0
- package/src/remark/transformImage/__tests__/__fixtures__/img.md +19 -0
- package/src/remark/transformImage/__tests__/{fixtures → __fixtures__}/noUrl.md +0 -0
- package/src/remark/transformImage/__tests__/{fixtures → __fixtures__}/pathname.md +0 -0
- package/src/remark/transformImage/__tests__/{fixtures → __fixtures__/static}/img.png +0 -0
- package/src/remark/{transformLinks/__tests__/fixtures/asset.pdf → transformImage/__tests__/__fixtures__/static2/img2.png} +0 -0
- package/src/remark/transformImage/__tests__/__snapshots__/index.test.ts.snap +33 -0
- package/src/remark/transformImage/__tests__/{index.test.js → index.test.ts} +23 -19
- package/src/remark/transformImage/{index.js → index.ts} +52 -24
- package/src/remark/transformLinks/__tests__/{fixtures → __fixtures__}/asset.md +6 -0
- package/src/remark/transformLinks/__tests__/{fixtures/static/staticAsset.pdf → __fixtures__/asset.pdf} +0 -0
- package/src/remark/transformLinks/__tests__/{fixtures → __fixtures__}/noUrl.md +0 -0
- package/src/remark/transformLinks/__tests__/{fixtures → __fixtures__}/pathname.md +0 -0
- package/src/remark/transformLinks/__tests__/__fixtures__/static/staticAsset.pdf +0 -0
- package/src/remark/transformLinks/__tests__/{fixtures → __fixtures__}/static/staticAssetImage.png +0 -0
- package/src/remark/transformLinks/__tests__/__fixtures__/static2/asset2.pdf +0 -0
- package/src/remark/transformLinks/__tests__/__snapshots__/index.test.ts.snap +47 -0
- package/src/remark/transformLinks/__tests__/{index.test.js → index.test.ts} +19 -8
- package/src/remark/transformLinks/{index.js → index.ts} +66 -44
- package/src/remark/unwrapMdxCodeBlocks/__tests__/__snapshots__/{index.test.js.snap → index.test.ts.snap} +0 -0
- package/src/remark/unwrapMdxCodeBlocks/__tests__/{index.test.js → index.test.ts} +0 -0
- package/src/remark/unwrapMdxCodeBlocks/{index.js → index.ts} +11 -9
- package/src/remark/utils/index.ts +39 -0
- package/tsconfig.json +5 -5
- package/src/index.js +0 -107
- package/src/remark/toc/search.js +0 -73
- package/src/remark/transformImage/__tests__/__snapshots__/index.test.js.snap +0 -31
- package/src/remark/transformImage/__tests__/fixtures/img.md +0 -17
- package/src/remark/transformLinks/__tests__/__snapshots__/index.test.js.snap +0 -41
- package/src/remark/utils/index.js +0 -39
package/src/index.js
DELETED
|
@@ -1,107 +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
|
-
const {content, contentTitle} = parseMarkdownContentTitle(contentWithTitle, {
|
|
35
|
-
removeContentTitle: reqOptions.removeContentTitle,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
39
|
-
|
|
40
|
-
const options = {
|
|
41
|
-
...reqOptions,
|
|
42
|
-
remarkPlugins: [
|
|
43
|
-
...(reqOptions.beforeDefaultRemarkPlugins || []),
|
|
44
|
-
...DEFAULT_OPTIONS.remarkPlugins,
|
|
45
|
-
[
|
|
46
|
-
transformImage,
|
|
47
|
-
{staticDir: reqOptions.staticDir, filePath: this.resourcePath},
|
|
48
|
-
],
|
|
49
|
-
[
|
|
50
|
-
transformLinks,
|
|
51
|
-
{staticDir: reqOptions.staticDir, filePath: this.resourcePath},
|
|
52
|
-
],
|
|
53
|
-
...(reqOptions.remarkPlugins || []),
|
|
54
|
-
],
|
|
55
|
-
rehypePlugins: [
|
|
56
|
-
...(reqOptions.beforeDefaultRehypePlugins || []),
|
|
57
|
-
...DEFAULT_OPTIONS.rehypePlugins,
|
|
58
|
-
|
|
59
|
-
...(reqOptions.rehypePlugins || []),
|
|
60
|
-
],
|
|
61
|
-
filepath: this.resourcePath,
|
|
62
|
-
};
|
|
63
|
-
let result;
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
result = await mdx(content, options);
|
|
67
|
-
} catch (err) {
|
|
68
|
-
return callback(err);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
let exportStr = ``;
|
|
72
|
-
exportStr += `\nexport const frontMatter = ${stringifyObject(frontMatter)};`;
|
|
73
|
-
exportStr += `\nexport const contentTitle = ${stringifyObject(
|
|
74
|
-
contentTitle,
|
|
75
|
-
)};`;
|
|
76
|
-
|
|
77
|
-
// Read metadata for this MDX and export it.
|
|
78
|
-
if (options.metadataPath && typeof options.metadataPath === 'function') {
|
|
79
|
-
const metadataPath = options.metadataPath(this.resourcePath);
|
|
80
|
-
|
|
81
|
-
if (metadataPath) {
|
|
82
|
-
// Add as dependency of this loader result so that we can
|
|
83
|
-
// recompile if metadata is changed.
|
|
84
|
-
this.addDependency(metadataPath);
|
|
85
|
-
const metadata = await readFile(metadataPath, 'utf8');
|
|
86
|
-
exportStr += `\nexport const metadata = ${metadata};`;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (
|
|
91
|
-
options.forbidFrontMatter &&
|
|
92
|
-
typeof options.forbidFrontMatter === 'function'
|
|
93
|
-
) {
|
|
94
|
-
if (options.forbidFrontMatter(this.resourcePath) && hasFrontMatter) {
|
|
95
|
-
return callback(new Error(`Front matter is forbidden in this file`));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const code = `
|
|
99
|
-
import React from 'react';
|
|
100
|
-
import { mdx } from '@mdx-js/react';
|
|
101
|
-
|
|
102
|
-
${exportStr}
|
|
103
|
-
${result}
|
|
104
|
-
`;
|
|
105
|
-
|
|
106
|
-
return callback(null, code);
|
|
107
|
-
};
|
package/src/remark/toc/search.js
DELETED
|
@@ -1,73 +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
|
-
// @ts-check
|
|
9
|
-
|
|
10
|
-
const toString = require('mdast-util-to-string');
|
|
11
|
-
const visit = require('unist-util-visit');
|
|
12
|
-
// Destructuring require tslib
|
|
13
|
-
// eslint-disable-next-line prefer-destructuring
|
|
14
|
-
const toValue = require('../utils').toValue;
|
|
15
|
-
|
|
16
|
-
/** @typedef {import('@docusaurus/types').TOCItem} TOC */
|
|
17
|
-
/** @typedef {import('unist').Node} Node */
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @typedef {Object} StringValuedNode
|
|
21
|
-
* @property {string} type
|
|
22
|
-
* @property {string} value
|
|
23
|
-
* @property {number} depth
|
|
24
|
-
* @property {Object} data
|
|
25
|
-
* @property {StringValuedNode[]} children
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
// Visit all headings. We `slug` all headings (to account for
|
|
29
|
-
// duplicates), but only take h2 and h3 headings.
|
|
30
|
-
/**
|
|
31
|
-
* @param {StringValuedNode} node
|
|
32
|
-
* @returns {TOC[]}
|
|
33
|
-
*/
|
|
34
|
-
function search(node) {
|
|
35
|
-
/** @type {TOC[]} */
|
|
36
|
-
const headings = [];
|
|
37
|
-
let current = -1;
|
|
38
|
-
let currentDepth = 0;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @param {StringValuedNode} child
|
|
42
|
-
* @param {number} index
|
|
43
|
-
* @param {Node | undefined} parent
|
|
44
|
-
* @returns {void}
|
|
45
|
-
*/
|
|
46
|
-
const onHeading = (child, index, parent) => {
|
|
47
|
-
const value = toString(child);
|
|
48
|
-
|
|
49
|
-
if (parent !== node || !value || child.depth > 3 || child.depth < 2) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const entry = {
|
|
54
|
-
value: toValue(child),
|
|
55
|
-
id: child.data.id,
|
|
56
|
-
children: [],
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
if (!headings.length || currentDepth >= child.depth) {
|
|
60
|
-
headings.push(entry);
|
|
61
|
-
current += 1;
|
|
62
|
-
currentDepth = child.depth;
|
|
63
|
-
} else {
|
|
64
|
-
headings[current].children.push(entry);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
visit(node, 'heading', onHeading);
|
|
69
|
-
|
|
70
|
-
return headings;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
module.exports = search;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`transformImage plugin fail if image does not exist 1`] = `"Image packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/fixtures/img/doesNotExist.png used in packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/fixtures/fail.md not found."`;
|
|
4
|
-
|
|
5
|
-
exports[`transformImage plugin fail if image url is absent 1`] = `"Markdown image URL is mandatory in \\"packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/fixtures/noUrl.md\\" file"`;
|
|
6
|
-
|
|
7
|
-
exports[`transformImage plugin pathname protocol 1`] = `
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
`;
|
|
11
|
-
|
|
12
|
-
exports[`transformImage plugin transform md images to <img /> 1`] = `
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
<img src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./img.png\\").default} />
|
|
16
|
-
|
|
17
|
-
<img alt={\\"img\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./img.png\\").default} />
|
|
18
|
-
|
|
19
|
-
<img alt={\\"img\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./img.png\\").default} title=\\"Title\\" /> <img alt={\\"img\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/fixtures/img.png\\").default} />
|
|
20
|
-
|
|
21
|
-
<img alt={\\"img with "quotes"\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./img.png\\").default} title=\\"'Quoted' title\\" />
|
|
22
|
-
|
|
23
|
-
## Heading
|
|
24
|
-
|
|
25
|
-
\`\`\`md
|
|
26
|
-

|
|
27
|
-
\`\`\`
|
|
28
|
-
|
|
29
|
-
<img alt={\\"img\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./img.png\\").default} />
|
|
30
|
-
"
|
|
31
|
-
`;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-

|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
 
|
|
8
|
-
|
|
9
|
-

|
|
10
|
-
|
|
11
|
-
## Heading
|
|
12
|
-
|
|
13
|
-
```md
|
|
14
|
-

|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-

|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`transformAsset plugin fail if asset url is absent 1`] = `"Markdown link URL is mandatory in \\"packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/fixtures/noUrl.md\\" file (title: asset, line: 1)."`;
|
|
4
|
-
|
|
5
|
-
exports[`transformAsset plugin pathname protocol 1`] = `
|
|
6
|
-
"[asset](pathname:///asset/unchecked.pdf)
|
|
7
|
-
"
|
|
8
|
-
`;
|
|
9
|
-
|
|
10
|
-
exports[`transformAsset plugin transform md links to <a /> 1`] = `
|
|
11
|
-
"[asset](https://example.com/asset.pdf)
|
|
12
|
-
|
|
13
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}></a>
|
|
14
|
-
|
|
15
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}>asset</a>
|
|
16
|
-
|
|
17
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}title=\\"Title\\">asset</a>
|
|
18
|
-
|
|
19
|
-
## Heading
|
|
20
|
-
|
|
21
|
-
\`\`\`md
|
|
22
|
-
[asset](./asset.pdf)
|
|
23
|
-
\`\`\`
|
|
24
|
-
|
|
25
|
-
[assets](!file-loader!./asset.pdf)
|
|
26
|
-
|
|
27
|
-
[assets](/github/!file-loader!/assets.pdf)
|
|
28
|
-
|
|
29
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}>asset</a>
|
|
30
|
-
|
|
31
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>staticAsset.pdf</a>
|
|
32
|
-
|
|
33
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>@site/static/staticAsset.pdf</a>
|
|
34
|
-
|
|
35
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>Just staticAsset.pdf</a>, and <a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}><strong>awesome</strong> staticAsset 2.pdf 'It is really "AWESOME"'</a>, but also <a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>coded <code>staticAsset 3.pdf</code></a>
|
|
36
|
-
|
|
37
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAssetImage.png').default}><img alt={\\"Clickable Docusaurus logo\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./static/staticAssetImage.png\\").default} /></a>
|
|
38
|
-
|
|
39
|
-
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}><span style={{color: \\"red\\"}}>Stylized link to asset file</span></a>
|
|
40
|
-
"
|
|
41
|
-
`;
|
|
@@ -1,39 +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 escapeHtml = require('escape-html');
|
|
9
|
-
const toString = require('mdast-util-to-string');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param {StringValuedNode | undefined} node
|
|
13
|
-
* @returns {string}
|
|
14
|
-
*/
|
|
15
|
-
function toValue(node) {
|
|
16
|
-
if (node && node.type) {
|
|
17
|
-
switch (node.type) {
|
|
18
|
-
case 'text':
|
|
19
|
-
return escapeHtml(node.value);
|
|
20
|
-
case 'heading':
|
|
21
|
-
return node.children.map(toValue).join('');
|
|
22
|
-
case 'inlineCode':
|
|
23
|
-
return `<code>${escapeHtml(node.value)}</code>`;
|
|
24
|
-
case 'emphasis':
|
|
25
|
-
return `<em>${node.children.map(toValue).join('')}</em>`;
|
|
26
|
-
case 'strong':
|
|
27
|
-
return `<strong>${node.children.map(toValue).join('')}</strong>`;
|
|
28
|
-
case 'delete':
|
|
29
|
-
return `<del>${node.children.map(toValue).join('')}</del>`;
|
|
30
|
-
default:
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return toString(node);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
module.exports = {
|
|
38
|
-
toValue,
|
|
39
|
-
};
|