Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] #381 Also consider crawl filter as a regexp #382

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage/*
logs/*
node_modules/*
*.map
/.idea/**
24 changes: 22 additions & 2 deletions js/crawl/crawl.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ const getProxyURLSetup = (url, origin) => {
};
};

/**
* Test whether the given path matches the provided filter.
* @param path The path of the website page.
* @param filter The filter to test the path against, using 'startsWith' and RegExp's test.
* @returns {boolean}
*/
const pathnameMatchesFilter = (path, filter) => {
try {
if (!filter || filter.length === 0 || path.startsWith(filter)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startsWith is most likely overkill, but I wanted no doubt that the current functionality would not change.

return true;
}
return new RegExp(filter).test(path);
} catch (e) {
// eslint-disable-next-line no-console
console.error(`Could not test path ${path} with provided filter: ${filter}`, e);
alert.error(`Could not test path with provided filter: ${filter}`);
return false;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the error be re-thrown? Should this error be added to an import report?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alert is fine.

}
};

const getContentFrame = () => document.querySelector(`${PARENT_SELECTOR} iframe`);

const attachListeners = () => {
Expand Down Expand Up @@ -193,7 +213,7 @@ const attachListeners = () => {
if (!crawlStatus.urls.includes(found)
&& !urlsArray.includes(found)
&& current !== found
&& u.pathname.startsWith(config.fields['crawl-filter-pathname'])) {
&& pathnameMatchesFilter(u.pathname, config.fields['crawl-filter-pathname'])) {
urlsArray.push(found);
linksToFollow.push(found);
} else {
Expand Down Expand Up @@ -354,7 +374,7 @@ const attachListeners = () => {
sitemap: config.fields['crawl-sitemap-file'],
})).filter((url) => {
const u = new URL(url);
return u.pathname.startsWith(config.fields['crawl-filter-pathname']);
return pathnameMatchesFilter(u.pathname, config.fields['crawl-filter-pathname']);
});

crawlStatus.crawled = crawlStatus.urls.length;
Expand Down