@docusaurus/utils 2.0.0-beta.8bda3b2db → 2.0.0-beta.9
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.d.ts +11 -0
- package/lib/globUtils.js +47 -0
- package/lib/{docuHash.d.ts → hashUtils.d.ts} +2 -0
- package/lib/{docuHash.js → hashUtils.js} +15 -5
- package/lib/index.d.ts +5 -4
- package/lib/index.js +38 -88
- package/lib/markdownLinks.js +19 -6
- package/lib/markdownParser.js +20 -12
- package/lib/mdxUtils.d.ts +16 -0
- package/lib/mdxUtils.js +30 -0
- package/lib/{getFilePathForRoutePath.d.ts → normalizeUrl.d.ts} +1 -1
- package/lib/normalizeUrl.js +66 -0
- package/lib/pathUtils.d.ts +0 -1
- package/lib/pathUtils.js +1 -6
- package/lib/tags.d.ts +18 -0
- package/lib/tags.js +72 -0
- package/package.json +17 -6
- package/src/__tests__/globUtils.test.ts +109 -0
- package/src/__tests__/{docuHash.test.ts → hashUtils.test.ts} +22 -1
- 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 +5 -22
- package/src/__tests__/tags.test.ts +183 -0
- package/src/dependencies.d.ts +12 -0
- package/src/globUtils.ts +63 -0
- package/src/{docuHash.ts → hashUtils.ts} +10 -1
- package/src/index.ts +14 -83
- package/src/markdownLinks.ts +19 -8
- package/src/markdownParser.ts +24 -17
- package/src/mdxUtils.ts +32 -0
- package/src/normalizeUrl.ts +80 -0
- package/src/pathUtils.ts +0 -6
- package/src/tags.ts +100 -0
- package/src/types.d.ts +10 -0
- package/yarn-error.log +17862 -0
- package/lib/getFilePathForRoutePath.js +0 -40
- package/src/__tests__/getFilePathForRoutePath.test.ts +0 -87
- package/src/getFilePathForRoutePath.ts +0 -43
|
@@ -0,0 +1,183 @@
|
|
|
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 {
|
|
9
|
+
normalizeFrontMatterTag,
|
|
10
|
+
normalizeFrontMatterTags,
|
|
11
|
+
groupTaggedItems,
|
|
12
|
+
Tag,
|
|
13
|
+
} from '../tags';
|
|
14
|
+
|
|
15
|
+
describe('normalizeFrontMatterTag', () => {
|
|
16
|
+
type Input = Parameters<typeof normalizeFrontMatterTag>[1];
|
|
17
|
+
type Output = ReturnType<typeof normalizeFrontMatterTag>;
|
|
18
|
+
|
|
19
|
+
test('should normalize simple string tag', () => {
|
|
20
|
+
const tagsPath = '/all/tags';
|
|
21
|
+
const input: Input = 'tag';
|
|
22
|
+
const expectedOutput: Output = {
|
|
23
|
+
label: 'tag',
|
|
24
|
+
permalink: `${tagsPath}/tag`,
|
|
25
|
+
};
|
|
26
|
+
expect(normalizeFrontMatterTag(tagsPath, input)).toEqual(expectedOutput);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('should normalize complex string tag', () => {
|
|
30
|
+
const tagsPath = '/all/tags';
|
|
31
|
+
const input: Input = 'some more Complex_tag';
|
|
32
|
+
const expectedOutput: Output = {
|
|
33
|
+
label: 'some more Complex_tag',
|
|
34
|
+
permalink: `${tagsPath}/some-more-complex-tag`,
|
|
35
|
+
};
|
|
36
|
+
expect(normalizeFrontMatterTag(tagsPath, input)).toEqual(expectedOutput);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('should normalize simple object tag', () => {
|
|
40
|
+
const tagsPath = '/all/tags';
|
|
41
|
+
const input: Input = {label: 'tag', permalink: 'tagPermalink'};
|
|
42
|
+
const expectedOutput: Output = {
|
|
43
|
+
label: 'tag',
|
|
44
|
+
permalink: `${tagsPath}/tagPermalink`,
|
|
45
|
+
};
|
|
46
|
+
expect(normalizeFrontMatterTag(tagsPath, input)).toEqual(expectedOutput);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('should normalize complex string tag', () => {
|
|
50
|
+
const tagsPath = '/all/tags';
|
|
51
|
+
const input: Input = {
|
|
52
|
+
label: 'tag complex Label',
|
|
53
|
+
permalink: '/MoreComplex/Permalink',
|
|
54
|
+
};
|
|
55
|
+
const expectedOutput: Output = {
|
|
56
|
+
label: 'tag complex Label',
|
|
57
|
+
permalink: `${tagsPath}/MoreComplex/Permalink`,
|
|
58
|
+
};
|
|
59
|
+
expect(normalizeFrontMatterTag(tagsPath, input)).toEqual(expectedOutput);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('normalizeFrontMatterTags', () => {
|
|
64
|
+
type Input = Parameters<typeof normalizeFrontMatterTags>[1];
|
|
65
|
+
type Output = ReturnType<typeof normalizeFrontMatterTags>;
|
|
66
|
+
|
|
67
|
+
test('should normalize string list', () => {
|
|
68
|
+
const tagsPath = '/all/tags';
|
|
69
|
+
const input: Input = ['tag 1', 'tag-1', 'tag 3', 'tag1', 'tag-2'];
|
|
70
|
+
// Keep user input order but remove tags that lead to same permalink
|
|
71
|
+
const expectedOutput: Output = [
|
|
72
|
+
{
|
|
73
|
+
label: 'tag 1',
|
|
74
|
+
permalink: `${tagsPath}/tag-1`,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
label: 'tag 3',
|
|
78
|
+
permalink: `${tagsPath}/tag-3`,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
label: 'tag-2',
|
|
82
|
+
permalink: `${tagsPath}/tag-2`,
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
expect(normalizeFrontMatterTags(tagsPath, input)).toEqual(expectedOutput);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('should normalize complex mixed list', () => {
|
|
89
|
+
const tagsPath = '/all/tags';
|
|
90
|
+
const input: Input = [
|
|
91
|
+
'tag 1',
|
|
92
|
+
{label: 'tag-1', permalink: '/tag-1'},
|
|
93
|
+
'tag 3',
|
|
94
|
+
'tag1',
|
|
95
|
+
{label: 'tag 4', permalink: '/tag4Permalink'},
|
|
96
|
+
];
|
|
97
|
+
// Keep user input order but remove tags that lead to same permalink
|
|
98
|
+
const expectedOutput: Output = [
|
|
99
|
+
{
|
|
100
|
+
label: 'tag 1',
|
|
101
|
+
permalink: `${tagsPath}/tag-1`,
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
label: 'tag 3',
|
|
105
|
+
permalink: `${tagsPath}/tag-3`,
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
label: 'tag 4',
|
|
109
|
+
permalink: `${tagsPath}/tag4Permalink`,
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
expect(normalizeFrontMatterTags(tagsPath, input)).toEqual(expectedOutput);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('groupTaggedItems', () => {
|
|
117
|
+
type SomeTaggedItem = {
|
|
118
|
+
id: string;
|
|
119
|
+
nested: {
|
|
120
|
+
tags: Tag[];
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
function groupItems(items: SomeTaggedItem[]) {
|
|
124
|
+
return groupTaggedItems(items, (item) => item.nested.tags);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
type Input = Parameters<typeof groupItems>[0];
|
|
128
|
+
type Output = ReturnType<typeof groupItems>;
|
|
129
|
+
|
|
130
|
+
test('should group items by tag permalink', () => {
|
|
131
|
+
const tagGuide = {label: 'Guide', permalink: '/guide'};
|
|
132
|
+
const tagTutorial = {label: 'Tutorial', permalink: '/tutorial'};
|
|
133
|
+
const tagAPI = {label: 'API', permalink: '/api'};
|
|
134
|
+
|
|
135
|
+
// This one will be grouped under same permalink and label is ignored
|
|
136
|
+
const tagTutorialOtherLabel = {
|
|
137
|
+
label: 'TutorialOtherLabel',
|
|
138
|
+
permalink: '/tutorial',
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const item1: SomeTaggedItem = {
|
|
142
|
+
id: '1',
|
|
143
|
+
nested: {
|
|
144
|
+
tags: [
|
|
145
|
+
tagGuide,
|
|
146
|
+
tagTutorial,
|
|
147
|
+
tagAPI,
|
|
148
|
+
// Add some duplicates on purpose: they should be filtered
|
|
149
|
+
tagGuide,
|
|
150
|
+
tagTutorialOtherLabel,
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
const item2: SomeTaggedItem = {
|
|
155
|
+
id: '2',
|
|
156
|
+
nested: {
|
|
157
|
+
tags: [tagAPI],
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
const item3: SomeTaggedItem = {
|
|
161
|
+
id: '3',
|
|
162
|
+
nested: {
|
|
163
|
+
tags: [tagTutorial],
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
const item4: SomeTaggedItem = {
|
|
167
|
+
id: '4',
|
|
168
|
+
nested: {
|
|
169
|
+
tags: [tagTutorialOtherLabel],
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const input: Input = [item1, item2, item3, item4];
|
|
174
|
+
|
|
175
|
+
const expectedOutput: Output = {
|
|
176
|
+
'/guide': {tag: tagGuide, items: [item1]},
|
|
177
|
+
'/tutorial': {tag: tagTutorial, items: [item1, item3, item4]},
|
|
178
|
+
'/api': {tag: tagAPI, items: [item1, item2]},
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
expect(groupItems(input)).toEqual(expectedOutput);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
// Dependencies with missing typedefs
|
|
9
|
+
|
|
10
|
+
declare module '@mdx-js/runtime';
|
|
11
|
+
declare module 'remark-mdx-remove-imports';
|
|
12
|
+
declare module 'remark-mdx-remove-exports';
|
package/src/globUtils.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
// Globby/Micromatch are the 2 libs we use in Docusaurus consistently
|
|
9
|
+
|
|
10
|
+
export {default as Globby} from 'globby';
|
|
11
|
+
import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby
|
|
12
|
+
import path from 'path';
|
|
13
|
+
|
|
14
|
+
// The default patterns we ignore when globbing
|
|
15
|
+
// using _ prefix for exclusion by convention
|
|
16
|
+
export const GlobExcludeDefault = [
|
|
17
|
+
// Ignore files starting with _
|
|
18
|
+
'**/_*.{js,jsx,ts,tsx,md,mdx}',
|
|
19
|
+
|
|
20
|
+
// Ignore folders starting with _ (including folder content)
|
|
21
|
+
'**/_*/**',
|
|
22
|
+
|
|
23
|
+
// Ignore tests
|
|
24
|
+
'**/*.test.{js,jsx,ts,tsx}',
|
|
25
|
+
'**/__tests__/**',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
type Matcher = (str: string) => boolean;
|
|
29
|
+
|
|
30
|
+
export function createMatcher(patterns: string[]): Matcher {
|
|
31
|
+
const regexp = new RegExp(
|
|
32
|
+
patterns.map((pattern) => Micromatch.makeRe(pattern).source).join('|'),
|
|
33
|
+
);
|
|
34
|
+
return (str) => regexp.test(str);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// We use match patterns like '**/_*/**',
|
|
38
|
+
// This function permits to help to:
|
|
39
|
+
// Match /user/sebastien/website/docs/_partials/xyz.md
|
|
40
|
+
// Ignore /user/_sebastien/website/docs/partials/xyz.md
|
|
41
|
+
export function createAbsoluteFilePathMatcher(
|
|
42
|
+
patterns: string[],
|
|
43
|
+
rootFolders: string[],
|
|
44
|
+
): Matcher {
|
|
45
|
+
const matcher = createMatcher(patterns);
|
|
46
|
+
|
|
47
|
+
function getRelativeFilePath(absoluteFilePath: string) {
|
|
48
|
+
const rootFolder = rootFolders.find((folderPath) =>
|
|
49
|
+
absoluteFilePath.startsWith(folderPath),
|
|
50
|
+
);
|
|
51
|
+
if (!rootFolder) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`createAbsoluteFilePathMatcher unexpected error, absoluteFilePath=${absoluteFilePath} was not contained in any of the root folders ${JSON.stringify(
|
|
54
|
+
rootFolders,
|
|
55
|
+
)}`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return path.relative(rootFolder, absoluteFilePath);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return (absoluteFilePath: string) =>
|
|
62
|
+
matcher(getRelativeFilePath(absoluteFilePath));
|
|
63
|
+
}
|
|
@@ -5,10 +5,19 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import {createHash} from 'crypto';
|
|
8
9
|
import {kebabCase} from 'lodash';
|
|
10
|
+
import {shortName, isNameTooLong} from './pathUtils';
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
export function md5Hash(str: string): string {
|
|
13
|
+
return createHash('md5').update(str).digest('hex');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function simpleHash(str: string, length: number): string {
|
|
17
|
+
return md5Hash(str).substr(0, length);
|
|
18
|
+
}
|
|
11
19
|
|
|
20
|
+
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
12
21
|
/**
|
|
13
22
|
* Given an input string, convert to kebab-case and append a hash.
|
|
14
23
|
* Avoid str collision.
|
package/src/index.ts
CHANGED
|
@@ -18,22 +18,29 @@ import {
|
|
|
18
18
|
TranslationFile,
|
|
19
19
|
} from '@docusaurus/types';
|
|
20
20
|
|
|
21
|
-
// @ts-expect-error: no typedefs :s
|
|
22
21
|
import resolvePathnameUnsafe from 'resolve-pathname';
|
|
23
22
|
|
|
24
23
|
import {posixPath as posixPathImport} from './posixPath';
|
|
25
|
-
import {simpleHash} from './
|
|
26
|
-
import {
|
|
24
|
+
import {simpleHash, docuHash} from './hashUtils';
|
|
25
|
+
import {normalizeUrl} from './normalizeUrl';
|
|
26
|
+
|
|
27
|
+
export * from './mdxUtils';
|
|
28
|
+
export * from './normalizeUrl';
|
|
29
|
+
export * from './tags';
|
|
27
30
|
|
|
28
31
|
export const posixPath = posixPathImport;
|
|
29
32
|
|
|
30
|
-
export * from './getFilePathForRoutePath';
|
|
31
33
|
export * from './codeTranslationsUtils';
|
|
32
34
|
export * from './markdownParser';
|
|
33
35
|
export * from './markdownLinks';
|
|
34
36
|
export * from './escapePath';
|
|
35
|
-
export
|
|
36
|
-
export {
|
|
37
|
+
export {md5Hash, simpleHash, docuHash} from './hashUtils';
|
|
38
|
+
export {
|
|
39
|
+
Globby,
|
|
40
|
+
GlobExcludeDefault,
|
|
41
|
+
createMatcher,
|
|
42
|
+
createAbsoluteFilePathMatcher,
|
|
43
|
+
} from './globUtils';
|
|
37
44
|
|
|
38
45
|
const fileHash = new Map();
|
|
39
46
|
export async function generate(
|
|
@@ -187,80 +194,6 @@ export function getSubFolder(file: string, refDir: string): string | null {
|
|
|
187
194
|
return match && match[1];
|
|
188
195
|
}
|
|
189
196
|
|
|
190
|
-
export function normalizeUrl(rawUrls: string[]): string {
|
|
191
|
-
const urls = [...rawUrls];
|
|
192
|
-
const resultArray = [];
|
|
193
|
-
|
|
194
|
-
let hasStartingSlash = false;
|
|
195
|
-
let hasEndingSlash = false;
|
|
196
|
-
|
|
197
|
-
// If the first part is a plain protocol, we combine it with the next part.
|
|
198
|
-
if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
|
|
199
|
-
const first = urls.shift();
|
|
200
|
-
urls[0] = first + urls[0];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// There must be two or three slashes in the file protocol,
|
|
204
|
-
// two slashes in anything else.
|
|
205
|
-
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
206
|
-
urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
|
|
207
|
-
|
|
208
|
-
// eslint-disable-next-line
|
|
209
|
-
for (let i = 0; i < urls.length; i++) {
|
|
210
|
-
let component = urls[i];
|
|
211
|
-
|
|
212
|
-
if (typeof component !== 'string') {
|
|
213
|
-
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (component === '') {
|
|
217
|
-
if (i === urls.length - 1 && hasEndingSlash) {
|
|
218
|
-
resultArray.push('/');
|
|
219
|
-
}
|
|
220
|
-
// eslint-disable-next-line
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (component !== '/') {
|
|
225
|
-
if (i > 0) {
|
|
226
|
-
// Removing the starting slashes for each component but the first.
|
|
227
|
-
component = component.replace(
|
|
228
|
-
/^[/]+/,
|
|
229
|
-
// Special case where the first element of rawUrls is empty ["", "/hello"] => /hello
|
|
230
|
-
component[0] === '/' && !hasStartingSlash ? '/' : '',
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
hasEndingSlash = component[component.length - 1] === '/';
|
|
235
|
-
// Removing the ending slashes for each component but the last.
|
|
236
|
-
// For the last component we will combine multiple slashes to a single one.
|
|
237
|
-
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
hasStartingSlash = true;
|
|
241
|
-
resultArray.push(component);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
let str = resultArray.join('/');
|
|
245
|
-
// Each input component is now separated by a single slash
|
|
246
|
-
// except the possible first plain protocol part.
|
|
247
|
-
|
|
248
|
-
// Remove trailing slash before parameters or hash.
|
|
249
|
-
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
|
|
250
|
-
|
|
251
|
-
// Replace ? in parameters with &.
|
|
252
|
-
const parts = str.split('?');
|
|
253
|
-
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
254
|
-
|
|
255
|
-
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
256
|
-
str = str.replace(/([^:]\/)\/+/g, '$1');
|
|
257
|
-
|
|
258
|
-
// Dedupe forward slashes at the beginning of the path.
|
|
259
|
-
str = str.replace(/^\/+/g, '/');
|
|
260
|
-
|
|
261
|
-
return str;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
197
|
/**
|
|
265
198
|
* Alias filepath relative to site directory, very useful so that we
|
|
266
199
|
* don't expose user's site structure.
|
|
@@ -494,9 +427,7 @@ export function updateTranslationFileMessages(
|
|
|
494
427
|
|
|
495
428
|
// Input: ## Some heading {#some-heading}
|
|
496
429
|
// Output: {text: "## Some heading", id: "some-heading"}
|
|
497
|
-
export function parseMarkdownHeadingId(
|
|
498
|
-
heading: string,
|
|
499
|
-
): {
|
|
430
|
+
export function parseMarkdownHeadingId(heading: string): {
|
|
500
431
|
text: string;
|
|
501
432
|
id?: string;
|
|
502
433
|
} {
|
package/src/markdownLinks.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
8
|
+
import path from 'path';
|
|
9
9
|
import {aliasedSitePath} from './index';
|
|
10
10
|
|
|
11
11
|
export type ContentPaths = {
|
|
@@ -63,16 +63,27 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
|
|
|
63
63
|
// Replace it to correct html link.
|
|
64
64
|
const mdLink = mdMatch[1];
|
|
65
65
|
|
|
66
|
-
const
|
|
67
|
-
|
|
66
|
+
const sourcesToTry = [
|
|
67
|
+
path.resolve(path.dirname(filePath), decodeURIComponent(mdLink)),
|
|
68
|
+
`${contentPathLocalized}/${decodeURIComponent(mdLink)}`,
|
|
69
|
+
`${contentPath}/${decodeURIComponent(mdLink)}`,
|
|
70
|
+
];
|
|
68
71
|
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
sourceToPermalink[
|
|
72
|
-
|
|
72
|
+
const aliasedSourceMatch = sourcesToTry
|
|
73
|
+
.map((source) => aliasedSitePath(source, siteDir))
|
|
74
|
+
.find((source) => sourceToPermalink[source]);
|
|
75
|
+
|
|
76
|
+
const permalink: string | undefined = aliasedSourceMatch
|
|
77
|
+
? sourceToPermalink[aliasedSourceMatch]
|
|
78
|
+
: undefined;
|
|
73
79
|
|
|
74
80
|
if (permalink) {
|
|
75
|
-
|
|
81
|
+
// MDX won't be happy if the permalink contains a space, we need to convert it to %20
|
|
82
|
+
const encodedPermalink = permalink
|
|
83
|
+
.split('/')
|
|
84
|
+
.map((part) => part.replace(/\s/g, '%20'))
|
|
85
|
+
.join('/');
|
|
86
|
+
modifiedLine = modifiedLine.replace(mdLink, encodedPermalink);
|
|
76
87
|
} else {
|
|
77
88
|
const brokenMarkdownLink: BrokenMarkdownLink<T> = {
|
|
78
89
|
contentPaths,
|
package/src/markdownParser.ts
CHANGED
|
@@ -18,6 +18,7 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
18
18
|
// Remove Markdown alternate title
|
|
19
19
|
.replace(/^[^\n]*\n[=]+/g, '')
|
|
20
20
|
.split('\n');
|
|
21
|
+
let inCode = false;
|
|
21
22
|
|
|
22
23
|
/* eslint-disable no-continue */
|
|
23
24
|
// eslint-disable-next-line no-restricted-syntax
|
|
@@ -32,21 +33,29 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
32
33
|
continue;
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
// Skip code block line.
|
|
37
|
+
if (fileLine.trim().startsWith('```')) {
|
|
38
|
+
inCode = !inCode;
|
|
39
|
+
continue;
|
|
40
|
+
} else if (inCode) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
35
44
|
const cleanedLine = fileLine
|
|
36
45
|
// Remove HTML tags.
|
|
37
46
|
.replace(/<[^>]*>/g, '')
|
|
38
47
|
// Remove Title headers
|
|
39
|
-
.replace(
|
|
48
|
+
.replace(/^#\s*([^#]*)\s*#?/gm, '')
|
|
40
49
|
// Remove Markdown + ATX-style headers
|
|
41
|
-
.replace(
|
|
50
|
+
.replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
|
|
42
51
|
// Remove emphasis and strikethroughs.
|
|
43
|
-
.replace(/([
|
|
52
|
+
.replace(/([*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
|
|
44
53
|
// Remove images.
|
|
45
|
-
.replace(
|
|
54
|
+
.replace(/!\[(.*?)\][[(].*?[\])]/g, '$1')
|
|
46
55
|
// Remove footnotes.
|
|
47
|
-
.replace(/\[\^.+?\](
|
|
56
|
+
.replace(/\[\^.+?\](: .*?$)?/g, '')
|
|
48
57
|
// Remove inline links.
|
|
49
|
-
.replace(/\[(.*?)\][
|
|
58
|
+
.replace(/\[(.*?)\][[(].*?[\])]/g, '$1')
|
|
50
59
|
// Remove inline code.
|
|
51
60
|
.replace(/`(.+?)`/g, '$1')
|
|
52
61
|
// Remove blockquotes.
|
|
@@ -67,9 +76,7 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
67
76
|
return undefined;
|
|
68
77
|
}
|
|
69
78
|
|
|
70
|
-
export function parseFrontMatter(
|
|
71
|
-
markdownFileContent: string,
|
|
72
|
-
): {
|
|
79
|
+
export function parseFrontMatter(markdownFileContent: string): {
|
|
73
80
|
frontMatter: Record<string, unknown>;
|
|
74
81
|
content: string;
|
|
75
82
|
} {
|
|
@@ -98,10 +105,11 @@ export function parseMarkdownContentTitle(
|
|
|
98
105
|
|
|
99
106
|
const content = contentUntrimmed.trim();
|
|
100
107
|
|
|
101
|
-
const IMPORT_STATEMENT =
|
|
102
|
-
|
|
103
|
-
const REGULAR_TITLE =
|
|
104
|
-
|
|
108
|
+
const IMPORT_STATEMENT =
|
|
109
|
+
/import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/.source;
|
|
110
|
+
const REGULAR_TITLE =
|
|
111
|
+
/(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
|
|
112
|
+
.source;
|
|
105
113
|
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
|
|
106
114
|
|
|
107
115
|
const regularTitleMatch = new RegExp(
|
|
@@ -141,9 +149,8 @@ export function parseMarkdownString(
|
|
|
141
149
|
options?: {removeContentTitle?: boolean},
|
|
142
150
|
): ParsedMarkdown {
|
|
143
151
|
try {
|
|
144
|
-
const {frontMatter, content: contentWithoutFrontMatter} =
|
|
145
|
-
markdownFileContent
|
|
146
|
-
);
|
|
152
|
+
const {frontMatter, content: contentWithoutFrontMatter} =
|
|
153
|
+
parseFrontMatter(markdownFileContent);
|
|
147
154
|
|
|
148
155
|
const {content, contentTitle} = parseMarkdownContentTitle(
|
|
149
156
|
contentWithoutFrontMatter,
|
|
@@ -176,7 +183,7 @@ export async function parseMarkdownFile(
|
|
|
176
183
|
return parseMarkdownString(markdownString, options);
|
|
177
184
|
} catch (e) {
|
|
178
185
|
throw new Error(
|
|
179
|
-
`Error while parsing Markdown file ${source}: "${e.message}".`,
|
|
186
|
+
`Error while parsing Markdown file ${source}: "${(e as Error).message}".`,
|
|
180
187
|
);
|
|
181
188
|
}
|
|
182
189
|
}
|
package/src/mdxUtils.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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 React from 'react';
|
|
9
|
+
import ReactDOMServer from 'react-dom/server';
|
|
10
|
+
import MDX from '@mdx-js/runtime';
|
|
11
|
+
import removeImports from 'remark-mdx-remove-imports';
|
|
12
|
+
import removeExports from 'remark-mdx-remove-exports';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Transform mdx text to plain html text
|
|
16
|
+
* Initially created to convert MDX blog posts to HTML for the RSS feed
|
|
17
|
+
* without import/export nodes
|
|
18
|
+
*
|
|
19
|
+
* TODO not ideal implementation, won't work well with MDX elements!
|
|
20
|
+
* TODO theme+global site config should be able to declare MDX comps in scope for rendering the RSS feeds
|
|
21
|
+
* see also https://github.com/facebook/docusaurus/issues/4625
|
|
22
|
+
*/
|
|
23
|
+
export function mdxToHtml(
|
|
24
|
+
mdxStr: string,
|
|
25
|
+
// TODO allow providing components/scope here, see https://github.com/mdx-js/mdx/tree/v1.6.13/packages/runtime
|
|
26
|
+
): string {
|
|
27
|
+
return ReactDOMServer.renderToString(
|
|
28
|
+
React.createElement(MDX, {remarkPlugins: [removeImports, removeExports]}, [
|
|
29
|
+
mdxStr,
|
|
30
|
+
]),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
export function normalizeUrl(rawUrls: string[]): string {
|
|
9
|
+
const urls = [...rawUrls];
|
|
10
|
+
const resultArray = [];
|
|
11
|
+
|
|
12
|
+
let hasStartingSlash = false;
|
|
13
|
+
let hasEndingSlash = false;
|
|
14
|
+
|
|
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
|
+
|
|
21
|
+
// There must be two or three slashes in the file protocol,
|
|
22
|
+
// two slashes in anything else.
|
|
23
|
+
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
24
|
+
urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
|
|
25
|
+
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
for (let i = 0; i < urls.length; i++) {
|
|
28
|
+
let component = urls[i];
|
|
29
|
+
|
|
30
|
+
if (typeof component !== 'string') {
|
|
31
|
+
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (component === '') {
|
|
35
|
+
if (i === urls.length - 1 && hasEndingSlash) {
|
|
36
|
+
resultArray.push('/');
|
|
37
|
+
}
|
|
38
|
+
// eslint-disable-next-line
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (component !== '/') {
|
|
43
|
+
if (i > 0) {
|
|
44
|
+
// Removing the starting slashes for each component but the first.
|
|
45
|
+
component = component.replace(
|
|
46
|
+
/^[/]+/,
|
|
47
|
+
// Special case where the first element of rawUrls is empty ["", "/hello"] => /hello
|
|
48
|
+
component[0] === '/' && !hasStartingSlash ? '/' : '',
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
hasEndingSlash = component[component.length - 1] === '/';
|
|
53
|
+
// Removing the ending slashes for each component but the last.
|
|
54
|
+
// For the last component we will combine multiple slashes to a single one.
|
|
55
|
+
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
hasStartingSlash = true;
|
|
59
|
+
resultArray.push(component);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let str = resultArray.join('/');
|
|
63
|
+
// Each input component is now separated by a single slash
|
|
64
|
+
// except the possible first plain protocol part.
|
|
65
|
+
|
|
66
|
+
// Remove trailing slash before parameters or hash.
|
|
67
|
+
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
|
|
68
|
+
|
|
69
|
+
// Replace ? in parameters with &.
|
|
70
|
+
const parts = str.split('?');
|
|
71
|
+
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
72
|
+
|
|
73
|
+
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
74
|
+
str = str.replace(/([^:]\/)\/+/g, '$1');
|
|
75
|
+
|
|
76
|
+
// Dedupe forward slashes at the beginning of the path.
|
|
77
|
+
str = str.replace(/^\/+/g, '/');
|
|
78
|
+
|
|
79
|
+
return str;
|
|
80
|
+
}
|
package/src/pathUtils.ts
CHANGED
|
@@ -7,8 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
9
9
|
|
|
10
|
-
import {createHash} from 'crypto';
|
|
11
|
-
|
|
12
10
|
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
|
|
13
11
|
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
14
12
|
const MAX_PATH_SEGMENT_BYTES = 255;
|
|
@@ -42,7 +40,3 @@ export const shortName = (str: string): string => {
|
|
|
42
40
|
)
|
|
43
41
|
.toString();
|
|
44
42
|
};
|
|
45
|
-
|
|
46
|
-
export function simpleHash(str: string, length: number): string {
|
|
47
|
-
return createHash('md5').update(str).digest('hex').substr(0, length);
|
|
48
|
-
}
|