@@ -39,29 +39,45 @@ export default Component.extend({
3939 matches : computed ( function ( ) {
4040 let metadata = this . get ( 'result.resultInfo.matchData.metadata' ) ;
4141
42- return Object . keys ( metadata ) . reduce ( ( matches , key ) => {
43- let match = metadata [ key ] ;
44- if ( match . text ) {
45- let text = this . get ( 'result.document.text' ) ;
46- let spaceIndices = text . split ( "" )
47- . map ( ( char , index ) => ( char === ' ' ) ? index : null )
48- . filter ( val => val > 0 ) ;
42+ return Object . keys ( metadata ) . reduce ( ( matches , term ) => {
43+ let match = metadata [ term ] ;
44+ let query = this . get ( 'query' ) ;
45+ let normalizedQuery = query . toLowerCase ( ) ;
46+ Object . keys ( match ) . forEach ( ( key ) => {
47+ if ( key === 'text' ) {
48+ let text = this . get ( 'result.document.text' ) ;
49+ let spaceIndices = text . split ( "" )
50+ . map ( ( char , index ) => ( char === ' ' ) ? index : null )
51+ . filter ( val => val > 0 ) ;
4952
50- match . text . position . forEach ( ( [ wordStart , length ] ) => {
51- let spaceAfterWord = spaceIndices . find ( i => i > wordStart ) ;
52- let indexOfSpaceAfterWord = spaceIndices . indexOf ( spaceAfterWord ) ;
53- let indexOfSpaceBeforeWord = indexOfSpaceAfterWord - 1 ;
54- let indexOfStartingSpace = ( indexOfSpaceBeforeWord > 3 ) ? indexOfSpaceBeforeWord - 3 : 0 ;
55- let indexOfEndingSpace = ( ( indexOfSpaceAfterWord + 3 ) < spaceIndices . length ) ? indexOfSpaceAfterWord + 3 : spaceIndices . length ;
56- let matchingText = text . slice ( spaceIndices [ indexOfStartingSpace ] , spaceIndices [ indexOfEndingSpace ] ) ;
57- let query = this . get ( 'query' ) ;
58- matchingText = matchingText . replace ( query , `<em class='docs-viewer-search__result-item__text--emphasis'>${ query } </em>` ) ;
53+ match . text . position . forEach ( ( [ wordStart , length ] ) => {
54+ let spaceAfterWord = spaceIndices . find ( i => i > wordStart ) ;
55+ let indexOfSpaceAfterWord = spaceIndices . indexOf ( spaceAfterWord ) ;
56+ let indexOfSpaceBeforeWord = indexOfSpaceAfterWord - 1 ;
57+ let indexOfStartingSpace = ( indexOfSpaceBeforeWord > 3 ) ? indexOfSpaceBeforeWord - 3 : 0 ;
58+ let indexOfEndingSpace = ( ( indexOfSpaceAfterWord + 3 ) < spaceIndices . length ) ? indexOfSpaceAfterWord + 3 : spaceIndices . length ;
59+ let matchingText = text . slice ( spaceIndices [ indexOfStartingSpace ] , spaceIndices [ indexOfEndingSpace ] ) ;
60+ matchingText = this . _highlight ( matchingText , matchingText . indexOf ( query ) , query . length ) ;
5961
60- matches . push ( matchingText ) ;
61- } ) ;
62- }
62+ matches . push ( matchingText ) ;
63+ } ) ;
64+ } else {
65+ let normalizedTerm = term . toLowerCase ( ) ;
66+ this . get ( 'result.document.keywords' ) . forEach ( ( keyword ) => {
67+ let normalizedKeyword = keyword . toLowerCase ( ) ;
68+ if ( keyword . toLowerCase ( ) . indexOf ( normalizedTerm ) !== - 1 ) {
69+ let index = normalizedKeyword . indexOf ( normalizedQuery ) ;
70+ matches . push ( this . _highlight ( keyword , index , normalizedQuery . length ) ) ;
71+ }
72+ } ) ;
73+ }
74+ } ) ;
6375
6476 return matches ;
65- } , [ ] ) . join ( ' · ' ) ;
66- } )
77+ } , [ ] ) . join ( ' · ' ) ;
78+ } ) ,
79+
80+ _highlight ( text , start , length ) {
81+ return `${ text . slice ( 0 , start ) } <em class='docs-viewer-search__result-item__text--emphasis'>${ text . slice ( start , start + length ) } </em>${ text . slice ( start + length ) } ` ;
82+ }
6783} ) ;
0 commit comments