mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-24 05:28:01 +02:00
refactor: install eslint-plugin-regexp (#6906)
* refactor: install eslint-plugin-regexp * simplify * simplify * fix
This commit is contained in:
parent
127183e70e
commit
3a4b9b4c30
23 changed files with 145 additions and 83 deletions
|
@ -8,43 +8,33 @@
|
|||
import type {NumberPrefixParser} from '@docusaurus/plugin-content-docs';
|
||||
|
||||
// Best-effort to avoid parsing some patterns as number prefix
|
||||
const IgnoredPrefixPatterns = (() => {
|
||||
// ignore common date-like patterns: https://github.com/facebook/docusaurus/issues/4640
|
||||
const DateLikePrefixRegex =
|
||||
/^(?:\d{2}|\d{4})[-_.]\d{2}(?:[-_.](?:\d{2}|\d{4}))?.*$/;
|
||||
// ignore common date-like patterns: https://github.com/facebook/docusaurus/issues/4640
|
||||
// ignore common versioning patterns: https://github.com/facebook/docusaurus/issues/4653
|
||||
// Both of them would look like 7.0-foo or 2021-11-foo
|
||||
// note: we could try to parse float numbers in filenames, but that is probably
|
||||
// not worth it, as a version such as "8.0" can be interpreted as either a
|
||||
// version or a float. User can configure her own NumberPrefixParser if she
|
||||
// wants 8.0 to be interpreted as a float
|
||||
const ignoredPrefixPattern = /^\d+[-_.]\d+/;
|
||||
|
||||
// ignore common versioning patterns: https://github.com/facebook/docusaurus/issues/4653
|
||||
// note: we could try to parse float numbers in filenames but that is
|
||||
// probably not worth it as a version such as "8.0" can be interpreted as both
|
||||
// a version and a float. User can configure her own NumberPrefixParser if
|
||||
// she wants 8.0 to be interpreted as a float
|
||||
const VersionLikePrefixRegex = /^\d+[-_.]\d+.*$/;
|
||||
|
||||
return new RegExp(
|
||||
`${DateLikePrefixRegex.source}|${VersionLikePrefixRegex.source}`,
|
||||
);
|
||||
})();
|
||||
|
||||
const NumberPrefixRegex =
|
||||
/^(?<numberPrefix>\d+)(?<separator>\s*[-_.]+\s*)(?<suffix>.*)$/;
|
||||
const numberPrefixPattern =
|
||||
/^(?<numberPrefix>\d+)\s*[-_.]+\s*(?<suffix>[^-_.\s].*)$/;
|
||||
|
||||
// 0-myDoc => {filename: myDoc, numberPrefix: 0}
|
||||
// 003 - myDoc => {filename: myDoc, numberPrefix: 3}
|
||||
export const DefaultNumberPrefixParser: NumberPrefixParser = (
|
||||
filename: string,
|
||||
) => {
|
||||
if (IgnoredPrefixPatterns.exec(filename)) {
|
||||
if (ignoredPrefixPattern.test(filename)) {
|
||||
return {filename, numberPrefix: undefined};
|
||||
}
|
||||
const match = numberPrefixPattern.exec(filename);
|
||||
if (!match) {
|
||||
return {filename, numberPrefix: undefined};
|
||||
}
|
||||
const match = NumberPrefixRegex.exec(filename);
|
||||
const cleanFileName = match?.groups?.suffix ?? filename;
|
||||
const numberPrefixString = match?.groups?.numberPrefix;
|
||||
const numberPrefix = numberPrefixString
|
||||
? parseInt(numberPrefixString, 10)
|
||||
: undefined;
|
||||
return {
|
||||
filename: cleanFileName,
|
||||
numberPrefix,
|
||||
filename: match.groups!.suffix!,
|
||||
numberPrefix: parseInt(match.groups!.numberPrefix!, 10),
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue