Skip to content

Commit

Permalink
feat: add regexp support to filter
Browse files Browse the repository at this point in the history
- also add filter to author
  • Loading branch information
enpitsuLin committed Mar 30, 2023
1 parent d86ea6f commit 36f4fe6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/SettingsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<p class="text-sm font-medium">{{ t('filter') }}</p>
<p class="text-sm text-$ud-text-secondary text-xs">
{{ t('filter-desc') }}
<span class="underline cursor-help" :title="t('filter-tips')">(?)</span>
</p>
</div>
<TagsInput v-model:tags="settings.filter" />
Expand Down
7 changes: 6 additions & 1 deletion src/composables/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AfterFetchContext, MaybeRef, UseFetchOptions } from '@vueuse/core';
import psl from 'psl';
import { getTypedFilter } from '../utils/filter';

export type GreasyforkScriptUser = {
id: number;
Expand Down Expand Up @@ -97,7 +98,11 @@ export function useDataList() {
settings.value.nsfw ? sleazyfork.value ?? [] : []
) ?? []
).filter((item) =>
settings.value.filter.every((keywords) => !item.name.includes(keywords))
settings.value.filter.every((keywords) => {
const filter = getTypedFilter(keywords);
if (filter.type === 'title') return !filter.regexp.test(item.name);
else return item.users.every((user) => !filter.regexp.test(user.name));
})
);
});

Expand Down
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"nsfw": "NSFW",
"nsfw-desc": "Show Sleazyfork's result in list",
"filter": "Filter",
"filter-desc": "Filter user script with specific content in title",
"filter-desc": "Filter user script with specific content in title or specific developer",
"filter-tips": "use title: to filter title or author: for developer, title for default",
"time-ago": {
"just-now": "just now",
"past": "{n} ago",
Expand Down
3 changes: 2 additions & 1 deletion src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"nsfw": "成人内容",
"nsfw-desc": "列表中展示Sleazyfork的搜索结果",
"filter": "过滤",
"filter-desc": "过滤标题含有指定内容的用户脚本",
"filter-desc": "过滤标题含有指定内容或指定开发者的用户脚本",
"filter-tips": "使用title:过滤标题或是author:指定开发者,没有相关描述则默认过滤标题,支持正则表达式",
"time-ago": {
"just-now": "就在刚刚",
"past": "{n}前",
Expand Down
29 changes: 29 additions & 0 deletions src/utils/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type AuthorFilterLiteral = `author:${string}`;
type TitleFilterLiteral = `title:${string}`;
type Filter = { type: 'author' | 'title'; regexp: RegExp };

const AUTHOR_REGEX = /^author:(\S+)$/;
const TITLE_REGEX = /^title:(\S+)$/;

export function isAuthorFilter(filter: string): filter is AuthorFilterLiteral {
return !!filter.match(AUTHOR_REGEX);
}

export function isTitleFilter(filter: string): filter is TitleFilterLiteral {
return !!filter.match(TITLE_REGEX);
}

export function getTypedFilter(filter: string): Filter {
if (filter.includes(':')) {
if (isAuthorFilter(filter)) {
const regexp = new RegExp(filter.match(AUTHOR_REGEX)?.[1]!);
return { type: 'author', regexp };
} else {
return getTypedFilter(filter.match(TITLE_REGEX)?.[1]!);
}
}
return {
type: 'title',
regexp: new RegExp(filter)
};
}

0 comments on commit 36f4fe6

Please sign in to comment.