@@ -4,19 +4,21 @@ import { checkIsSearchUser, checkIsUnderFilter } from '../utils/checkPrefix';
44import { titleTextExclusionFilter } from '../utils/configs' ;
55import { debounced } from '../utils/debounce' ;
66import { filterTiddlersAsync } from '../utils/filterTiddlersAsync' ;
7- import { getFieldsAsText } from '../utils/getFieldsAsTitle' ;
7+ import { getFieldsAsText , getFieldsAsTitle } from '../utils/getFieldsAsTitle' ;
88import { lingo } from '../utils/lingo' ;
99
1010export const plugin = {
1111 async getSources ( parameters ) {
1212 if ( parameters . query . length === 0 ) return [ ] ;
1313 if ( ! checkIsSearchUser ( parameters ) || checkIsUnderFilter ( parameters ) ) return [ ] ;
14+ const { textFields, fieldsAsText } = getFieldsAsText ( ) ;
15+ const { titleFields } = getFieldsAsTitle ( ) ;
1416 return await debounced ( [
1517 {
1618 sourceId : 'text' ,
1719 async getItems ( { query } ) {
1820 if ( query === '' ) return [ ] ;
19- const filter = `[all[tiddlers]!is[system]] ${ titleTextExclusionFilter ( ) } :filter[has[text]get[text]!compare:string:eq[]] +[search:${ getFieldsAsText ( ) } [${ query } ]]` ;
21+ const filter = `[all[tiddlers]!is[system]] ${ titleTextExclusionFilter ( ) } +[search:${ fieldsAsText } [${ query } ]]` ;
2022 return await filterTiddlersAsync ( filter , { system : false , exclude : [ ] } ) ;
2123 } ,
2224 getItemUrl ( { item } ) {
@@ -27,21 +29,23 @@ export const plugin = {
2729 return lingo ( 'UserText' ) ;
2830 } ,
2931 item ( { item, createElement, state } ) {
30- const title = typeof item . caption === 'string' && item . caption !== '' ? `${ item . caption } (${ item . title } )` : item . title ;
32+ const titles = titleFields . map ( field => item [ field ] ) . filter ( ( item ) : item is string => typeof item === 'string' && item !== '' ) . map ( ( item , index ) =>
33+ index === 0 ? item : `(${ item } )`
34+ ) . join ( ' ' ) ;
3135 const textCountAroundHit = 30 ;
3236 let contextNearText = '' ;
3337 const keywords = state . query . split ( ' ' ) . filter ( Boolean ) ;
34-
38+ const itemText = textFields . map ( field => item [ field ] ) . filter ( Boolean ) . join ( ' ' ) ;
3539 keywords . forEach ( keyword => {
36- const index = item . text . indexOf ( keyword ) ;
40+ const index = itemText . indexOf ( keyword ) ;
3741 if ( index !== - 1 ) {
3842 const start = Math . max ( 0 , index - textCountAroundHit ) ;
39- const end = Math . min ( item . text . length , index + keyword . length + textCountAroundHit ) ;
43+ const end = Math . min ( itemText . length , index + keyword . length + textCountAroundHit ) ;
4044 const prefix = start > 0 ? '...' : '' ;
41- const suffix = end < item . text . length ? '...' : '' ;
42- const beforeMatch = item . text . slice ( start , index ) ;
43- const matchedText = item . text . slice ( index , index + keyword . length ) ;
44- const afterMatch = item . text . slice ( index + keyword . length , end ) ;
45+ const suffix = end < itemText . length ? '...' : '' ;
46+ const beforeMatch = itemText . slice ( start , index ) ;
47+ const matchedText = itemText . slice ( index , index + keyword . length ) ;
48+ const afterMatch = itemText . slice ( index + keyword . length , end ) ;
4549
4650 contextNearText += `${ prefix } ${ beforeMatch } <mark>${ matchedText } </mark>${ afterMatch } ${ suffix } ` ;
4751 }
@@ -55,7 +59,7 @@ export const plugin = {
5559 onclick,
5660 onTap : onclick ,
5761 } , [
58- createElement ( 'div' , { style : 'margin-bottom:0.25em;' } , title ) ,
62+ createElement ( 'div' , { style : 'margin-bottom:0.25em;' } , titles ) ,
5963 createElement ( 'div' , { } , [
6064 createElement ( 'small' , { innerHTML : contextNearText } ) ,
6165 ] ) ,
0 commit comments