@docusaurus/utils 2.0.0-beta.6f366f4b4 → 2.0.0-beta.7
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 -1
- package/lib/codeTranslationsUtils.js +2 -2
- package/lib/globUtils.js +3 -3
- package/lib/hashUtils.js +4 -4
- package/lib/index.d.ts +3 -1
- package/lib/index.js +28 -81
- package/lib/markdownLinks.js +19 -6
- package/lib/markdownParser.js +14 -6
- package/lib/mdxUtils.d.ts +16 -0
- package/lib/mdxUtils.js +30 -0
- package/lib/normalizeUrl.d.ts +7 -0
- package/lib/normalizeUrl.js +66 -0
- package/lib/tags.d.ts +18 -0
- package/lib/tags.js +72 -0
- package/package.json +13 -5
- package/src/__tests__/index.test.ts +0 -108
- package/src/__tests__/markdownParser.test.ts +13 -0
- package/src/__tests__/mdxUtils.test.ts +133 -0
- package/src/__tests__/normalizeUrl.test.ts +117 -0
- package/src/__tests__/pathUtils.test.ts +4 -2
- package/src/__tests__/tags.test.ts +183 -0
- package/src/dependencies.d.ts +12 -0
- package/src/index.ts +6 -77
- package/src/markdownLinks.ts +19 -8
- package/src/markdownParser.ts +18 -11
- package/src/mdxUtils.ts +32 -0
- package/src/normalizeUrl.ts +80 -0
- package/src/tags.ts +100 -0
- package/yarn-error.log +17862 -0
package/lib/index.js
CHANGED
|
@@ -6,24 +6,28 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.parseMarkdownHeadingId = exports.updateTranslationFileMessages = exports.getSwizzledComponent = exports.mergeTranslations = exports.reportMessage = exports.getFolderContainingFile = exports.findFolderContainingFile = exports.findAsyncSequential = exports.mapAsyncSequencial = exports.getPluginI18nPath = exports.getElementsAround = exports.removePrefix = exports.removeSuffix = exports.removeTrailingSlash = exports.addTrailingSlash = exports.addTrailingPathSeparator = exports.addLeadingSlash = exports.resolvePathname = exports.isValidPathname = exports.getEditUrl = exports.aliasedSitePath = exports.
|
|
9
|
+
exports.parseMarkdownHeadingId = exports.updateTranslationFileMessages = exports.getSwizzledComponent = exports.mergeTranslations = exports.reportMessage = exports.getFolderContainingFile = exports.findFolderContainingFile = exports.findAsyncSequential = exports.mapAsyncSequencial = exports.getPluginI18nPath = exports.getElementsAround = exports.removePrefix = exports.removeSuffix = exports.removeTrailingSlash = exports.addTrailingSlash = exports.addTrailingPathSeparator = exports.addLeadingSlash = exports.resolvePathname = exports.isValidPathname = exports.getEditUrl = exports.aliasedSitePath = exports.getSubFolder = exports.idx = exports.genChunkName = exports.toMessageRelativeFilePath = exports.genComponentName = exports.upperFirst = exports.encodePath = exports.fileToPath = exports.objectWithKeySorted = exports.generate = exports.createAbsoluteFilePathMatcher = exports.createMatcher = exports.GlobExcludeDefault = exports.Globby = exports.docuHash = exports.simpleHash = exports.md5Hash = exports.posixPath = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
|
-
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
12
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
11
|
+
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
|
|
12
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
13
13
|
const crypto_1 = require("crypto");
|
|
14
14
|
const lodash_1 = require("lodash");
|
|
15
|
-
const escape_string_regexp_1 = tslib_1.__importDefault(require("escape-string-regexp"));
|
|
16
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
15
|
+
const escape_string_regexp_1 = (0, tslib_1.__importDefault)(require("escape-string-regexp"));
|
|
16
|
+
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
17
17
|
const url_1 = require("url");
|
|
18
18
|
// @ts-expect-error: no typedefs :s
|
|
19
|
-
const resolve_pathname_1 = tslib_1.__importDefault(require("resolve-pathname"));
|
|
19
|
+
const resolve_pathname_1 = (0, tslib_1.__importDefault)(require("resolve-pathname"));
|
|
20
20
|
const posixPath_1 = require("./posixPath");
|
|
21
21
|
const hashUtils_1 = require("./hashUtils");
|
|
22
|
+
const normalizeUrl_1 = require("./normalizeUrl");
|
|
23
|
+
(0, tslib_1.__exportStar)(require("./mdxUtils"), exports);
|
|
24
|
+
(0, tslib_1.__exportStar)(require("./normalizeUrl"), exports);
|
|
25
|
+
(0, tslib_1.__exportStar)(require("./tags"), exports);
|
|
22
26
|
exports.posixPath = posixPath_1.posixPath;
|
|
23
|
-
tslib_1.__exportStar(require("./codeTranslationsUtils"), exports);
|
|
24
|
-
tslib_1.__exportStar(require("./markdownParser"), exports);
|
|
25
|
-
tslib_1.__exportStar(require("./markdownLinks"), exports);
|
|
26
|
-
tslib_1.__exportStar(require("./escapePath"), exports);
|
|
27
|
+
(0, tslib_1.__exportStar)(require("./codeTranslationsUtils"), exports);
|
|
28
|
+
(0, tslib_1.__exportStar)(require("./markdownParser"), exports);
|
|
29
|
+
(0, tslib_1.__exportStar)(require("./markdownLinks"), exports);
|
|
30
|
+
(0, tslib_1.__exportStar)(require("./escapePath"), exports);
|
|
27
31
|
var hashUtils_2 = require("./hashUtils");
|
|
28
32
|
Object.defineProperty(exports, "md5Hash", { enumerable: true, get: function () { return hashUtils_2.md5Hash; } });
|
|
29
33
|
Object.defineProperty(exports, "simpleHash", { enumerable: true, get: function () { return hashUtils_2.simpleHash; } });
|
|
@@ -47,10 +51,10 @@ async function generate(generatedFilesDir, file, content, skipCache = process.en
|
|
|
47
51
|
// This is to avoid unnecessary overwriting and we can reuse old file.
|
|
48
52
|
if (!lastHash && fs_extra_1.default.existsSync(filepath)) {
|
|
49
53
|
const lastContent = await fs_extra_1.default.readFile(filepath, 'utf8');
|
|
50
|
-
lastHash = crypto_1.createHash('md5').update(lastContent).digest('hex');
|
|
54
|
+
lastHash = (0, crypto_1.createHash)('md5').update(lastContent).digest('hex');
|
|
51
55
|
fileHash.set(filepath, lastHash);
|
|
52
56
|
}
|
|
53
|
-
const currentHash = crypto_1.createHash('md5').update(content).digest('hex');
|
|
57
|
+
const currentHash = (0, crypto_1.createHash)('md5').update(content).digest('hex');
|
|
54
58
|
if (lastHash !== currentHash) {
|
|
55
59
|
await fs_extra_1.default.ensureDir(path_1.default.dirname(filepath));
|
|
56
60
|
await fs_extra_1.default.writeFile(filepath, content);
|
|
@@ -104,8 +108,8 @@ function genComponentName(pagePath) {
|
|
|
104
108
|
if (pagePath === '/') {
|
|
105
109
|
return 'index';
|
|
106
110
|
}
|
|
107
|
-
const pageHash = hashUtils_1.docuHash(pagePath);
|
|
108
|
-
return upperFirst(lodash_1.camelCase(pageHash));
|
|
111
|
+
const pageHash = (0, hashUtils_1.docuHash)(pagePath);
|
|
112
|
+
return upperFirst((0, lodash_1.camelCase)(pageHash));
|
|
109
113
|
}
|
|
110
114
|
exports.genComponentName = genComponentName;
|
|
111
115
|
// When you want to display a path in a message/warning/error,
|
|
@@ -116,7 +120,7 @@ exports.genComponentName = genComponentName;
|
|
|
116
120
|
// on both Unix/Windows
|
|
117
121
|
// For Windows users this is not perfect (as they see / instead of \) but it's probably good enough
|
|
118
122
|
function toMessageRelativeFilePath(filePath) {
|
|
119
|
-
return exports.posixPath(path_1.default.relative(process.cwd(), filePath));
|
|
123
|
+
return (0, exports.posixPath)(path_1.default.relative(process.cwd(), filePath));
|
|
120
124
|
}
|
|
121
125
|
exports.toMessageRelativeFilePath = toMessageRelativeFilePath;
|
|
122
126
|
const chunkNameCache = new Map();
|
|
@@ -127,15 +131,15 @@ function genChunkName(modulePath, prefix, preferredName, shortId = process.env.N
|
|
|
127
131
|
let chunkName = chunkNameCache.get(modulePath);
|
|
128
132
|
if (!chunkName) {
|
|
129
133
|
if (shortId) {
|
|
130
|
-
chunkName = hashUtils_1.simpleHash(modulePath, 8);
|
|
134
|
+
chunkName = (0, hashUtils_1.simpleHash)(modulePath, 8);
|
|
131
135
|
}
|
|
132
136
|
else {
|
|
133
137
|
let str = modulePath;
|
|
134
138
|
if (preferredName) {
|
|
135
|
-
const shortHash = hashUtils_1.simpleHash(modulePath, 3);
|
|
139
|
+
const shortHash = (0, hashUtils_1.simpleHash)(modulePath, 3);
|
|
136
140
|
str = `${preferredName}${shortHash}`;
|
|
137
141
|
}
|
|
138
|
-
const name = str === '/' ? 'index' : hashUtils_1.docuHash(str);
|
|
142
|
+
const name = str === '/' ? 'index' : (0, hashUtils_1.docuHash)(str);
|
|
139
143
|
chunkName = prefix ? `${prefix}---${name}` : name;
|
|
140
144
|
}
|
|
141
145
|
chunkNameCache.set(modulePath, chunkName);
|
|
@@ -157,77 +161,20 @@ exports.idx = idx;
|
|
|
157
161
|
* Given a filepath and dirpath, get the first directory.
|
|
158
162
|
*/
|
|
159
163
|
function getSubFolder(file, refDir) {
|
|
160
|
-
const separator = escape_string_regexp_1.default(path_1.default.sep);
|
|
161
|
-
const baseDir = escape_string_regexp_1.default(path_1.default.basename(refDir));
|
|
164
|
+
const separator = (0, escape_string_regexp_1.default)(path_1.default.sep);
|
|
165
|
+
const baseDir = (0, escape_string_regexp_1.default)(path_1.default.basename(refDir));
|
|
162
166
|
const regexSubFolder = new RegExp(`${baseDir}${separator}(.*?)${separator}.*`);
|
|
163
167
|
const match = regexSubFolder.exec(file);
|
|
164
168
|
return match && match[1];
|
|
165
169
|
}
|
|
166
170
|
exports.getSubFolder = getSubFolder;
|
|
167
|
-
function normalizeUrl(rawUrls) {
|
|
168
|
-
const urls = [...rawUrls];
|
|
169
|
-
const resultArray = [];
|
|
170
|
-
let hasStartingSlash = false;
|
|
171
|
-
let hasEndingSlash = false;
|
|
172
|
-
// If the first part is a plain protocol, we combine it with the next part.
|
|
173
|
-
if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
|
|
174
|
-
const first = urls.shift();
|
|
175
|
-
urls[0] = first + urls[0];
|
|
176
|
-
}
|
|
177
|
-
// There must be two or three slashes in the file protocol,
|
|
178
|
-
// two slashes in anything else.
|
|
179
|
-
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
180
|
-
urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
|
|
181
|
-
// eslint-disable-next-line
|
|
182
|
-
for (let i = 0; i < urls.length; i++) {
|
|
183
|
-
let component = urls[i];
|
|
184
|
-
if (typeof component !== 'string') {
|
|
185
|
-
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
186
|
-
}
|
|
187
|
-
if (component === '') {
|
|
188
|
-
if (i === urls.length - 1 && hasEndingSlash) {
|
|
189
|
-
resultArray.push('/');
|
|
190
|
-
}
|
|
191
|
-
// eslint-disable-next-line
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
if (component !== '/') {
|
|
195
|
-
if (i > 0) {
|
|
196
|
-
// Removing the starting slashes for each component but the first.
|
|
197
|
-
component = component.replace(/^[/]+/,
|
|
198
|
-
// Special case where the first element of rawUrls is empty ["", "/hello"] => /hello
|
|
199
|
-
component[0] === '/' && !hasStartingSlash ? '/' : '');
|
|
200
|
-
}
|
|
201
|
-
hasEndingSlash = component[component.length - 1] === '/';
|
|
202
|
-
// Removing the ending slashes for each component but the last.
|
|
203
|
-
// For the last component we will combine multiple slashes to a single one.
|
|
204
|
-
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
|
205
|
-
}
|
|
206
|
-
hasStartingSlash = true;
|
|
207
|
-
resultArray.push(component);
|
|
208
|
-
}
|
|
209
|
-
let str = resultArray.join('/');
|
|
210
|
-
// Each input component is now separated by a single slash
|
|
211
|
-
// except the possible first plain protocol part.
|
|
212
|
-
// Remove trailing slash before parameters or hash.
|
|
213
|
-
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
|
|
214
|
-
// Replace ? in parameters with &.
|
|
215
|
-
const parts = str.split('?');
|
|
216
|
-
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
217
|
-
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
218
|
-
str = str.replace(/([^:]\/)\/+/g, '$1');
|
|
219
|
-
// Dedupe forward slashes at the beginning of the path.
|
|
220
|
-
str = str.replace(/^\/+/g, '/');
|
|
221
|
-
return str;
|
|
222
|
-
}
|
|
223
|
-
exports.normalizeUrl = normalizeUrl;
|
|
224
171
|
/**
|
|
225
172
|
* Alias filepath relative to site directory, very useful so that we
|
|
226
173
|
* don't expose user's site structure.
|
|
227
174
|
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
|
228
175
|
*/
|
|
229
176
|
function aliasedSitePath(filePath, siteDir) {
|
|
230
|
-
const relativePath = exports.posixPath(path_1.default.relative(siteDir, filePath));
|
|
177
|
+
const relativePath = (0, exports.posixPath)(path_1.default.relative(siteDir, filePath));
|
|
231
178
|
// Cannot use path.join() as it resolves '../' and removes
|
|
232
179
|
// the '@site'. Let webpack loader resolve it.
|
|
233
180
|
return `@site/${relativePath}`;
|
|
@@ -235,7 +182,7 @@ function aliasedSitePath(filePath, siteDir) {
|
|
|
235
182
|
exports.aliasedSitePath = aliasedSitePath;
|
|
236
183
|
function getEditUrl(fileRelativePath, editUrl) {
|
|
237
184
|
return editUrl
|
|
238
|
-
? normalizeUrl([editUrl, exports.posixPath(fileRelativePath)])
|
|
185
|
+
? (0, normalizeUrl_1.normalizeUrl)([editUrl, (0, exports.posixPath)(fileRelativePath)])
|
|
239
186
|
: undefined;
|
|
240
187
|
}
|
|
241
188
|
exports.getEditUrl = getEditUrl;
|
|
@@ -255,7 +202,7 @@ function isValidPathname(str) {
|
|
|
255
202
|
exports.isValidPathname = isValidPathname;
|
|
256
203
|
// resolve pathname and fail fast if resolution fails
|
|
257
204
|
function resolvePathname(to, from) {
|
|
258
|
-
return resolve_pathname_1.default(to, from);
|
|
205
|
+
return (0, resolve_pathname_1.default)(to, from);
|
|
259
206
|
}
|
|
260
207
|
exports.resolvePathname = resolvePathname;
|
|
261
208
|
function addLeadingSlash(str) {
|
|
@@ -383,7 +330,7 @@ exports.getSwizzledComponent = getSwizzledComponent;
|
|
|
383
330
|
function updateTranslationFileMessages(translationFile, updateMessage) {
|
|
384
331
|
return {
|
|
385
332
|
...translationFile,
|
|
386
|
-
content: lodash_1.mapValues(translationFile.content, (translation) => ({
|
|
333
|
+
content: (0, lodash_1.mapValues)(translationFile.content, (translation) => ({
|
|
387
334
|
...translation,
|
|
388
335
|
message: updateMessage(translation.message),
|
|
389
336
|
})),
|
package/lib/markdownLinks.js
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.replaceMarkdownLinks = void 0;
|
|
10
|
-
const
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
11
12
|
const index_1 = require("./index");
|
|
12
13
|
function replaceMarkdownLinks({ siteDir, fileString, filePath, contentPaths, sourceToPermalink, }) {
|
|
13
14
|
const { contentPath, contentPathLocalized } = contentPaths;
|
|
@@ -30,12 +31,24 @@ function replaceMarkdownLinks({ siteDir, fileString, filePath, contentPaths, sou
|
|
|
30
31
|
while (mdMatch !== null) {
|
|
31
32
|
// Replace it to correct html link.
|
|
32
33
|
const mdLink = mdMatch[1];
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
const sourcesToTry = [
|
|
35
|
+
path_1.default.resolve(path_1.default.dirname(filePath), decodeURIComponent(mdLink)),
|
|
36
|
+
`${contentPathLocalized}/${decodeURIComponent(mdLink)}`,
|
|
37
|
+
`${contentPath}/${decodeURIComponent(mdLink)}`,
|
|
38
|
+
];
|
|
39
|
+
const aliasedSourceMatch = sourcesToTry
|
|
40
|
+
.map((source) => (0, index_1.aliasedSitePath)(source, siteDir))
|
|
41
|
+
.find((source) => sourceToPermalink[source]);
|
|
42
|
+
const permalink = aliasedSourceMatch
|
|
43
|
+
? sourceToPermalink[aliasedSourceMatch]
|
|
44
|
+
: undefined;
|
|
37
45
|
if (permalink) {
|
|
38
|
-
|
|
46
|
+
// MDX won't be happy if the permalink contains a space, we need to convert it to %20
|
|
47
|
+
const encodedPermalink = permalink
|
|
48
|
+
.split('/')
|
|
49
|
+
.map((part) => part.replace(/\s/g, '%20'))
|
|
50
|
+
.join('/');
|
|
51
|
+
modifiedLine = modifiedLine.replace(mdLink, encodedPermalink);
|
|
39
52
|
}
|
|
40
53
|
else {
|
|
41
54
|
const brokenMarkdownLink = {
|
package/lib/markdownParser.js
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.parseMarkdownFile = exports.parseMarkdownString = exports.parseMarkdownContentTitle = exports.parseFrontMatter = exports.createExcerpt = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
|
-
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
12
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
13
|
-
const gray_matter_1 = tslib_1.__importDefault(require("gray-matter"));
|
|
11
|
+
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
|
|
12
|
+
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
13
|
+
const gray_matter_1 = (0, tslib_1.__importDefault)(require("gray-matter"));
|
|
14
14
|
// Hacky way of stripping out import statements from the excerpt
|
|
15
15
|
// TODO: Find a better way to do so, possibly by compiling the Markdown content,
|
|
16
16
|
// stripping out HTML tags and obtaining the first line.
|
|
@@ -20,6 +20,7 @@ function createExcerpt(fileString) {
|
|
|
20
20
|
// Remove Markdown alternate title
|
|
21
21
|
.replace(/^[^\n]*\n[=]+/g, '')
|
|
22
22
|
.split('\n');
|
|
23
|
+
let inCode = false;
|
|
23
24
|
/* eslint-disable no-continue */
|
|
24
25
|
// eslint-disable-next-line no-restricted-syntax
|
|
25
26
|
for (const fileLine of fileLines) {
|
|
@@ -31,6 +32,14 @@ function createExcerpt(fileString) {
|
|
|
31
32
|
if (/^\s*?import\s.*(from.*)?;?|export\s.*{.*};?/.test(fileLine)) {
|
|
32
33
|
continue;
|
|
33
34
|
}
|
|
35
|
+
// Skip code block line.
|
|
36
|
+
if (fileLine.trim().startsWith('```')) {
|
|
37
|
+
inCode = !inCode;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
else if (inCode) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
34
43
|
const cleanedLine = fileLine
|
|
35
44
|
// Remove HTML tags.
|
|
36
45
|
.replace(/<[^>]*>/g, '')
|
|
@@ -66,7 +75,7 @@ function createExcerpt(fileString) {
|
|
|
66
75
|
exports.createExcerpt = createExcerpt;
|
|
67
76
|
function parseFrontMatter(markdownFileContent) {
|
|
68
77
|
var _a;
|
|
69
|
-
const { data, content } = gray_matter_1.default(markdownFileContent);
|
|
78
|
+
const { data, content } = (0, gray_matter_1.default)(markdownFileContent);
|
|
70
79
|
return {
|
|
71
80
|
frontMatter: data !== null && data !== void 0 ? data : {},
|
|
72
81
|
content: (_a = content === null || content === void 0 ? void 0 : content.trim()) !== null && _a !== void 0 ? _a : '',
|
|
@@ -86,8 +95,7 @@ function parseMarkdownContentTitle(contentUntrimmed, options) {
|
|
|
86
95
|
var _a, _b;
|
|
87
96
|
const removeContentTitleOption = (_a = options === null || options === void 0 ? void 0 : options.removeContentTitle) !== null && _a !== void 0 ? _a : false;
|
|
88
97
|
const content = contentUntrimmed.trim();
|
|
89
|
-
const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n
|
|
90
|
-
.source;
|
|
98
|
+
const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/.source;
|
|
91
99
|
const REGULAR_TITLE = /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
|
|
92
100
|
.source;
|
|
93
101
|
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
* Transform mdx text to plain html text
|
|
9
|
+
* Initially created to convert MDX blog posts to HTML for the RSS feed
|
|
10
|
+
* without import/export nodes
|
|
11
|
+
*
|
|
12
|
+
* TODO not ideal implementation, won't work well with MDX elements!
|
|
13
|
+
* TODO theme+global site config should be able to declare MDX comps in scope for rendering the RSS feeds
|
|
14
|
+
* see also https://github.com/facebook/docusaurus/issues/4625
|
|
15
|
+
*/
|
|
16
|
+
export declare function mdxToHtml(mdxStr: string): string;
|
package/lib/mdxUtils.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.mdxToHtml = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const react_1 = (0, tslib_1.__importDefault)(require("react"));
|
|
12
|
+
const server_1 = (0, tslib_1.__importDefault)(require("react-dom/server"));
|
|
13
|
+
const runtime_1 = (0, tslib_1.__importDefault)(require("@mdx-js/runtime"));
|
|
14
|
+
const remark_mdx_remove_imports_1 = (0, tslib_1.__importDefault)(require("remark-mdx-remove-imports"));
|
|
15
|
+
const remark_mdx_remove_exports_1 = (0, tslib_1.__importDefault)(require("remark-mdx-remove-exports"));
|
|
16
|
+
/**
|
|
17
|
+
* Transform mdx text to plain html text
|
|
18
|
+
* Initially created to convert MDX blog posts to HTML for the RSS feed
|
|
19
|
+
* without import/export nodes
|
|
20
|
+
*
|
|
21
|
+
* TODO not ideal implementation, won't work well with MDX elements!
|
|
22
|
+
* TODO theme+global site config should be able to declare MDX comps in scope for rendering the RSS feeds
|
|
23
|
+
* see also https://github.com/facebook/docusaurus/issues/4625
|
|
24
|
+
*/
|
|
25
|
+
function mdxToHtml(mdxStr) {
|
|
26
|
+
return server_1.default.renderToString(react_1.default.createElement(runtime_1.default, { remarkPlugins: [remark_mdx_remove_imports_1.default, remark_mdx_remove_exports_1.default] }, [
|
|
27
|
+
mdxStr,
|
|
28
|
+
]));
|
|
29
|
+
}
|
|
30
|
+
exports.mdxToHtml = mdxToHtml;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.normalizeUrl = void 0;
|
|
10
|
+
function normalizeUrl(rawUrls) {
|
|
11
|
+
const urls = [...rawUrls];
|
|
12
|
+
const resultArray = [];
|
|
13
|
+
let hasStartingSlash = false;
|
|
14
|
+
let hasEndingSlash = false;
|
|
15
|
+
// If the first part is a plain protocol, we combine it with the next part.
|
|
16
|
+
if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
|
|
17
|
+
const first = urls.shift();
|
|
18
|
+
urls[0] = first + urls[0];
|
|
19
|
+
}
|
|
20
|
+
// There must be two or three slashes in the file protocol,
|
|
21
|
+
// two slashes in anything else.
|
|
22
|
+
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
23
|
+
urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
|
|
24
|
+
// eslint-disable-next-line
|
|
25
|
+
for (let i = 0; i < urls.length; i++) {
|
|
26
|
+
let component = urls[i];
|
|
27
|
+
if (typeof component !== 'string') {
|
|
28
|
+
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
29
|
+
}
|
|
30
|
+
if (component === '') {
|
|
31
|
+
if (i === urls.length - 1 && hasEndingSlash) {
|
|
32
|
+
resultArray.push('/');
|
|
33
|
+
}
|
|
34
|
+
// eslint-disable-next-line
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (component !== '/') {
|
|
38
|
+
if (i > 0) {
|
|
39
|
+
// Removing the starting slashes for each component but the first.
|
|
40
|
+
component = component.replace(/^[/]+/,
|
|
41
|
+
// Special case where the first element of rawUrls is empty ["", "/hello"] => /hello
|
|
42
|
+
component[0] === '/' && !hasStartingSlash ? '/' : '');
|
|
43
|
+
}
|
|
44
|
+
hasEndingSlash = component[component.length - 1] === '/';
|
|
45
|
+
// Removing the ending slashes for each component but the last.
|
|
46
|
+
// For the last component we will combine multiple slashes to a single one.
|
|
47
|
+
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
|
48
|
+
}
|
|
49
|
+
hasStartingSlash = true;
|
|
50
|
+
resultArray.push(component);
|
|
51
|
+
}
|
|
52
|
+
let str = resultArray.join('/');
|
|
53
|
+
// Each input component is now separated by a single slash
|
|
54
|
+
// except the possible first plain protocol part.
|
|
55
|
+
// Remove trailing slash before parameters or hash.
|
|
56
|
+
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
|
|
57
|
+
// Replace ? in parameters with &.
|
|
58
|
+
const parts = str.split('?');
|
|
59
|
+
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
60
|
+
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
61
|
+
str = str.replace(/([^:]\/)\/+/g, '$1');
|
|
62
|
+
// Dedupe forward slashes at the beginning of the path.
|
|
63
|
+
str = str.replace(/^\/+/g, '/');
|
|
64
|
+
return str;
|
|
65
|
+
}
|
|
66
|
+
exports.normalizeUrl = normalizeUrl;
|
package/lib/tags.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
export declare type Tag = {
|
|
8
|
+
label: string;
|
|
9
|
+
permalink: string;
|
|
10
|
+
};
|
|
11
|
+
export declare type FrontMatterTag = string | Tag;
|
|
12
|
+
export declare function normalizeFrontMatterTag(tagsPath: string, frontMatterTag: FrontMatterTag): Tag;
|
|
13
|
+
export declare function normalizeFrontMatterTags(tagsPath: string, frontMatterTags: FrontMatterTag[] | undefined): Tag[];
|
|
14
|
+
export declare type TaggedItemGroup<Item> = {
|
|
15
|
+
tag: Tag;
|
|
16
|
+
items: Item[];
|
|
17
|
+
};
|
|
18
|
+
export declare function groupTaggedItems<Item>(items: Item[], getItemTags: (item: Item) => Tag[]): Record<string, TaggedItemGroup<Item>>;
|
package/lib/tags.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.groupTaggedItems = exports.normalizeFrontMatterTags = exports.normalizeFrontMatterTag = void 0;
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
11
|
+
const normalizeUrl_1 = require("./normalizeUrl");
|
|
12
|
+
function normalizeFrontMatterTag(tagsPath, frontMatterTag) {
|
|
13
|
+
function toTagObject(tagString) {
|
|
14
|
+
return {
|
|
15
|
+
label: tagString,
|
|
16
|
+
permalink: (0, lodash_1.kebabCase)(tagString),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
// TODO maybe make ensure the permalink is valid url path?
|
|
20
|
+
function normalizeTagPermalink(permalink) {
|
|
21
|
+
// note: we always apply tagsPath on purpose
|
|
22
|
+
// for versioned docs, v1/doc.md and v2/doc.md tags with custom permalinks don't lead to the same created page
|
|
23
|
+
// tagsPath is different for each doc version
|
|
24
|
+
return (0, normalizeUrl_1.normalizeUrl)([tagsPath, permalink]);
|
|
25
|
+
}
|
|
26
|
+
const tag = typeof frontMatterTag === 'string'
|
|
27
|
+
? toTagObject(frontMatterTag)
|
|
28
|
+
: frontMatterTag;
|
|
29
|
+
return {
|
|
30
|
+
label: tag.label,
|
|
31
|
+
permalink: normalizeTagPermalink(tag.permalink),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
exports.normalizeFrontMatterTag = normalizeFrontMatterTag;
|
|
35
|
+
function normalizeFrontMatterTags(tagsPath, frontMatterTags) {
|
|
36
|
+
var _a;
|
|
37
|
+
const tags = (_a = frontMatterTags === null || frontMatterTags === void 0 ? void 0 : frontMatterTags.map((tag) => normalizeFrontMatterTag(tagsPath, tag))) !== null && _a !== void 0 ? _a : [];
|
|
38
|
+
return (0, lodash_1.uniqBy)(tags, (tag) => tag.permalink);
|
|
39
|
+
}
|
|
40
|
+
exports.normalizeFrontMatterTags = normalizeFrontMatterTags;
|
|
41
|
+
// Permits to group docs/blogPosts by tag (provided by FrontMatter)
|
|
42
|
+
// Note: groups are indexed by permalink, because routes must be unique in the end
|
|
43
|
+
// Labels may vary on 2 md files but they are normalized.
|
|
44
|
+
// Docs with label='some label' and label='some-label' should end-up in the same group/page in the end
|
|
45
|
+
// We can't create 2 routes /some-label because one would override the other
|
|
46
|
+
function groupTaggedItems(items, getItemTags) {
|
|
47
|
+
const result = {};
|
|
48
|
+
function handleItemTag(item, tag) {
|
|
49
|
+
var _a;
|
|
50
|
+
// Init missing tag groups
|
|
51
|
+
// TODO: it's not really clear what should be the behavior if 2 items have the same tag but the permalink is different for each
|
|
52
|
+
// For now, the first tag found wins
|
|
53
|
+
result[tag.permalink] = (_a = result[tag.permalink]) !== null && _a !== void 0 ? _a : {
|
|
54
|
+
tag,
|
|
55
|
+
items: [],
|
|
56
|
+
};
|
|
57
|
+
// Add item to group
|
|
58
|
+
result[tag.permalink].items.push(item);
|
|
59
|
+
}
|
|
60
|
+
items.forEach((item) => {
|
|
61
|
+
getItemTags(item).forEach((tag) => {
|
|
62
|
+
handleItemTag(item, tag);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
// If user add twice the same tag to a md doc (weird but possible),
|
|
66
|
+
// we don't want the item to appear twice in the list...
|
|
67
|
+
Object.values(result).forEach((group) => {
|
|
68
|
+
group.items = (0, lodash_1.uniq)(group.items);
|
|
69
|
+
});
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
exports.groupTaggedItems = groupTaggedItems;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/utils",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.7",
|
|
4
4
|
"description": "Node utility functions for Docusaurus packages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,17 +18,20 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/types": "2.0.0-beta.
|
|
21
|
+
"@docusaurus/types": "2.0.0-beta.7",
|
|
22
|
+
"@mdx-js/runtime": "^1.6.22",
|
|
22
23
|
"@types/github-slugger": "^1.3.0",
|
|
23
|
-
"chalk": "^4.1.
|
|
24
|
+
"chalk": "^4.1.2",
|
|
24
25
|
"escape-string-regexp": "^4.0.0",
|
|
25
26
|
"fs-extra": "^10.0.0",
|
|
26
27
|
"globby": "^11.0.4",
|
|
27
28
|
"gray-matter": "^4.0.3",
|
|
28
29
|
"lodash": "^4.17.20",
|
|
29
30
|
"micromatch": "^4.0.4",
|
|
31
|
+
"remark-mdx-remove-exports": "^1.6.22",
|
|
32
|
+
"remark-mdx-remove-imports": "^1.6.22",
|
|
30
33
|
"resolve-pathname": "^3.0.0",
|
|
31
|
-
"tslib": "^2.
|
|
34
|
+
"tslib": "^2.3.1"
|
|
32
35
|
},
|
|
33
36
|
"engines": {
|
|
34
37
|
"node": ">=12.13.0"
|
|
@@ -36,7 +39,12 @@
|
|
|
36
39
|
"devDependencies": {
|
|
37
40
|
"@types/dedent": "^0.7.0",
|
|
38
41
|
"@types/micromatch": "^4.0.2",
|
|
42
|
+
"@types/react-dom": "^17.0.1",
|
|
39
43
|
"dedent": "^0.7.0"
|
|
40
44
|
},
|
|
41
|
-
"
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"react": "*",
|
|
47
|
+
"react-dom": "*"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "71a934b4d7319cd026657e3bb7e5832447454d07"
|
|
42
50
|
}
|