@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.
- package/lib/index.d.ts +33 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/loader.d.ts +39 -0
- package/lib/loader.d.ts.map +1 -0
- package/lib/loader.js +194 -0
- package/lib/loader.js.map +1 -0
- package/lib/remark/headings/index.d.ts +9 -0
- package/lib/remark/headings/index.d.ts.map +1 -0
- package/lib/remark/headings/index.js +58 -0
- package/lib/remark/headings/index.js.map +1 -0
- package/lib/remark/toc/index.d.ts +13 -0
- package/lib/remark/toc/index.d.ts.map +1 -0
- package/lib/remark/toc/index.js +81 -0
- package/lib/remark/toc/index.js.map +1 -0
- package/lib/remark/transformImage/index.d.ts +14 -0
- package/lib/remark/transformImage/index.d.ts.map +1 -0
- package/lib/remark/transformImage/index.js +109 -0
- package/lib/remark/transformImage/index.js.map +1 -0
- package/lib/remark/transformLinks/index.d.ts +14 -0
- package/lib/remark/transformLinks/index.d.ts.map +1 -0
- package/lib/remark/transformLinks/index.js +102 -0
- package/lib/remark/transformLinks/index.js.map +1 -0
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts +9 -0
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +1 -0
- package/lib/remark/unwrapMdxCodeBlocks/index.js +29 -0
- package/lib/remark/unwrapMdxCodeBlocks/index.js.map +1 -0
- package/lib/remark/utils/index.d.ts +11 -0
- package/lib/remark/utils/index.d.ts.map +1 -0
- package/lib/remark/utils/index.js +38 -0
- package/lib/remark/utils/index.js.map +1 -0
- package/package.json +23 -20
- package/src/deps.d.ts +31 -0
- package/src/index.ts +35 -0
- package/src/loader.ts +253 -0
- package/src/remark/headings/{index.js → index.ts} +22 -26
- package/src/remark/toc/index.ts +102 -0
- package/src/remark/transformImage/index.ts +167 -0
- package/src/remark/transformLinks/index.ts +152 -0
- package/src/remark/unwrapMdxCodeBlocks/{index.js → index.ts} +14 -15
- package/src/remark/utils/index.ts +36 -0
- package/src/index.d.ts +0 -19
- package/src/index.js +0 -104
- package/src/remark/headings/__tests__/index.test.js +0 -315
- package/src/remark/toc/__tests__/__snapshots__/index.test.js.snap +0 -85
- package/src/remark/toc/__tests__/fixtures/empty-headings.md +0 -5
- package/src/remark/toc/__tests__/fixtures/inline-code.md +0 -9
- package/src/remark/toc/__tests__/fixtures/insert-below-imports.md +0 -11
- package/src/remark/toc/__tests__/fixtures/just-content.md +0 -15
- package/src/remark/toc/__tests__/fixtures/name-exist.md +0 -7
- package/src/remark/toc/__tests__/fixtures/non-text-content.md +0 -9
- package/src/remark/toc/__tests__/index.test.js +0 -212
- package/src/remark/toc/index.js +0 -79
- 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/fail.md +0 -1
- package/src/remark/transformImage/__tests__/fixtures/img.md +0 -17
- package/src/remark/transformImage/__tests__/fixtures/img.png +0 -0
- package/src/remark/transformImage/__tests__/fixtures/noUrl.md +0 -1
- package/src/remark/transformImage/__tests__/fixtures/pathname.md +0 -1
- package/src/remark/transformImage/__tests__/index.test.js +0 -59
- package/src/remark/transformImage/index.js +0 -106
- package/src/remark/transformLinks/__tests__/__snapshots__/index.test.js.snap +0 -41
- package/src/remark/transformLinks/__tests__/fixtures/asset.md +0 -29
- package/src/remark/transformLinks/__tests__/fixtures/asset.pdf +0 -0
- package/src/remark/transformLinks/__tests__/fixtures/noUrl.md +0 -1
- package/src/remark/transformLinks/__tests__/fixtures/pathname.md +0 -1
- package/src/remark/transformLinks/__tests__/fixtures/static/staticAsset.pdf +0 -0
- package/src/remark/transformLinks/__tests__/fixtures/static/staticAssetImage.png +0 -0
- package/src/remark/transformLinks/__tests__/index.test.js +0 -44
- package/src/remark/transformLinks/index.js +0 -143
- package/src/remark/unwrapMdxCodeBlocks/__tests__/__snapshots__/index.test.js.snap +0 -819
- package/src/remark/unwrapMdxCodeBlocks/__tests__/fixtures/has-mdx-code-blocks.mdx +0 -95
- package/src/remark/unwrapMdxCodeBlocks/__tests__/index.test.js +0 -38
- package/src/remark/utils/index.js +0 -39
- package/tsconfig.json +0 -9
|
@@ -1,44 +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 '..';
|
|
13
|
-
import transformImage from '../../transformImage';
|
|
14
|
-
|
|
15
|
-
const processFixture = async (name, options) => {
|
|
16
|
-
const path = join(__dirname, 'fixtures', `${name}.md`);
|
|
17
|
-
const staticDir = join(__dirname, 'fixtures', 'static');
|
|
18
|
-
const file = await vfile.read(path);
|
|
19
|
-
const result = await remark()
|
|
20
|
-
.use(mdx)
|
|
21
|
-
.use(transformImage, {...options, filePath: path, staticDir})
|
|
22
|
-
.use(plugin, {...options, filePath: path, staticDir})
|
|
23
|
-
.process(file);
|
|
24
|
-
|
|
25
|
-
return result.toString();
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
describe('transformAsset plugin', () => {
|
|
29
|
-
test('fail if asset url is absent', async () => {
|
|
30
|
-
await expect(
|
|
31
|
-
processFixture('noUrl'),
|
|
32
|
-
).rejects.toThrowErrorMatchingSnapshot();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('transform md links to <a />', async () => {
|
|
36
|
-
const result = await processFixture('asset');
|
|
37
|
-
expect(result).toMatchSnapshot();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('pathname protocol', async () => {
|
|
41
|
-
const result = await processFixture('pathname');
|
|
42
|
-
expect(result).toMatchSnapshot();
|
|
43
|
-
});
|
|
44
|
-
});
|
|
@@ -1,143 +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 {
|
|
9
|
-
toMessageRelativeFilePath,
|
|
10
|
-
posixPath,
|
|
11
|
-
escapePath,
|
|
12
|
-
} = require('@docusaurus/utils');
|
|
13
|
-
|
|
14
|
-
const visit = require('unist-util-visit');
|
|
15
|
-
const path = require('path');
|
|
16
|
-
const url = require('url');
|
|
17
|
-
const fs = require('fs-extra');
|
|
18
|
-
const escapeHtml = require('escape-html');
|
|
19
|
-
const {toValue} = require('../utils');
|
|
20
|
-
const {getFileLoaderUtils} = require('@docusaurus/core/lib/webpack/utils');
|
|
21
|
-
|
|
22
|
-
const {
|
|
23
|
-
loaders: {inlineMarkdownLinkFileLoader},
|
|
24
|
-
} = getFileLoaderUtils();
|
|
25
|
-
|
|
26
|
-
async function ensureAssetFileExist(fileSystemAssetPath, sourceFilePath) {
|
|
27
|
-
const assetExists = await fs.pathExists(fileSystemAssetPath);
|
|
28
|
-
if (!assetExists) {
|
|
29
|
-
throw new Error(
|
|
30
|
-
`Asset ${toMessageRelativeFilePath(
|
|
31
|
-
fileSystemAssetPath,
|
|
32
|
-
)} used in ${toMessageRelativeFilePath(sourceFilePath)} not found.`,
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// transform the link node to a jsx link with a require() call
|
|
38
|
-
function toAssetRequireNode({node, filePath, requireAssetPath}) {
|
|
39
|
-
/* eslint-disable no-param-reassign */
|
|
40
|
-
|
|
41
|
-
let relativeRequireAssetPath = posixPath(
|
|
42
|
-
path.relative(path.dirname(filePath), requireAssetPath),
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// nodejs does not like require("assets/file.pdf")
|
|
46
|
-
relativeRequireAssetPath = relativeRequireAssetPath.startsWith('.')
|
|
47
|
-
? relativeRequireAssetPath
|
|
48
|
-
: `./${relativeRequireAssetPath}`;
|
|
49
|
-
|
|
50
|
-
const href = `require('${inlineMarkdownLinkFileLoader}${escapePath(
|
|
51
|
-
relativeRequireAssetPath,
|
|
52
|
-
)}').default`;
|
|
53
|
-
const children = (node.children || []).map((n) => toValue(n)).join('');
|
|
54
|
-
const title = node.title ? `title="${escapeHtml(node.title)}"` : '';
|
|
55
|
-
|
|
56
|
-
node.type = 'jsx';
|
|
57
|
-
node.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// If the link looks like an asset link, we'll link to the asset,
|
|
61
|
-
// and use a require("assetUrl") (using webpack url-loader/file-loader)
|
|
62
|
-
// instead of navigating to such link
|
|
63
|
-
async function convertToAssetLinkIfNeeded({node, staticDir, filePath}) {
|
|
64
|
-
const assetPath = node.url;
|
|
65
|
-
|
|
66
|
-
const hasSiteAlias = assetPath.startsWith('@site/');
|
|
67
|
-
const hasAssetLikeExtension =
|
|
68
|
-
path.extname(assetPath) && !assetPath.match(/#|.md|.mdx|.html/);
|
|
69
|
-
|
|
70
|
-
const looksLikeAssetLink = hasSiteAlias || hasAssetLikeExtension;
|
|
71
|
-
|
|
72
|
-
if (!looksLikeAssetLink) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function toAssetLinkNode(requireAssetPath) {
|
|
77
|
-
toAssetRequireNode({
|
|
78
|
-
node,
|
|
79
|
-
filePath,
|
|
80
|
-
requireAssetPath,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (assetPath.startsWith('@site/')) {
|
|
85
|
-
const siteDir = path.join(staticDir, '..');
|
|
86
|
-
const fileSystemAssetPath = path.join(
|
|
87
|
-
siteDir,
|
|
88
|
-
assetPath.replace('@site/', ''),
|
|
89
|
-
);
|
|
90
|
-
await ensureAssetFileExist(fileSystemAssetPath, filePath);
|
|
91
|
-
toAssetLinkNode(fileSystemAssetPath);
|
|
92
|
-
} else if (path.isAbsolute(assetPath)) {
|
|
93
|
-
const fileSystemAssetPath = path.join(staticDir, assetPath);
|
|
94
|
-
if (await fs.pathExists(fileSystemAssetPath)) {
|
|
95
|
-
toAssetLinkNode(fileSystemAssetPath);
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
const fileSystemAssetPath = path.join(path.dirname(filePath), assetPath);
|
|
99
|
-
if (await fs.pathExists(fileSystemAssetPath)) {
|
|
100
|
-
toAssetLinkNode(fileSystemAssetPath);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async function processLinkNode({node, _index, _parent, filePath, staticDir}) {
|
|
106
|
-
if (!node.url) {
|
|
107
|
-
// try to improve error feedback
|
|
108
|
-
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
|
|
109
|
-
const title =
|
|
110
|
-
node.title || (node.children[0] && node.children[0].value) || '?';
|
|
111
|
-
const line =
|
|
112
|
-
(node.position && node.position.start && node.position.start.line) || '?';
|
|
113
|
-
throw new Error(
|
|
114
|
-
`Markdown link url is mandatory. filePath=${toMessageRelativeFilePath(
|
|
115
|
-
filePath,
|
|
116
|
-
)}, title=${title}, line=${line}`,
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const parsedUrl = url.parse(node.url);
|
|
121
|
-
if (parsedUrl.protocol) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
await convertToAssetLinkIfNeeded({
|
|
126
|
-
node,
|
|
127
|
-
staticDir,
|
|
128
|
-
filePath,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const plugin = (options) => {
|
|
133
|
-
const transformer = async (root) => {
|
|
134
|
-
const promises = [];
|
|
135
|
-
visit(root, 'link', (node, index, parent) => {
|
|
136
|
-
promises.push(processLinkNode({node, index, parent, ...options}));
|
|
137
|
-
});
|
|
138
|
-
await Promise.all(promises);
|
|
139
|
-
};
|
|
140
|
-
return transformer;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
module.exports = plugin;
|