File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -1583,11 +1583,17 @@ paths:
15831583 properties :
15841584 end :
15851585 type : " number"
1586+ description : " Line number or 0 when the filename matched"
15861587 start :
15871588 type : " number"
1589+ description : " Line number or 0 when the filename matched"
1590+ source :
1591+ type : " string"
1592+ description : " Where the search term matched: 'filename' or 'content'"
15881593 required :
15891594 - " start"
15901595 - " end"
1596+ - " source"
15911597 type : " object"
15921598 required :
15931599 - " match"
Original file line number Diff line number Diff line change @@ -1010,17 +1010,40 @@ export default class RequestHandler {
10101010 } ) ;
10111011 }
10121012
1013+ // Below we virtually add the headline to the search text with 2 line breaks.
1014+ // That causes the start and end line numbers to be wrong with an offset of 3.
1015+ // This is fixed by substracting 3 from the start and end position.
1016+ const positionOffset = 3 ;
1017+
10131018 for ( const file of this . app . vault . getMarkdownFiles ( ) ) {
10141019 const cachedContents = await this . app . vault . cachedRead ( file ) ;
1020+
1021+ // Add the headline to the search text to include it in the search.
10151022 const result = search ( file . basename + "\n\n" + cachedContents ) ;
1023+
10161024 if ( result ) {
10171025 const contextMatches : SearchContext [ ] = [ ] ;
10181026 for ( const match of result . matches ) {
1027+ let matchDetails ;
1028+
1029+ if ( match [ 0 ] == 1 ) {
1030+ // When start position is line 1, that means the search term matched within the headline.
1031+ matchDetails = {
1032+ start : 1 ,
1033+ end : 1 ,
1034+ source : "filename"
1035+ }
1036+ } else {
1037+ // Otherwise, the match was in the content
1038+ matchDetails = {
1039+ start : match [ 0 ] - positionOffset ,
1040+ end : match [ 1 ] - positionOffset ,
1041+ source : "content"
1042+ }
1043+ }
1044+
10191045 contextMatches . push ( {
1020- match : {
1021- start : match [ 0 ] ,
1022- end : match [ 1 ] ,
1023- } ,
1046+ match : matchDetails ,
10241047 context : cachedContents . slice (
10251048 Math . max ( match [ 0 ] - contextLength , 0 ) ,
10261049 match [ 1 ] + contextLength
You can’t perform that action at this time.
0 commit comments