@docusaurus/utils 2.0.0-beta.15d451942 → 2.0.0-beta.18
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 +68 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +71 -0
- package/lib/constants.js.map +1 -0
- package/lib/dataFileUtils.d.ts +60 -0
- package/lib/dataFileUtils.d.ts.map +1 -0
- package/lib/dataFileUtils.js +91 -0
- package/lib/dataFileUtils.js.map +1 -0
- package/lib/emitUtils.d.ts +35 -0
- package/lib/emitUtils.d.ts.map +1 -0
- package/lib/emitUtils.js +107 -0
- package/lib/emitUtils.js.map +1 -0
- package/lib/gitUtils.d.ts +66 -0
- package/lib/gitUtils.d.ts.map +1 -0
- package/lib/gitUtils.js +66 -0
- package/lib/gitUtils.js.map +1 -0
- package/lib/globUtils.d.ts +39 -0
- package/lib/globUtils.d.ts.map +1 -0
- package/lib/globUtils.js +66 -0
- package/lib/globUtils.js.map +1 -0
- package/lib/hashUtils.d.ts +17 -0
- package/lib/hashUtils.d.ts.map +1 -0
- package/lib/hashUtils.js +42 -0
- package/lib/hashUtils.js.map +1 -0
- package/lib/i18nUtils.d.ts +28 -0
- package/lib/i18nUtils.d.ts.map +1 -0
- package/lib/i18nUtils.js +49 -0
- package/lib/i18nUtils.js.map +1 -0
- package/lib/index.d.ts +16 -82
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +89 -411
- package/lib/index.js.map +1 -0
- package/lib/jsUtils.d.ts +45 -0
- package/lib/jsUtils.d.ts.map +1 -0
- package/lib/jsUtils.js +94 -0
- package/lib/jsUtils.js.map +1 -0
- package/lib/markdownLinks.d.ts +49 -5
- package/lib/markdownLinks.d.ts.map +1 -0
- package/lib/markdownLinks.js +48 -13
- package/lib/markdownLinks.js.map +1 -0
- package/lib/markdownUtils.d.ts +112 -0
- package/lib/markdownUtils.d.ts.map +1 -0
- package/lib/markdownUtils.js +271 -0
- package/lib/markdownUtils.js.map +1 -0
- package/lib/pathUtils.d.ts +52 -0
- package/lib/pathUtils.d.ts.map +1 -0
- package/lib/pathUtils.js +113 -0
- package/lib/pathUtils.js.map +1 -0
- package/lib/slugger.d.ts +24 -0
- package/lib/slugger.d.ts.map +1 -0
- package/lib/slugger.js +23 -0
- package/lib/slugger.js.map +1 -0
- package/lib/tags.d.ts +48 -0
- package/lib/tags.d.ts.map +1 -0
- package/lib/tags.js +91 -0
- package/lib/tags.js.map +1 -0
- package/lib/urlUtils.d.ts +66 -0
- package/lib/urlUtils.d.ts.map +1 -0
- package/lib/urlUtils.js +208 -0
- package/lib/urlUtils.js.map +1 -0
- package/lib/webpackUtils.d.ts +35 -0
- package/lib/webpackUtils.d.ts.map +1 -0
- package/lib/webpackUtils.js +116 -0
- package/lib/webpackUtils.js.map +1 -0
- package/package.json +20 -10
- package/src/constants.ts +90 -0
- package/src/dataFileUtils.ts +122 -0
- package/src/deps.d.ts +10 -0
- package/src/emitUtils.ts +130 -0
- package/src/gitUtils.ts +153 -0
- package/src/globUtils.ts +80 -0
- package/src/hashUtils.ts +38 -0
- package/src/i18nUtils.ts +67 -0
- package/src/index.ts +94 -521
- package/src/jsUtils.ts +102 -0
- package/src/markdownLinks.ts +89 -30
- package/src/markdownUtils.ts +354 -0
- package/src/pathUtils.ts +121 -0
- package/src/slugger.ts +36 -0
- package/src/tags.ts +117 -0
- package/src/urlUtils.ts +235 -0
- package/src/webpackUtils.ts +154 -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/markdownParser.d.ts +0 -28
- package/lib/markdownParser.js +0 -132
- 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/markdownParser.ts +0 -177
- package/src/posixPath.ts +0 -27
- package/tsconfig.json +0 -9
package/src/emitUtils.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
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 path from 'path';
|
|
9
|
+
import fs from 'fs-extra';
|
|
10
|
+
import {createHash} from 'crypto';
|
|
11
|
+
import {simpleHash, docuHash} from './hashUtils';
|
|
12
|
+
import {findAsyncSequential} from './jsUtils';
|
|
13
|
+
|
|
14
|
+
const fileHash = new Map<string, string>();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Outputs a file to the generated files directory. Only writes files if content
|
|
18
|
+
* differs from cache (for hot reload performance).
|
|
19
|
+
*
|
|
20
|
+
* @param generatedFilesDir Absolute path.
|
|
21
|
+
* @param file Path relative to `generatedFilesDir`.
|
|
22
|
+
* @param content String content to write.
|
|
23
|
+
* @param skipCache If `true` (defaults as `true` for production), file is
|
|
24
|
+
* force-rewritten, skipping cache.
|
|
25
|
+
*/
|
|
26
|
+
export async function generate(
|
|
27
|
+
generatedFilesDir: string,
|
|
28
|
+
file: string,
|
|
29
|
+
content: string,
|
|
30
|
+
skipCache: boolean = process.env.NODE_ENV === 'production',
|
|
31
|
+
): Promise<void> {
|
|
32
|
+
const filepath = path.join(generatedFilesDir, file);
|
|
33
|
+
|
|
34
|
+
if (skipCache) {
|
|
35
|
+
await fs.outputFile(filepath, content);
|
|
36
|
+
// Cache still needs to be reset, otherwise, writing "A", "B", and "A" where
|
|
37
|
+
// "B" skips cache will cause the last "A" not be able to overwrite as the
|
|
38
|
+
// first "A" remains in cache. But if the file never existed in cache, no
|
|
39
|
+
// need to register it.
|
|
40
|
+
if (fileHash.get(filepath)) {
|
|
41
|
+
fileHash.set(filepath, createHash('md5').update(content).digest('hex'));
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let lastHash = fileHash.get(filepath);
|
|
47
|
+
|
|
48
|
+
// If file already exists but it's not in runtime cache yet, we try to
|
|
49
|
+
// calculate the content hash and then compare. This is to avoid unnecessary
|
|
50
|
+
// overwriting and we can reuse old file.
|
|
51
|
+
if (!lastHash && (await fs.pathExists(filepath))) {
|
|
52
|
+
const lastContent = await fs.readFile(filepath, 'utf8');
|
|
53
|
+
lastHash = createHash('md5').update(lastContent).digest('hex');
|
|
54
|
+
fileHash.set(filepath, lastHash);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const currentHash = createHash('md5').update(content).digest('hex');
|
|
58
|
+
|
|
59
|
+
if (lastHash !== currentHash) {
|
|
60
|
+
await fs.outputFile(filepath, content);
|
|
61
|
+
fileHash.set(filepath, currentHash);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const chunkNameCache = new Map<string, string>();
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Generate unique chunk name given a module path.
|
|
69
|
+
*/
|
|
70
|
+
export function genChunkName(
|
|
71
|
+
modulePath: string,
|
|
72
|
+
prefix?: string,
|
|
73
|
+
preferredName?: string,
|
|
74
|
+
shortId: boolean = process.env.NODE_ENV === 'production',
|
|
75
|
+
): string {
|
|
76
|
+
let chunkName = chunkNameCache.get(modulePath);
|
|
77
|
+
if (!chunkName) {
|
|
78
|
+
if (shortId) {
|
|
79
|
+
chunkName = simpleHash(modulePath, 8);
|
|
80
|
+
} else {
|
|
81
|
+
let str = modulePath;
|
|
82
|
+
if (preferredName) {
|
|
83
|
+
const shortHash = simpleHash(modulePath, 3);
|
|
84
|
+
str = `${preferredName}${shortHash}`;
|
|
85
|
+
}
|
|
86
|
+
const name = str === '/' ? 'index' : docuHash(str);
|
|
87
|
+
chunkName = prefix ? `${prefix}---${name}` : name;
|
|
88
|
+
}
|
|
89
|
+
chunkNameCache.set(modulePath, chunkName);
|
|
90
|
+
}
|
|
91
|
+
return chunkName;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param permalink The URL that the HTML file corresponds to, without base URL
|
|
96
|
+
* @param outDir Full path to the output directory
|
|
97
|
+
* @param trailingSlash The site config option. If provided, only one path will
|
|
98
|
+
* be read.
|
|
99
|
+
* @returns This returns a buffer, which you have to decode string yourself if
|
|
100
|
+
* needed. (Not always necessary since the output isn't for human consumption
|
|
101
|
+
* anyways, and most HTML manipulation libs accept buffers)
|
|
102
|
+
* @throws Throws when the HTML file is not found at any of the potential paths.
|
|
103
|
+
* This should never happen as it would lead to a 404.
|
|
104
|
+
*/
|
|
105
|
+
export async function readOutputHTMLFile(
|
|
106
|
+
permalink: string,
|
|
107
|
+
outDir: string,
|
|
108
|
+
trailingSlash: boolean | undefined,
|
|
109
|
+
): Promise<Buffer> {
|
|
110
|
+
const withTrailingSlashPath = path.join(outDir, permalink, 'index.html');
|
|
111
|
+
const withoutTrailingSlashPath = path.join(
|
|
112
|
+
outDir,
|
|
113
|
+
`${permalink.replace(/\/$/, '')}.html`,
|
|
114
|
+
);
|
|
115
|
+
if (trailingSlash) {
|
|
116
|
+
return fs.readFile(withTrailingSlashPath);
|
|
117
|
+
} else if (trailingSlash === false) {
|
|
118
|
+
return fs.readFile(withoutTrailingSlashPath);
|
|
119
|
+
}
|
|
120
|
+
const HTMLPath = await findAsyncSequential(
|
|
121
|
+
[withTrailingSlashPath, withoutTrailingSlashPath],
|
|
122
|
+
fs.pathExists,
|
|
123
|
+
);
|
|
124
|
+
if (!HTMLPath) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
`Expected output HTML file to be found at ${withTrailingSlashPath}`,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
return fs.readFile(HTMLPath);
|
|
130
|
+
}
|
package/src/gitUtils.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
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 path from 'path';
|
|
9
|
+
import shell from 'shelljs';
|
|
10
|
+
|
|
11
|
+
/** Custom error thrown when git is not found in `PATH`. */
|
|
12
|
+
export class GitNotFoundError extends Error {}
|
|
13
|
+
|
|
14
|
+
/** Custom error thrown when the current file is not tracked by git. */
|
|
15
|
+
export class FileNotTrackedError extends Error {}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fetches the git history of a file and returns a relevant commit date.
|
|
19
|
+
* It gets the commit date instead of author date so that amended commits
|
|
20
|
+
* can have their dates updated.
|
|
21
|
+
*
|
|
22
|
+
* @throws {GitNotFoundError} If git is not found in `PATH`.
|
|
23
|
+
* @throws {FileNotTrackedError} If the current file is not tracked by git.
|
|
24
|
+
* @throws Also throws when `git log` exited with non-zero, or when it outputs
|
|
25
|
+
* unexpected text.
|
|
26
|
+
*/
|
|
27
|
+
export function getFileCommitDate(
|
|
28
|
+
/** Absolute path to the file. */
|
|
29
|
+
file: string,
|
|
30
|
+
args: {
|
|
31
|
+
/**
|
|
32
|
+
* `"oldest"` is the commit that added the file, following renames;
|
|
33
|
+
* `"newest"` is the last commit that edited the file.
|
|
34
|
+
*/
|
|
35
|
+
age?: 'oldest' | 'newest';
|
|
36
|
+
/** Use `includeAuthor: true` to get the author information as well. */
|
|
37
|
+
includeAuthor?: false;
|
|
38
|
+
},
|
|
39
|
+
): {
|
|
40
|
+
/** Relevant commit date. */
|
|
41
|
+
date: Date;
|
|
42
|
+
/** Timestamp in **seconds**, as returned from git. */
|
|
43
|
+
timestamp: number;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Fetches the git history of a file and returns a relevant commit date.
|
|
47
|
+
* It gets the commit date instead of author date so that amended commits
|
|
48
|
+
* can have their dates updated.
|
|
49
|
+
*
|
|
50
|
+
* @throws {GitNotFoundError} If git is not found in `PATH`.
|
|
51
|
+
* @throws {FileNotTrackedError} If the current file is not tracked by git.
|
|
52
|
+
* @throws Also throws when `git log` exited with non-zero, or when it outputs
|
|
53
|
+
* unexpected text.
|
|
54
|
+
*/
|
|
55
|
+
export function getFileCommitDate(
|
|
56
|
+
/** Absolute path to the file. */
|
|
57
|
+
file: string,
|
|
58
|
+
args: {
|
|
59
|
+
/**
|
|
60
|
+
* `"oldest"` is the commit that added the file, following renames;
|
|
61
|
+
* `"newest"` is the last commit that edited the file.
|
|
62
|
+
*/
|
|
63
|
+
age?: 'oldest' | 'newest';
|
|
64
|
+
includeAuthor: true;
|
|
65
|
+
},
|
|
66
|
+
): {
|
|
67
|
+
/** Relevant commit date. */
|
|
68
|
+
date: Date;
|
|
69
|
+
/** Timestamp in **seconds**, as returned from git. */
|
|
70
|
+
timestamp: number;
|
|
71
|
+
/** The author's name, as returned from git. */
|
|
72
|
+
author: string;
|
|
73
|
+
};
|
|
74
|
+
export function getFileCommitDate(
|
|
75
|
+
file: string,
|
|
76
|
+
{
|
|
77
|
+
age = 'oldest',
|
|
78
|
+
includeAuthor = false,
|
|
79
|
+
}: {
|
|
80
|
+
age?: 'oldest' | 'newest';
|
|
81
|
+
includeAuthor?: boolean;
|
|
82
|
+
},
|
|
83
|
+
): {
|
|
84
|
+
date: Date;
|
|
85
|
+
timestamp: number;
|
|
86
|
+
author?: string;
|
|
87
|
+
} {
|
|
88
|
+
if (!shell.which('git')) {
|
|
89
|
+
throw new GitNotFoundError(
|
|
90
|
+
`Failed to retrieve git history for "${file}" because git is not installed.`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!shell.test('-f', file)) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Failed to retrieve git history for "${file}" because the file does not exist.`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let formatArg = '--format=%ct';
|
|
101
|
+
if (includeAuthor) {
|
|
102
|
+
formatArg += ',%an';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let extraArgs = '--max-count=1';
|
|
106
|
+
if (age === 'oldest') {
|
|
107
|
+
// --follow is necessary to follow file renames
|
|
108
|
+
// --diff-filter=A ensures we only get the commit which (A)dded the file
|
|
109
|
+
extraArgs += ' --follow --diff-filter=A';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const result = shell.exec(
|
|
113
|
+
`git log ${extraArgs} ${formatArg} -- "${path.basename(file)}"`,
|
|
114
|
+
{
|
|
115
|
+
// cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
|
|
116
|
+
cwd: path.dirname(file),
|
|
117
|
+
silent: true,
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
if (result.code !== 0) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`Failed to retrieve the git history for file "${file}" with exit code ${result.code}: ${result.stderr}`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
let regex = /^(?<timestamp>\d+)$/;
|
|
126
|
+
if (includeAuthor) {
|
|
127
|
+
regex = /^(?<timestamp>\d+),(?<author>.+)$/;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const output = result.stdout.trim();
|
|
131
|
+
|
|
132
|
+
if (!output) {
|
|
133
|
+
throw new FileNotTrackedError(
|
|
134
|
+
`Failed to retrieve the git history for file "${file}" because the file is not tracked by git.`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const match = output.match(regex);
|
|
139
|
+
|
|
140
|
+
if (!match) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
`Failed to retrieve the git history for file "${file}" with unexpected output: ${output}`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const timestamp = Number(match.groups!.timestamp);
|
|
147
|
+
const date = new Date(timestamp * 1000);
|
|
148
|
+
|
|
149
|
+
if (includeAuthor) {
|
|
150
|
+
return {date, timestamp, author: match.groups!.author!};
|
|
151
|
+
}
|
|
152
|
+
return {date, timestamp};
|
|
153
|
+
}
|
package/src/globUtils.ts
ADDED
|
@@ -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
|
+
// Globby/Micromatch are the 2 libs we use in Docusaurus consistently
|
|
9
|
+
|
|
10
|
+
import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby
|
|
11
|
+
import path from 'path';
|
|
12
|
+
|
|
13
|
+
/** A re-export of the globby instance. */
|
|
14
|
+
export {default as Globby} from 'globby';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The default glob patterns we ignore when sourcing content.
|
|
18
|
+
* - Ignore files and folders starting with `_` recursively
|
|
19
|
+
* - Ignore tests
|
|
20
|
+
*/
|
|
21
|
+
export const GlobExcludeDefault = [
|
|
22
|
+
'**/_*.{js,jsx,ts,tsx,md,mdx}',
|
|
23
|
+
'**/_*/**',
|
|
24
|
+
'**/*.test.{js,jsx,ts,tsx}',
|
|
25
|
+
'**/__tests__/**',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
type Matcher = (str: string) => boolean;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A very thin wrapper around `Micromatch.makeRe`.
|
|
32
|
+
*
|
|
33
|
+
* @see {@link createAbsoluteFilePathMatcher}
|
|
34
|
+
* @param patterns A list of glob patterns.
|
|
35
|
+
* @returns A matcher handle that tells if a file path is matched by any of the
|
|
36
|
+
* patterns.
|
|
37
|
+
*/
|
|
38
|
+
export function createMatcher(patterns: string[]): Matcher {
|
|
39
|
+
const regexp = new RegExp(
|
|
40
|
+
patterns.map((pattern) => Micromatch.makeRe(pattern).source).join('|'),
|
|
41
|
+
);
|
|
42
|
+
return (str) => regexp.test(str);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* We use match patterns like `"** /_* /**"` (ignore the spaces), where `"_*"`
|
|
47
|
+
* should only be matched within a subfolder. This function would:
|
|
48
|
+
* - Match `/user/sebastien/website/docs/_partials/xyz.md`
|
|
49
|
+
* - Ignore `/user/_sebastien/website/docs/partials/xyz.md`
|
|
50
|
+
*
|
|
51
|
+
* @param patterns A list of glob patterns.
|
|
52
|
+
* @param rootFolders A list of root folders to resolve the glob from.
|
|
53
|
+
* @returns A matcher handle that tells if a file path is matched by any of the
|
|
54
|
+
* patterns, resolved from the first root folder that contains the path.
|
|
55
|
+
* @throws Throws when the returned matcher receives a path that doesn't belong
|
|
56
|
+
* to any of the `rootFolders`.
|
|
57
|
+
*/
|
|
58
|
+
export function createAbsoluteFilePathMatcher(
|
|
59
|
+
patterns: string[],
|
|
60
|
+
rootFolders: string[],
|
|
61
|
+
): Matcher {
|
|
62
|
+
const matcher = createMatcher(patterns);
|
|
63
|
+
|
|
64
|
+
function getRelativeFilePath(absoluteFilePath: string) {
|
|
65
|
+
const rootFolder = rootFolders.find((folderPath) =>
|
|
66
|
+
absoluteFilePath.startsWith(folderPath),
|
|
67
|
+
);
|
|
68
|
+
if (!rootFolder) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`createAbsoluteFilePathMatcher unexpected error, absoluteFilePath=${absoluteFilePath} was not contained in any of the root folders: ${rootFolders.join(
|
|
71
|
+
', ',
|
|
72
|
+
)}`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return path.relative(rootFolder, absoluteFilePath);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (absoluteFilePath: string) =>
|
|
79
|
+
matcher(getRelativeFilePath(absoluteFilePath));
|
|
80
|
+
}
|
package/src/hashUtils.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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 {createHash} from 'crypto';
|
|
9
|
+
import _ from 'lodash';
|
|
10
|
+
import {shortName, isNameTooLong} from './pathUtils';
|
|
11
|
+
|
|
12
|
+
/** Thin wrapper around `crypto.createHash("md5")`. */
|
|
13
|
+
export function md5Hash(str: string): string {
|
|
14
|
+
return createHash('md5').update(str).digest('hex');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Creates an MD5 hash and truncates it to the given length. */
|
|
18
|
+
export function simpleHash(str: string, length: number): string {
|
|
19
|
+
return md5Hash(str).substring(0, length);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
23
|
+
/**
|
|
24
|
+
* Given an input string, convert to kebab-case and append a hash, avoiding name
|
|
25
|
+
* collision. Also removes part of the string if its larger than the allowed
|
|
26
|
+
* filename per OS, avoiding `ERRNAMETOOLONG` error.
|
|
27
|
+
*/
|
|
28
|
+
export function docuHash(str: string): string {
|
|
29
|
+
if (str === '/') {
|
|
30
|
+
return 'index';
|
|
31
|
+
}
|
|
32
|
+
const shortHash = simpleHash(str, 3);
|
|
33
|
+
const parsedPath = `${_.kebabCase(str)}-${shortHash}`;
|
|
34
|
+
if (isNameTooLong(parsedPath)) {
|
|
35
|
+
return `${shortName(_.kebabCase(str))}-${shortHash}`;
|
|
36
|
+
}
|
|
37
|
+
return parsedPath;
|
|
38
|
+
}
|
package/src/i18nUtils.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
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 path from 'path';
|
|
9
|
+
import _ from 'lodash';
|
|
10
|
+
import type {TranslationFileContent, TranslationFile} from '@docusaurus/types';
|
|
11
|
+
import {DEFAULT_PLUGIN_ID, I18N_DIR_NAME} from './constants';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Takes a list of translation file contents, and shallow-merges them into one.
|
|
15
|
+
*/
|
|
16
|
+
export function mergeTranslations(
|
|
17
|
+
contents: TranslationFileContent[],
|
|
18
|
+
): TranslationFileContent {
|
|
19
|
+
return contents.reduce((acc, content) => ({...acc, ...content}), {});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Useful to update all the messages of a translation file. Used in tests to
|
|
24
|
+
* simulate translations.
|
|
25
|
+
*/
|
|
26
|
+
export function updateTranslationFileMessages(
|
|
27
|
+
translationFile: TranslationFile,
|
|
28
|
+
updateMessage: (message: string) => string,
|
|
29
|
+
): TranslationFile {
|
|
30
|
+
return {
|
|
31
|
+
...translationFile,
|
|
32
|
+
content: _.mapValues(translationFile.content, (translation) => ({
|
|
33
|
+
...translation,
|
|
34
|
+
message: updateMessage(translation.message),
|
|
35
|
+
})),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Takes everything needed and constructs a plugin i18n path. Plugins should
|
|
41
|
+
* expect everything it needs for translations to be found under this path.
|
|
42
|
+
*/
|
|
43
|
+
export function getPluginI18nPath({
|
|
44
|
+
siteDir,
|
|
45
|
+
locale,
|
|
46
|
+
pluginName,
|
|
47
|
+
pluginId = DEFAULT_PLUGIN_ID,
|
|
48
|
+
subPaths = [],
|
|
49
|
+
}: {
|
|
50
|
+
siteDir: string;
|
|
51
|
+
locale: string;
|
|
52
|
+
pluginName: string;
|
|
53
|
+
pluginId?: string | undefined;
|
|
54
|
+
subPaths?: string[];
|
|
55
|
+
}): string {
|
|
56
|
+
return path.join(
|
|
57
|
+
siteDir,
|
|
58
|
+
I18N_DIR_NAME,
|
|
59
|
+
// namespace first by locale: convenient to work in a single folder for a
|
|
60
|
+
// translator
|
|
61
|
+
locale,
|
|
62
|
+
// Make it convenient to use for single-instance
|
|
63
|
+
// ie: return "docs", not "docs-default" nor "docs/default"
|
|
64
|
+
`${pluginName}${pluginId === DEFAULT_PLUGIN_ID ? '' : `-${pluginId}`}`,
|
|
65
|
+
...subPaths,
|
|
66
|
+
);
|
|
67
|
+
}
|