@docusaurus/utils 2.0.0-beta.15d451942 → 2.0.0-beta.16
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/README.md +1 -1
- package/lib/constants.d.ts +20 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +27 -0
- package/lib/constants.js.map +1 -0
- package/lib/dataFileUtils.d.ts +24 -0
- package/lib/dataFileUtils.d.ts.map +1 -0
- package/lib/dataFileUtils.js +65 -0
- package/lib/dataFileUtils.js.map +1 -0
- package/lib/gitUtils.d.ts +17 -0
- package/lib/gitUtils.d.ts.map +1 -0
- package/lib/gitUtils.js +63 -0
- package/lib/gitUtils.js.map +1 -0
- package/lib/globUtils.d.ts +12 -0
- package/lib/globUtils.d.ts.map +1 -0
- package/lib/globUtils.js +48 -0
- package/lib/globUtils.js.map +1 -0
- package/lib/hashUtils.d.ts +16 -0
- package/lib/hashUtils.d.ts.map +1 -0
- package/lib/hashUtils.js +41 -0
- package/lib/hashUtils.js.map +1 -0
- package/lib/index.d.ts +30 -52
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +126 -247
- package/lib/index.js.map +1 -0
- package/lib/markdownLinks.d.ts +1 -0
- package/lib/markdownLinks.d.ts.map +1 -0
- package/lib/markdownLinks.js +36 -11
- package/lib/markdownLinks.js.map +1 -0
- package/lib/markdownParser.d.ts +7 -3
- package/lib/markdownParser.d.ts.map +1 -0
- package/lib/markdownParser.js +77 -48
- package/lib/markdownParser.js.map +1 -0
- package/lib/pathUtils.d.ts +51 -0
- package/lib/pathUtils.d.ts.map +1 -0
- package/lib/pathUtils.js +106 -0
- package/lib/pathUtils.js.map +1 -0
- package/lib/slugger.d.ts +14 -0
- package/lib/slugger.d.ts.map +1 -0
- package/lib/slugger.js +19 -0
- package/lib/slugger.js.map +1 -0
- package/lib/tags.d.ts +27 -0
- package/lib/tags.d.ts.map +1 -0
- package/lib/tags.js +77 -0
- package/lib/tags.js.map +1 -0
- package/lib/urlUtils.d.ts +9 -0
- package/lib/urlUtils.d.ts.map +1 -0
- package/lib/urlUtils.js +81 -0
- package/lib/urlUtils.js.map +1 -0
- package/lib/webpackUtils.d.ts +30 -0
- package/lib/webpackUtils.d.ts.map +1 -0
- package/lib/webpackUtils.js +112 -0
- package/lib/webpackUtils.js.map +1 -0
- package/package.json +20 -10
- package/src/constants.ts +38 -0
- package/src/dataFileUtils.ts +90 -0
- package/src/deps.d.ts +10 -0
- package/src/gitUtils.ts +93 -0
- package/src/globUtils.ts +64 -0
- package/src/hashUtils.ts +37 -0
- package/src/index.ts +135 -294
- package/src/markdownLinks.ts +35 -13
- package/src/markdownParser.ts +86 -62
- package/src/pathUtils.ts +115 -0
- package/src/slugger.ts +24 -0
- package/src/tags.ts +105 -0
- package/src/urlUtils.ts +96 -0
- package/src/webpackUtils.ts +146 -0
- package/lib/.tsbuildinfo +0 -3972
- package/lib/codeTranslationsUtils.d.ts +0 -11
- package/lib/codeTranslationsUtils.js +0 -50
- package/lib/escapePath.d.ts +0 -17
- package/lib/escapePath.js +0 -25
- package/lib/posixPath.d.ts +0 -14
- package/lib/posixPath.js +0 -28
- package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
- package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
- package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
- package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
- package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
- package/src/__tests__/escapePath.test.ts +0 -25
- package/src/__tests__/index.test.ts +0 -681
- package/src/__tests__/markdownParser.test.ts +0 -772
- package/src/__tests__/posixPath.test.ts +0 -25
- package/src/codeTranslationsUtils.ts +0 -56
- package/src/escapePath.ts +0 -23
- package/src/posixPath.ts +0 -27
- package/tsconfig.json +0 -9
package/src/markdownParser.ts
CHANGED
|
@@ -5,10 +5,26 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import fs from 'fs-extra';
|
|
8
|
+
import logger from '@docusaurus/logger';
|
|
10
9
|
import matter from 'gray-matter';
|
|
11
10
|
|
|
11
|
+
// Input: ## Some heading {#some-heading}
|
|
12
|
+
// Output: {text: "## Some heading", id: "some-heading"}
|
|
13
|
+
export function parseMarkdownHeadingId(heading: string): {
|
|
14
|
+
text: string;
|
|
15
|
+
id?: string;
|
|
16
|
+
} {
|
|
17
|
+
const customHeadingIdRegex = /^(?<text>.*?)\s*\{#(?<id>[\w-]+)\}$/;
|
|
18
|
+
const matches = customHeadingIdRegex.exec(heading);
|
|
19
|
+
if (matches) {
|
|
20
|
+
return {
|
|
21
|
+
text: matches.groups!.text,
|
|
22
|
+
id: matches.groups!.id,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return {text: heading, id: undefined};
|
|
26
|
+
}
|
|
27
|
+
|
|
12
28
|
// Hacky way of stripping out import statements from the excerpt
|
|
13
29
|
// TODO: Find a better way to do so, possibly by compiling the Markdown content,
|
|
14
30
|
// stripping out HTML tags and obtaining the first line.
|
|
@@ -18,9 +34,10 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
18
34
|
// Remove Markdown alternate title
|
|
19
35
|
.replace(/^[^\n]*\n[=]+/g, '')
|
|
20
36
|
.split('\n');
|
|
37
|
+
let inCode = false;
|
|
38
|
+
let lastCodeFence = '';
|
|
21
39
|
|
|
22
40
|
/* eslint-disable no-continue */
|
|
23
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
24
41
|
for (const fileLine of fileLines) {
|
|
25
42
|
// Skip empty line.
|
|
26
43
|
if (!fileLine.trim()) {
|
|
@@ -28,7 +45,24 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
28
45
|
}
|
|
29
46
|
|
|
30
47
|
// Skip import/export declaration.
|
|
31
|
-
if (
|
|
48
|
+
if (/^(?:import|export)\s.*/.test(fileLine)) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Skip code block line.
|
|
53
|
+
if (fileLine.trim().startsWith('```')) {
|
|
54
|
+
if (!inCode) {
|
|
55
|
+
inCode = true;
|
|
56
|
+
[lastCodeFence] = fileLine.trim().match(/^`+/)!;
|
|
57
|
+
// If we are in a ````-fenced block, all ``` would be plain text instead
|
|
58
|
+
// of fences
|
|
59
|
+
} else if (
|
|
60
|
+
fileLine.trim().match(/^`+/)![0].length >= lastCodeFence.length
|
|
61
|
+
) {
|
|
62
|
+
inCode = false;
|
|
63
|
+
}
|
|
64
|
+
continue;
|
|
65
|
+
} else if (inCode) {
|
|
32
66
|
continue;
|
|
33
67
|
}
|
|
34
68
|
|
|
@@ -36,25 +70,29 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
36
70
|
// Remove HTML tags.
|
|
37
71
|
.replace(/<[^>]*>/g, '')
|
|
38
72
|
// Remove Title headers
|
|
39
|
-
.replace(
|
|
73
|
+
.replace(/^#\s*[^#]*\s*#?/gm, '')
|
|
40
74
|
// Remove Markdown + ATX-style headers
|
|
41
|
-
.replace(
|
|
42
|
-
// Remove emphasis
|
|
43
|
-
.replace(/([
|
|
75
|
+
.replace(/^#{1,6}\s*(?<text>[^#]*)\s*(?:#{1,6})?/gm, '$1')
|
|
76
|
+
// Remove emphasis.
|
|
77
|
+
.replace(/(?<opening>[*_]{1,3})(?<text>.*?)\1/g, '$2')
|
|
78
|
+
// Remove strikethroughs.
|
|
79
|
+
.replace(/~~(?<text>\S.*\S)~~/g, '$1')
|
|
44
80
|
// Remove images.
|
|
45
|
-
.replace(
|
|
81
|
+
.replace(/!\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
|
|
46
82
|
// Remove footnotes.
|
|
47
|
-
.replace(/\[\^.+?\](
|
|
83
|
+
.replace(/\[\^.+?\](?:: .*?$)?/g, '')
|
|
48
84
|
// Remove inline links.
|
|
49
|
-
.replace(/\[(
|
|
85
|
+
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
|
|
50
86
|
// Remove inline code.
|
|
51
|
-
.replace(/`(
|
|
87
|
+
.replace(/`(?<text>.+?)`/g, '$1')
|
|
52
88
|
// Remove blockquotes.
|
|
53
89
|
.replace(/^\s{0,3}>\s?/g, '')
|
|
54
90
|
// Remove admonition definition.
|
|
55
|
-
.replace(
|
|
91
|
+
.replace(/:::.*/, '')
|
|
56
92
|
// Remove Emoji names within colons include preceding whitespace.
|
|
57
|
-
.replace(/\s
|
|
93
|
+
.replace(/\s?:(?:::|[^:\n])+:/g, '')
|
|
94
|
+
// Remove custom Markdown heading id.
|
|
95
|
+
.replace(/{#*[\w-]+}/, '')
|
|
58
96
|
.trim();
|
|
59
97
|
|
|
60
98
|
if (cleanedLine) {
|
|
@@ -65,31 +103,42 @@ export function createExcerpt(fileString: string): string | undefined {
|
|
|
65
103
|
return undefined;
|
|
66
104
|
}
|
|
67
105
|
|
|
68
|
-
export function parseFrontMatter(
|
|
69
|
-
markdownFileContent: string,
|
|
70
|
-
): {
|
|
106
|
+
export function parseFrontMatter(markdownFileContent: string): {
|
|
71
107
|
frontMatter: Record<string, unknown>;
|
|
72
108
|
content: string;
|
|
73
109
|
} {
|
|
74
110
|
const {data, content} = matter(markdownFileContent);
|
|
75
111
|
return {
|
|
76
|
-
frontMatter: data
|
|
77
|
-
content: content
|
|
112
|
+
frontMatter: data,
|
|
113
|
+
content: content.trim(),
|
|
78
114
|
};
|
|
79
115
|
}
|
|
80
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Try to convert markdown heading to text. Does not need to be perfect, it is
|
|
119
|
+
* only used as a fallback when frontMatter.title is not provided. For now, we
|
|
120
|
+
* just unwrap possible inline code blocks (# `config.js`)
|
|
121
|
+
*/
|
|
122
|
+
function toTextContentTitle(contentTitle: string): string {
|
|
123
|
+
if (contentTitle.startsWith('`') && contentTitle.endsWith('`')) {
|
|
124
|
+
return contentTitle.substring(1, contentTitle.length - 1);
|
|
125
|
+
}
|
|
126
|
+
return contentTitle;
|
|
127
|
+
}
|
|
128
|
+
|
|
81
129
|
export function parseMarkdownContentTitle(
|
|
82
130
|
contentUntrimmed: string,
|
|
83
|
-
options?: {
|
|
131
|
+
options?: {removeContentTitle?: boolean},
|
|
84
132
|
): {content: string; contentTitle: string | undefined} {
|
|
85
|
-
const
|
|
133
|
+
const removeContentTitleOption = options?.removeContentTitle ?? false;
|
|
86
134
|
|
|
87
135
|
const content = contentUntrimmed.trim();
|
|
88
136
|
|
|
89
|
-
const IMPORT_STATEMENT =
|
|
90
|
-
|
|
91
|
-
const REGULAR_TITLE =
|
|
92
|
-
|
|
137
|
+
const IMPORT_STATEMENT =
|
|
138
|
+
/import\s+(?:[\w*{}\s\n,]+from\s+)?["'\s][@\w/_.-]+["'\s];?|\n/.source;
|
|
139
|
+
const REGULAR_TITLE =
|
|
140
|
+
/(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>(?:{#*[\w-]+})|#)?\n*?)/
|
|
141
|
+
.source;
|
|
93
142
|
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
|
|
94
143
|
|
|
95
144
|
const regularTitleMatch = new RegExp(
|
|
@@ -107,14 +156,12 @@ export function parseMarkdownContentTitle(
|
|
|
107
156
|
if (!pattern || !title) {
|
|
108
157
|
return {content, contentTitle: undefined};
|
|
109
158
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
: content.replace(pattern, '');
|
|
114
|
-
|
|
159
|
+
const newContent = removeContentTitleOption
|
|
160
|
+
? content.replace(pattern, '')
|
|
161
|
+
: content;
|
|
115
162
|
return {
|
|
116
163
|
content: newContent.trim(),
|
|
117
|
-
contentTitle: title.trim(),
|
|
164
|
+
contentTitle: toTextContentTitle(title.trim()).trim(),
|
|
118
165
|
};
|
|
119
166
|
}
|
|
120
167
|
|
|
@@ -127,22 +174,15 @@ type ParsedMarkdown = {
|
|
|
127
174
|
|
|
128
175
|
export function parseMarkdownString(
|
|
129
176
|
markdownFileContent: string,
|
|
130
|
-
options?: {
|
|
131
|
-
keepContentTitle?: boolean;
|
|
132
|
-
},
|
|
177
|
+
options?: {removeContentTitle?: boolean},
|
|
133
178
|
): ParsedMarkdown {
|
|
134
179
|
try {
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
const {frontMatter, content: contentWithoutFrontMatter} = parseFrontMatter(
|
|
138
|
-
markdownFileContent,
|
|
139
|
-
);
|
|
180
|
+
const {frontMatter, content: contentWithoutFrontMatter} =
|
|
181
|
+
parseFrontMatter(markdownFileContent);
|
|
140
182
|
|
|
141
183
|
const {content, contentTitle} = parseMarkdownContentTitle(
|
|
142
184
|
contentWithoutFrontMatter,
|
|
143
|
-
|
|
144
|
-
keepContentTitle,
|
|
145
|
-
},
|
|
185
|
+
options,
|
|
146
186
|
);
|
|
147
187
|
|
|
148
188
|
const excerpt = createExcerpt(content);
|
|
@@ -153,25 +193,9 @@ export function parseMarkdownString(
|
|
|
153
193
|
contentTitle,
|
|
154
194
|
excerpt,
|
|
155
195
|
};
|
|
156
|
-
} catch (
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
);
|
|
161
|
-
throw e;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export async function parseMarkdownFile(
|
|
166
|
-
source: string,
|
|
167
|
-
): Promise<ParsedMarkdown> {
|
|
168
|
-
const markdownString = await fs.readFile(source, 'utf-8');
|
|
169
|
-
try {
|
|
170
|
-
return parseMarkdownString(markdownString);
|
|
171
|
-
} catch (e) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
`Error while parsing markdown file ${source}
|
|
174
|
-
${e.message}`,
|
|
175
|
-
);
|
|
196
|
+
} catch (err) {
|
|
197
|
+
logger.error(`Error while parsing Markdown front matter.
|
|
198
|
+
This can happen if you use special characters in front matter values (try using double quotes around that value).`);
|
|
199
|
+
throw err;
|
|
176
200
|
}
|
|
177
201
|
}
|
package/src/pathUtils.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
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 https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
9
|
+
|
|
10
|
+
import path from 'path';
|
|
11
|
+
|
|
12
|
+
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars,
|
|
13
|
+
// Others = 255 bytes
|
|
14
|
+
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
15
|
+
const MAX_PATH_SEGMENT_BYTES = 255;
|
|
16
|
+
// Space for appending things to the string like file extensions and so on
|
|
17
|
+
const SPACE_FOR_APPENDING = 10;
|
|
18
|
+
|
|
19
|
+
const isMacOs = () => process.platform === 'darwin';
|
|
20
|
+
const isWindows = () => process.platform === 'win32';
|
|
21
|
+
|
|
22
|
+
export const isNameTooLong = (str: string): boolean =>
|
|
23
|
+
// Not entirely correct: we can't assume FS from OS. But good enough?
|
|
24
|
+
isMacOs() || isWindows()
|
|
25
|
+
? str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars)
|
|
26
|
+
: Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes)
|
|
27
|
+
|
|
28
|
+
export const shortName = (str: string): string => {
|
|
29
|
+
if (isMacOs() || isWindows()) {
|
|
30
|
+
const overflowingChars = str.length - MAX_PATH_SEGMENT_CHARS;
|
|
31
|
+
return str.slice(
|
|
32
|
+
0,
|
|
33
|
+
str.length - overflowingChars - SPACE_FOR_APPENDING - 1,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const strBuffer = Buffer.from(str);
|
|
37
|
+
const overflowingBytes =
|
|
38
|
+
Buffer.byteLength(strBuffer) - MAX_PATH_SEGMENT_BYTES;
|
|
39
|
+
return strBuffer
|
|
40
|
+
.slice(
|
|
41
|
+
0,
|
|
42
|
+
Buffer.byteLength(strBuffer) - overflowingBytes - SPACE_FOR_APPENDING - 1,
|
|
43
|
+
)
|
|
44
|
+
.toString();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Convert Windows backslash paths to posix style paths.
|
|
49
|
+
* E.g: endi\lie -> endi/lie
|
|
50
|
+
*
|
|
51
|
+
* Returns original path if the posix counterpart is not valid Windows path.
|
|
52
|
+
* This makes the legacy code that uses posixPath safe; but also makes it less
|
|
53
|
+
* useful when you actually want a path with forward slashes (e.g. for URL)
|
|
54
|
+
*
|
|
55
|
+
* Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
56
|
+
*/
|
|
57
|
+
export function posixPath(str: string): string {
|
|
58
|
+
const isExtendedLengthPath = /^\\\\\?\\/.test(str);
|
|
59
|
+
|
|
60
|
+
// Forward slashes are only valid Windows paths when they don't contain non-
|
|
61
|
+
// ascii characters.
|
|
62
|
+
// eslint-disable-next-line no-control-regex
|
|
63
|
+
const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
|
|
64
|
+
|
|
65
|
+
if (isExtendedLengthPath || hasNonAscii) {
|
|
66
|
+
return str;
|
|
67
|
+
}
|
|
68
|
+
return str.replace(/\\/g, '/');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* When you want to display a path in a message/warning/error, it's more
|
|
73
|
+
* convenient to:
|
|
74
|
+
*
|
|
75
|
+
* - make it relative to `cwd()`
|
|
76
|
+
* - convert to posix (ie not using windows \ path separator)
|
|
77
|
+
*
|
|
78
|
+
* This way, Jest tests can run more reliably on any computer/CI on both
|
|
79
|
+
* Unix/Windows
|
|
80
|
+
* For Windows users this is not perfect (as they see / instead of \) but it's
|
|
81
|
+
* probably good enough
|
|
82
|
+
*/
|
|
83
|
+
export function toMessageRelativeFilePath(filePath: string): string {
|
|
84
|
+
return posixPath(path.relative(process.cwd(), filePath));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Alias filepath relative to site directory, very useful so that we
|
|
89
|
+
* don't expose user's site structure.
|
|
90
|
+
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
|
91
|
+
*/
|
|
92
|
+
export function aliasedSitePath(filePath: string, siteDir: string): string {
|
|
93
|
+
const relativePath = posixPath(path.relative(siteDir, filePath));
|
|
94
|
+
// Cannot use path.join() as it resolves '../' and removes
|
|
95
|
+
// the '@site'. Let webpack loader resolve it.
|
|
96
|
+
return `@site/${relativePath}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* When you have a path like C:\X\Y
|
|
101
|
+
* It is not safe to use directly when generating code
|
|
102
|
+
* For example, this would fail due to unescaped \:
|
|
103
|
+
* `<img src={require('${filePath}')} />`
|
|
104
|
+
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
105
|
+
*
|
|
106
|
+
* posixPath can't be used in all cases, because forward slashes are only valid
|
|
107
|
+
* Windows paths when they don't contain non-ascii characters, and posixPath
|
|
108
|
+
* doesn't escape those that fail to be converted.
|
|
109
|
+
*/
|
|
110
|
+
export function escapePath(str: string): string {
|
|
111
|
+
const escaped = JSON.stringify(str);
|
|
112
|
+
|
|
113
|
+
// Remove the " around the json string;
|
|
114
|
+
return escaped.substring(1, escaped.length - 1);
|
|
115
|
+
}
|
package/src/slugger.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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 GithubSlugger from 'github-slugger';
|
|
9
|
+
|
|
10
|
+
// We create our own abstraction on top of the lib:
|
|
11
|
+
// - unify usage everywhere in the codebase
|
|
12
|
+
// - ability to add extra options
|
|
13
|
+
export type SluggerOptions = {maintainCase?: boolean};
|
|
14
|
+
|
|
15
|
+
export type Slugger = {
|
|
16
|
+
slug: (value: string, options?: SluggerOptions) => string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function createSlugger(): Slugger {
|
|
20
|
+
const githubSlugger = new GithubSlugger();
|
|
21
|
+
return {
|
|
22
|
+
slug: (value, options) => githubSlugger.slug(value, options?.maintainCase),
|
|
23
|
+
};
|
|
24
|
+
}
|
package/src/tags.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
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 _ from 'lodash';
|
|
9
|
+
import {normalizeUrl} from './urlUtils';
|
|
10
|
+
|
|
11
|
+
export type Tag = {
|
|
12
|
+
label: string;
|
|
13
|
+
permalink: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type FrontMatterTag = string | Tag;
|
|
17
|
+
|
|
18
|
+
export function normalizeFrontMatterTag(
|
|
19
|
+
tagsPath: string,
|
|
20
|
+
frontMatterTag: FrontMatterTag,
|
|
21
|
+
): Tag {
|
|
22
|
+
function toTagObject(tagString: string): Tag {
|
|
23
|
+
return {
|
|
24
|
+
label: tagString,
|
|
25
|
+
permalink: _.kebabCase(tagString),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// TODO maybe make ensure the permalink is valid url path?
|
|
30
|
+
function normalizeTagPermalink(permalink: string): string {
|
|
31
|
+
// note: we always apply tagsPath on purpose. For versioned docs, v1/doc.md
|
|
32
|
+
// and v2/doc.md tags with custom permalinks don't lead to the same created
|
|
33
|
+
// page. tagsPath is different for each doc version
|
|
34
|
+
return normalizeUrl([tagsPath, permalink]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const tag: Tag =
|
|
38
|
+
typeof frontMatterTag === 'string'
|
|
39
|
+
? toTagObject(frontMatterTag)
|
|
40
|
+
: frontMatterTag;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
label: tag.label,
|
|
44
|
+
permalink: normalizeTagPermalink(tag.permalink),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function normalizeFrontMatterTags(
|
|
49
|
+
tagsPath: string,
|
|
50
|
+
frontMatterTags: FrontMatterTag[] | undefined = [],
|
|
51
|
+
): Tag[] {
|
|
52
|
+
const tags = frontMatterTags.map((tag) =>
|
|
53
|
+
normalizeFrontMatterTag(tagsPath, tag),
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return _.uniqBy(tags, (tag) => tag.permalink);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type TaggedItemGroup<Item> = {
|
|
60
|
+
tag: Tag;
|
|
61
|
+
items: Item[];
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Permits to group docs/blogPosts by tag (provided by front matter)
|
|
66
|
+
* Note: groups are indexed by permalink, because routes must be unique in the
|
|
67
|
+
* end. Labels may vary on 2 md files but they are normalized. Docs with
|
|
68
|
+
* label='some label' and label='some-label' should end-up in the same
|
|
69
|
+
* group/page in the end. We can't create 2 routes /some-label because one would
|
|
70
|
+
* override the other
|
|
71
|
+
*/
|
|
72
|
+
export function groupTaggedItems<Item>(
|
|
73
|
+
items: Item[],
|
|
74
|
+
getItemTags: (item: Item) => Tag[],
|
|
75
|
+
): Record<string, TaggedItemGroup<Item>> {
|
|
76
|
+
const result: Record<string, TaggedItemGroup<Item>> = {};
|
|
77
|
+
|
|
78
|
+
function handleItemTag(item: Item, tag: Tag) {
|
|
79
|
+
// Init missing tag groups
|
|
80
|
+
// TODO: it's not really clear what should be the behavior if 2 items have
|
|
81
|
+
// the same tag but the permalink is different for each
|
|
82
|
+
// For now, the first tag found wins
|
|
83
|
+
result[tag.permalink] = result[tag.permalink] ?? {
|
|
84
|
+
tag,
|
|
85
|
+
items: [],
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Add item to group
|
|
89
|
+
result[tag.permalink].items.push(item);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
items.forEach((item) => {
|
|
93
|
+
getItemTags(item).forEach((tag) => {
|
|
94
|
+
handleItemTag(item, tag);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// If user add twice the same tag to a md doc (weird but possible),
|
|
99
|
+
// we don't want the item to appear twice in the list...
|
|
100
|
+
Object.values(result).forEach((group) => {
|
|
101
|
+
group.items = _.uniq(group.items);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return result;
|
|
105
|
+
}
|
package/src/urlUtils.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
if (first!.startsWith('file:') && urls[0].startsWith('/')) {
|
|
19
|
+
// Force a double slash here, else we lose the information that the next
|
|
20
|
+
// segment is an absolute path
|
|
21
|
+
urls[0] = `${first}//${urls[0]}`;
|
|
22
|
+
} else {
|
|
23
|
+
urls[0] = first + urls[0];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// There must be two or three slashes in the file protocol,
|
|
28
|
+
// two slashes in anything else.
|
|
29
|
+
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
30
|
+
urls[0] = urls[0].replace(/^(?<protocol>[^/:]+):\/*/, replacement);
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < urls.length; i += 1) {
|
|
33
|
+
let component = urls[i];
|
|
34
|
+
|
|
35
|
+
if (typeof component !== 'string') {
|
|
36
|
+
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (component === '') {
|
|
40
|
+
if (i === urls.length - 1 && hasEndingSlash) {
|
|
41
|
+
resultArray.push('/');
|
|
42
|
+
}
|
|
43
|
+
// eslint-disable-next-line no-continue
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (component !== '/') {
|
|
48
|
+
if (i > 0) {
|
|
49
|
+
// Removing the starting slashes for each component but the first.
|
|
50
|
+
component = component.replace(
|
|
51
|
+
/^[/]+/,
|
|
52
|
+
// Special case where the first element of rawUrls is empty
|
|
53
|
+
// ["", "/hello"] => /hello
|
|
54
|
+
component[0] === '/' && !hasStartingSlash ? '/' : '',
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
hasEndingSlash = component[component.length - 1] === '/';
|
|
59
|
+
// Removing the ending slashes for each component but the last. For the
|
|
60
|
+
// last component we will combine multiple slashes to a single one.
|
|
61
|
+
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
hasStartingSlash = true;
|
|
65
|
+
resultArray.push(component);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let str = resultArray.join('/');
|
|
69
|
+
// Each input component is now separated by a single slash
|
|
70
|
+
// except the possible first plain protocol part.
|
|
71
|
+
|
|
72
|
+
// Remove trailing slash before parameters or hash.
|
|
73
|
+
str = str.replace(/\/(?<search>\?|&|#[^!])/g, '$1');
|
|
74
|
+
|
|
75
|
+
// Replace ? in parameters with &.
|
|
76
|
+
const parts = str.split('?');
|
|
77
|
+
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
78
|
+
|
|
79
|
+
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
80
|
+
str = str.replace(/(?<textBefore>[^:/]\/)\/+/g, '$1');
|
|
81
|
+
|
|
82
|
+
// Dedupe forward slashes at the beginning of the path.
|
|
83
|
+
str = str.replace(/^\/+/g, '/');
|
|
84
|
+
|
|
85
|
+
return str;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function getEditUrl(
|
|
89
|
+
fileRelativePath: string,
|
|
90
|
+
editUrl?: string,
|
|
91
|
+
): string | undefined {
|
|
92
|
+
return editUrl
|
|
93
|
+
? // Don't use posixPath for this: we need to force a forward slash path
|
|
94
|
+
normalizeUrl([editUrl, fileRelativePath.replace(/\\/g, '/')])
|
|
95
|
+
: undefined;
|
|
96
|
+
}
|