Better search for attention mode

This commit is contained in:
2026-06-23 09:21:39 +08:00
parent 04c5e9e637
commit cce5e1f2cc
2 changed files with 50 additions and 13 deletions

View File

@@ -207,19 +207,27 @@ export class HighlightedMarkdown extends Component {
['nickname', NICKNAME_RE], ['nickname', NICKNAME_RE],
]; ];
if (props.search_param) { if (props.search_param) {
let search_kws = props.search_param let search_re = null;
.split(/[\s()+-]+/) if (props.search_param.match(/\/.+\//)) {
.filter((s) => s && s !== 'AND' && s !== 'OR'); try {
if (search_kws.length) { search_re = new RegExp(
rules.push([ `(${props.search_param.slice(1, -1)})`,
'search', 'g',
new RegExp( );
} catch (e) {}
} else {
let search_kws = props.search_param.split(/[\s()+-]+/);
if (search_kws.length) {
search_re = new RegExp(
`(${search_kws `(${search_kws
.map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) .map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
.join('|')})`, .join('|')})`,
'g', 'g',
), );
]); }
}
if (search_re) {
rules.push(['search', search_re]);
} }
} }
const splitted = split_text(originalText, rules); const splitted = split_text(originalText, rules);

View File

@@ -33,6 +33,12 @@ window.LATEST_POST_ID = parseInt(localStorage['_LATEST_POST_ID'], 10) || 0;
const DZ_NAME = '洞主'; const DZ_NAME = '洞主';
function get_search_text(post) {
return [post.text]
.concat((post.comments || []).map((comment) => comment.text))
.join('\n');
}
function check_block(info) { function check_block(info) {
return ( return (
(((window.config.block_tmp || !window.config.show_all_rooms) && (((window.config.block_tmp || !window.config.show_all_rooms) &&
@@ -1666,10 +1672,12 @@ class SubFlow extends PureComponent {
? json.data.filter((post) => { ? json.data.filter((post) => {
return this.state.search_param return this.state.search_param
.split(' ') .split(' ')
.every((keyword) => post.text.includes(keyword)); .every((keyword) =>
get_search_text(post).includes(keyword),
);
}) // Not using regex }) // Not using regex
: json.data.filter( : json.data.filter(
(post) => !!post.text.match(regex_search), (post) => !!get_search_text(post).match(regex_search),
), // Using regex ), // Using regex
}, },
mode: 'attention_finished', mode: 'attention_finished',
@@ -1707,8 +1715,29 @@ class SubFlow extends PureComponent {
}); });
this.setState((prev, props) => ({ this.setState((prev, props) => ({
chunks: { chunks: {
title: 'Attention List: Local', title: `${
data: prev.chunks.data.concat(json.data), use_search
? use_regex
? `Result for RegEx ${regex_search.toString()} in `
: `Result for "${this.state.search_param}" in `
: ''
}Attention List: Local`,
data: prev.chunks.data.concat(
!use_search
? json.data
: !use_regex
? json.data.filter((post) => {
return this.state.search_param
.split(' ')
.every((keyword) =>
get_search_text(post).includes(keyword),
);
})
: json.data.filter(
(post) =>
!!get_search_text(post).match(regex_search),
),
),
}, },
loading_status: 'done', loading_status: 'done',
})); }));