refactor: enforce named capture groups; clean up regexes (#6524)

* refactor: enforce named capture groups; clean up regexes

* fixes

* fix
This commit is contained in:
Joshua Chen 2022-02-01 17:43:15 +08:00 committed by GitHub
parent c56e6194b4
commit 1cefb643dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 80 additions and 77 deletions

View file

@ -116,7 +116,7 @@ describe('createExcerpt', () => {
export function ItemCol(props) { return <Item {...props} className={'col col--6 margin-bottom--lg'}/> };
Lorem **ipsum** dolor sit \`amet\`[^1], consectetur _adipiscing_ elit. [**Vestibulum**](https://wiktionary.org/wiki/vestibulum) ex urna[^bignote], ~molestie~ et sagittis ut, varius ac justo :wink:.
Lorem **ipsum** dolor sit \`amet\`[^1], consectetur _adipiscing_ elit. [**Vestibulum**](https://wiktionary.org/wiki/vestibulum) ex urna[^bignote], ~~molestie~~ et sagittis ut, varius ac justo :wink:.
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis.
`),

View file

@ -69,8 +69,8 @@ export async function generate(
}
}
const indexRE = /(^|.*\/)index\.(md|mdx|js|jsx|ts|tsx)$/i;
const extRE = /\.(md|mdx|js|jsx|ts|tsx)$/;
const indexRE = /(?<dirname>^|.*\/)index\.(?:mdx?|jsx?|tsx?)$/i;
const extRE = /\.(?:mdx?|jsx?|tsx?)$/;
/**
* Convert filepath to url path.

View file

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

View file

@ -14,12 +14,12 @@ export function parseMarkdownHeadingId(heading: string): {
text: string;
id?: string;
} {
const customHeadingIdRegex = /^(.*?)\s*\{#([\w-]+)\}$/;
const customHeadingIdRegex = /^(?<text>.*?)\s*\{#(?<id>[\w-]+)\}$/;
const matches = customHeadingIdRegex.exec(heading);
if (matches) {
return {
text: matches[1],
id: matches[2],
text: matches.groups!.text,
id: matches.groups!.id,
};
}
return {text: heading, id: undefined};
@ -46,7 +46,7 @@ export function createExcerpt(fileString: string): string | undefined {
}
// Skip import/export declaration.
if (/^\s*?import\s.*(from.*)?;?|export\s.*{.*};?/.test(fileLine)) {
if (/^(?:import|export)\s.*/.test(fileLine)) {
continue;
}
@ -71,25 +71,27 @@ export function createExcerpt(fileString: string): string | undefined {
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
.replace(/^#\s*([^#]*)\s*#?/gm, '')
.replace(/^#\s*[^#]*\s*#?/gm, '')
// Remove Markdown + ATX-style headers
.replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
// Remove emphasis and strikethroughs.
.replace(/([*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
.replace(/^#{1,6}\s*(?<text>[^#]*)\s*(?:#{1,6})?/gm, '$1')
// Remove emphasis.
.replace(/(?<opening>[*_]{1,3})(?<text>.*?)\1/g, '$2')
// Remove strikethroughs.
.replace(/~~(?<text>\S.*\S)~~/g, '$1')
// Remove images.
.replace(/!\[(.*?)\][[(].*?[\])]/g, '$1')
.replace(/!\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove footnotes.
.replace(/\[\^.+?\](: .*?$)?/g, '')
.replace(/\[\^.+?\](?:: .*?$)?/g, '')
// Remove inline links.
.replace(/\[(.*?)\][[(].*?[\])]/g, '$1')
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove inline code.
.replace(/`(.+?)`/g, '$1')
.replace(/`(?<text>.+?)`/g, '$1')
// Remove blockquotes.
.replace(/^\s{0,3}>\s?/g, '')
// Remove admonition definition.
.replace(/(:{3}.*)/, '')
.replace(/:::.*/, '')
// Remove Emoji names within colons include preceding whitespace.
.replace(/\s?(:(::|[^:\n])+:)/g, '')
.replace(/\s?:(?:::|[^:\n])+:/g, '')
// Remove custom Markdown heading id.
.replace(/{#*[\w-]+}/, '')
.trim();
@ -134,9 +136,9 @@ export function parseMarkdownContentTitle(
const content = contentUntrimmed.trim();
const IMPORT_STATEMENT =
/import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/.source;
/import\s+(?:[\w*{}\s\n,]+from\s+)?["'\s][@\w/_.-]+["'\s];?|\n/.source;
const REGULAR_TITLE =
/(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
/(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>(?:{#*[\w-]+})|#)?\n*?)/
.source;
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;

View file

@ -27,7 +27,7 @@ export function normalizeUrl(rawUrls: string[]): string {
// There must be two or three slashes in the file protocol,
// two slashes in anything else.
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
urls[0] = urls[0].replace(/^(?<protocol>[^/:]+):\/*/, replacement);
for (let i = 0; i < urls.length; i += 1) {
let component = urls[i];
@ -70,14 +70,14 @@ export function normalizeUrl(rawUrls: string[]): string {
// except the possible first plain protocol part.
// Remove trailing slash before parameters or hash.
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
str = str.replace(/\/(?<search>\?|&|#[^!])/g, '$1');
// Replace ? in parameters with &.
const parts = str.split('?');
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
str = str.replace(/([^:/]\/)\/+/g, '$1');
str = str.replace(/(?<textBefore>[^:/]\/)\/+/g, '$1');
// Dedupe forward slashes at the beginning of the path.
str = str.replace(/^\/+/g, '/');

View file

@ -79,12 +79,12 @@ export function getFileLoaderUtils(): FileLoaderUtils {
*/
images: () => ({
use: [loaders.url({folder: 'images'})],
test: /\.(ico|jpg|jpeg|png|gif|webp)(\?.*)?$/,
test: /\.(?:ico|jpe?g|png|gif|webp)(?:\?.*)?$/i,
}),
fonts: () => ({
use: [loaders.url({folder: 'fonts'})],
test: /\.(woff|woff2|eot|ttf|otf)$/,
test: /\.(?:woff2?|eot|ttf|otf)$/i,
}),
/**
@ -93,11 +93,11 @@ export function getFileLoaderUtils(): FileLoaderUtils {
*/
media: () => ({
use: [loaders.url({folder: 'medias'})],
test: /\.(mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/,
test: /\.(?:mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/i,
}),
svg: () => ({
test: /\.svg?$/,
test: /\.svg$/i,
oneOf: [
{
use: [
@ -126,7 +126,7 @@ export function getFileLoaderUtils(): FileLoaderUtils {
// We don't want to use SVGR loader for non-React source code
// ie we don't want to use SVGR for CSS files...
issuer: {
and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
and: [/\.(?:tsx?|jsx?|mdx?)$/i],
},
},
{
@ -137,7 +137,7 @@ export function getFileLoaderUtils(): FileLoaderUtils {
otherAssets: () => ({
use: [loaders.file({folder: 'files'})],
test: /\.(pdf|doc|docx|xls|xlsx|zip|rar)$/,
test: /\.(?:pdf|docx?|xlsx?|zip|rar)$/i,
}),
};