refactor: install eslint-plugin-regexp (#6906)

* refactor: install eslint-plugin-regexp

* simplify

* simplify

* fix
This commit is contained in:
Joshua Chen 2022-03-13 18:32:17 +08:00 committed by GitHub
parent 127183e70e
commit 3a4b9b4c30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 145 additions and 83 deletions

View file

@ -169,14 +169,16 @@ describe('reportMessage', () => {
`"Unexpected \\"reportingSeverity\\" value: foo."`,
);
expect(consoleLog).toBeCalledTimes(1);
expect(consoleLog).toBeCalledWith(expect.stringMatching(/.*\[INFO].* hey/));
expect(consoleLog).toBeCalledWith(
expect.stringMatching(/.*\[INFO\].* hey/),
);
expect(consoleWarn).toBeCalledTimes(1);
expect(consoleWarn).toBeCalledWith(
expect.stringMatching(/.*\[WARNING].* hey/),
expect.stringMatching(/.*\[WARNING\].* hey/),
);
expect(consoleError).toBeCalledTimes(1);
expect(consoleError).toBeCalledWith(
expect.stringMatching(/.*\[ERROR].* hey/),
expect.stringMatching(/.*\[ERROR\].* hey/),
);
});
});

View file

@ -404,7 +404,7 @@ Lorem Ipsum
expect(
parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
).toEqual({
content: markdown.trim().replace('# Markdown Title', ''),
content: markdown.trim().replace('# Markdown Title\n', ''),
contentTitle: 'Markdown Title',
});
});
@ -444,7 +444,7 @@ Lorem Ipsum
expect(
parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
).toEqual({
content: markdown.replace('Markdown Title\n==============\n\n', ''),
content: markdown.replace('Markdown Title\n==============\n', ''),
contentTitle: 'Markdown Title',
});
});
@ -547,7 +547,7 @@ Lorem Ipsum
expect(
parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
).toEqual({
content: markdown.replace('# Markdown Title', ''),
content: markdown.replace('# Markdown Title\n', ''),
contentTitle: 'Markdown Title',
});
});

View file

@ -68,7 +68,7 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
// ink
// [doc1]: doc1.md -> we replace this doc1.md with correct link
const mdRegex =
/(?:(?:\]\()|(?:\]:\s*))(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
let mdMatch = mdRegex.exec(modifiedLine);
while (mdMatch !== null) {
// Replace it to correct html link.

View file

@ -14,11 +14,11 @@ export function parseMarkdownHeadingId(heading: string): {
text: string;
id?: string;
} {
const customHeadingIdRegex = /^(?<text>.*?)\s*\{#(?<id>[\w-]+)\}$/;
const customHeadingIdRegex = /\s*\{#(?<id>[\w-]+)\}$/;
const matches = customHeadingIdRegex.exec(heading);
if (matches) {
return {
text: matches.groups!.text!,
text: heading.replace(matches[0]!, ''),
id: matches.groups!.id!,
};
}
@ -69,9 +69,9 @@ export function createExcerpt(fileString: string): string | undefined {
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
.replace(/^#\s*[^#]*\s*#?/gm, '')
.replace(/^#[^#]+#?/gm, '')
// Remove Markdown + ATX-style headers
.replace(/^#{1,6}\s*(?<text>[^#]*)\s*(?:#{1,6})?/gm, '$1')
.replace(/^#{1,6}\s*(?<text>[^#]*)\s*#{0,6}/gm, '$1')
// Remove emphasis.
.replace(/(?<opening>[*_]{1,3})(?<text>.*?)\1/g, '$2')
// Remove strikethroughs.
@ -79,7 +79,7 @@ export function createExcerpt(fileString: string): string | undefined {
// Remove images.
.replace(/!\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove footnotes.
.replace(/\[\^.+?\](?:: .*?$)?/g, '')
.replace(/\[\^.+?\](?:: .*$)?/g, '')
// Remove inline links.
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove inline code.
@ -91,7 +91,7 @@ export function createExcerpt(fileString: string): string | undefined {
// Remove Emoji names within colons include preceding whitespace.
.replace(/\s?:(?:::|[^:\n])+:/g, '')
// Remove custom Markdown heading id.
.replace(/{#*[\w-]+}/, '')
.replace(/\{#*[\w-]+\}/, '')
.trim();
if (cleanedLine) {
@ -132,35 +132,42 @@ export function parseMarkdownContentTitle(
const removeContentTitleOption = options?.removeContentTitle ?? false;
const content = contentUntrimmed.trim();
// We only need to detect import statements that will be parsed by MDX as
// `import` nodes, as broken syntax can't render anyways. That means any block
// that has `import` at the very beginning and surrounded by empty lines.
const contentWithoutImport = content
.replace(/^(?:import\s(?:.|\n(?!\n))*\n{2,})*/, '')
.trim();
const IMPORT_STATEMENT =
/import\s+(?:[\w*{}\s\n,]+from\s+)?["'\s][@\w/_.-]+["'\s];?|\n/.source;
const REGULAR_TITLE =
/(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>(?:{#*[\w-]+})|#)?\n*?)/
.source;
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
const regularTitleMatch = new RegExp(
`^(?:${IMPORT_STATEMENT})*?${REGULAR_TITLE}`,
'g',
).exec(content);
const alternateTitleMatch = new RegExp(
`^(?:${IMPORT_STATEMENT})*?${ALTERNATE_TITLE}`,
'g',
).exec(content);
const regularTitleMatch = /^#[ \t]+(?<title>[^ \t].*)(?:\n|$)/.exec(
contentWithoutImport,
);
const alternateTitleMatch = /^(?<title>.*)\n=+(?:\n|$)/.exec(
contentWithoutImport,
);
const titleMatch = regularTitleMatch ?? alternateTitleMatch;
const {pattern, title} = titleMatch?.groups ?? {};
if (!pattern || !title) {
if (!titleMatch) {
return {content, contentTitle: undefined};
}
const newContent = removeContentTitleOption
? content.replace(pattern, '')
? content.replace(titleMatch[0]!, '')
: content;
if (regularTitleMatch) {
return {
content: newContent.trim(),
contentTitle: toTextContentTitle(
regularTitleMatch
.groups!.title!.trim()
.replace(/\s*(?:\{#*[\w-]+\}|#+)$/, ''),
).trim(),
};
}
return {
content: newContent.trim(),
contentTitle: toTextContentTitle(title.trim()).trim(),
contentTitle: toTextContentTitle(
alternateTitleMatch!.groups!.title!.trim().replace(/\s*=+$/, ''),
).trim(),
};
}

View file

@ -58,7 +58,7 @@ export function normalizeUrl(rawUrls: string[]): string {
if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(
/^[/]+/,
/^\/+/,
// Special case where the first element of rawUrls is empty
// ["", "/hello"] => /hello
component[0] === '/' && !hasStartingSlash ? '/' : '',
@ -68,7 +68,7 @@ export function normalizeUrl(rawUrls: string[]): string {
hasEndingSlash = component[component.length - 1] === '/';
// Removing the ending slashes for each component but the last. For the
// last component we will combine multiple slashes to a single one.
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
component = component.replace(/\/+$/, i < urls.length - 1 ? '' : '/');
}
hasStartingSlash = true;