mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-19 01:28:38 +02:00
refactor(utils): reorganize functions; move authors file resolution to utils (#6229)
* refactor(utils): reorganize functions; move authors file resolution to utils * More refactor
This commit is contained in:
parent
7adc1c0cdb
commit
24d65d9bdd
39 changed files with 533 additions and 747 deletions
|
@ -7,6 +7,8 @@
|
|||
|
||||
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
||||
|
||||
import path from 'path';
|
||||
|
||||
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
|
||||
const MAX_PATH_SEGMENT_CHARS = 255;
|
||||
const MAX_PATH_SEGMENT_BYTES = 255;
|
||||
|
@ -39,3 +41,66 @@ export const shortName = (str: string): string => {
|
|||
)
|
||||
.toString();
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert Windows backslash paths to posix style paths.
|
||||
* E.g: endi\lie -> endi/lie
|
||||
*
|
||||
* Returns original path if the posix counterpart is not valid Windows path.
|
||||
* This makes the legacy code that uses posixPath safe; but also makes it less
|
||||
* useful when you actually want a path with forward slashes (e.g. for URL)
|
||||
*
|
||||
* Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
|
||||
*/
|
||||
export function posixPath(str: string): string {
|
||||
const isExtendedLengthPath = /^\\\\\?\\/.test(str);
|
||||
|
||||
// Forward slashes are only valid Windows paths when they don't contain non-ascii characters.
|
||||
// eslint-disable-next-line no-control-regex
|
||||
const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
|
||||
|
||||
if (isExtendedLengthPath || hasNonAscii) {
|
||||
return str;
|
||||
}
|
||||
return str.replace(/\\/g, '/');
|
||||
}
|
||||
|
||||
// When you want to display a path in a message/warning/error,
|
||||
// it's more convenient to:
|
||||
// - make it relative to cwd()
|
||||
// - convert to posix (ie not using windows \ path separator)
|
||||
// This way, Jest tests can run more reliably on any computer/CI
|
||||
// on both Unix/Windows
|
||||
// For Windows users this is not perfect (as they see / instead of \) but it's probably good enough
|
||||
export function toMessageRelativeFilePath(filePath: string): string {
|
||||
return posixPath(path.relative(process.cwd(), filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias filepath relative to site directory, very useful so that we
|
||||
* don't expose user's site structure.
|
||||
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
||||
*/
|
||||
export function aliasedSitePath(filePath: string, siteDir: string): string {
|
||||
const relativePath = posixPath(path.relative(siteDir, filePath));
|
||||
// Cannot use path.join() as it resolves '../' and removes
|
||||
// the '@site'. Let webpack loader resolve it.
|
||||
return `@site/${relativePath}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* When you have a path like C:\X\Y
|
||||
* It is not safe to use directly when generating code
|
||||
* For example, this would fail due to unescaped \: `<img src={require('${filePath}')} />`
|
||||
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
||||
*
|
||||
* posixPath can't be used in all cases, because forward slashes are only valid
|
||||
* Windows paths when they don't contain non-ascii characters, and posixPath
|
||||
* doesn't escape those that fail to be converted.
|
||||
*/
|
||||
export function escapePath(str: string): string {
|
||||
const escaped = JSON.stringify(str);
|
||||
|
||||
// Remove the " around the json string;
|
||||
return escaped.substring(1, escaped.length - 1);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue