@@ -5,7 +5,7 @@ import {log} from "../logger/logManager";
5
5
import type QuickAdd from "../main" ;
6
6
import type { IChoiceExecutor } from "../IChoiceExecutor" ;
7
7
import { escapeRegExp , getLinesInString , templaterParseTemplate } from "../utility" ;
8
- import { CREATE_IF_NOT_FOUND_TOP } from "../constants" ;
8
+ import { CREATE_IF_NOT_FOUND_BOTTOM , CREATE_IF_NOT_FOUND_TOP } from "../constants" ;
9
9
10
10
export class CaptureChoiceFormatter extends CompleteFormatter {
11
11
private choice : ICaptureChoice ;
@@ -47,74 +47,86 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
47
47
return `${ this . fileContent } \n${ formatted } `
48
48
49
49
if ( this . choice . insertAfter . enabled ) {
50
- const target : string = await this . format ( this . choice . insertAfter . after ) ;
51
- const targetRegex = new RegExp ( `\s*${ escapeRegExp ( target ) } \s*` )
52
- let fileContentLines : string [ ] = getLinesInString ( this . fileContent ) ;
53
-
54
- const targetPosition = fileContentLines . findIndex ( line => targetRegex . test ( line ) ) ;
55
- if ( targetPosition === - 1 ) {
56
- if ( this . choice . insertAfter ?. createIfNotFound ) {
57
- const insertAfterLine : string = this . replaceLinebreakInString ( await this . format ( this . choice . insertAfter . after ) ) ;
58
- const insertAfterLineAndFormatted : string = `${ insertAfterLine } \n${ formatted } ` ;
59
-
60
- if ( this . choice . insertAfter ?. createIfNotFoundLocation === CREATE_IF_NOT_FOUND_TOP ) {
61
- const frontmatterEndPosition = this . file ? await this . getFrontmatterEndPosition ( this . file ) : 0 ;
62
- return this . insertTextAfterPositionInBody ( insertAfterLineAndFormatted , this . fileContent , frontmatterEndPosition ) ;
63
- }
64
- else {
65
- return this . insertTextAfterPositionInBody ( insertAfterLineAndFormatted , this . fileContent , fileContentLines . length - 1 ) ;
66
- }
67
- }
50
+ return await this . insertAfterHandler ( formatted ) ;
51
+ }
68
52
69
- log . logError ( "unable to find insert after line in file." )
53
+ const frontmatterEndPosition = this . file ? await this . getFrontmatterEndPosition ( this . file ) : null ;
54
+ if ( ! frontmatterEndPosition )
55
+ return `${ formatted } ${ this . fileContent } ` ;
56
+
57
+ return this . insertTextAfterPositionInBody ( formatted , this . fileContent , frontmatterEndPosition ) ;
58
+ }
59
+
60
+ private async insertAfterHandler ( formatted : string ) {
61
+ const targetString : string = await this . format ( this . choice . insertAfter . after ) ;
62
+ const targetRegex = new RegExp ( `\s*${ escapeRegExp ( targetString . replace ( '\\n' , '' ) ) } \s*` ) ;
63
+ let fileContentLines : string [ ] = getLinesInString ( this . fileContent ) ;
64
+
65
+ const targetPosition = fileContentLines . findIndex ( line => targetRegex . test ( line ) ) ;
66
+ const targetNotFound = targetPosition === - 1 ;
67
+ if ( targetNotFound ) {
68
+ if ( this . choice . insertAfter ?. createIfNotFound ) {
69
+ return await this . createInsertAfterIfNotFound ( formatted ) ;
70
70
}
71
71
72
- if ( this . choice . insertAfter ?. insertAtEnd ) {
73
- const nextHeaderPositionAfterTargetPosition = fileContentLines . slice ( targetPosition + 1 ) . findIndex ( line => ( / ^ # + | - - - / ) . test ( line ) )
74
- const foundNextHeader = nextHeaderPositionAfterTargetPosition !== - 1 ;
72
+ log . logError ( "unable to find insert after line in file." )
73
+ }
74
+
75
+ if ( this . choice . insertAfter ?. insertAtEnd ) {
76
+ const nextHeaderPositionAfterTargetPosition = fileContentLines
77
+ . slice ( targetPosition + 1 )
78
+ . findIndex ( line => ( / ^ # + | - - - / ) . test ( line ) )
79
+ const foundNextHeader = nextHeaderPositionAfterTargetPosition !== - 1 ;
75
80
76
- if ( foundNextHeader ) {
77
- let endOfSectionIndex : number ;
81
+ if ( foundNextHeader ) {
82
+ let endOfSectionIndex : number ;
78
83
79
- for ( let i = nextHeaderPositionAfterTargetPosition + targetPosition ; i > targetPosition ; i -- ) {
80
- const lineIsNewline : boolean = ( / ^ [ \s \n ] * $ / ) . test ( fileContentLines [ i ] ) ;
84
+ for ( let i = nextHeaderPositionAfterTargetPosition + targetPosition ; i > targetPosition ; i -- ) {
85
+ const lineIsNewline : boolean = ( / ^ [ \s \n ] * $ / ) . test ( fileContentLines [ i ] ) ;
81
86
82
- if ( ! lineIsNewline ) {
83
- endOfSectionIndex = i ;
84
- break ;
85
- }
87
+ if ( ! lineIsNewline ) {
88
+ endOfSectionIndex = i ;
89
+ break ;
86
90
}
91
+ }
87
92
88
- if ( ! endOfSectionIndex ) endOfSectionIndex = targetPosition ;
93
+ if ( ! endOfSectionIndex ) endOfSectionIndex = targetPosition ;
89
94
90
- return this . insertTextAfterPositionInBody ( formatted , this . fileContent , endOfSectionIndex ) ;
91
- } else {
92
- return this . insertTextAfterPositionInBody ( formatted , this . fileContent , fileContentLines . length - 1 ) ;
93
- }
95
+ return this . insertTextAfterPositionInBody ( formatted , this . fileContent , endOfSectionIndex ) ;
96
+ } else {
97
+ return this . insertTextAfterPositionInBody ( formatted , this . fileContent , fileContentLines . length - 1 ) ;
94
98
}
95
-
96
- return this . insertTextAfterPositionInBody ( formatted , this . fileContent , targetPosition ) ;
97
99
}
98
100
99
- const frontmatterEndPosition = this . file ? await this . getFrontmatterEndPosition ( this . file ) : null ;
100
- if ( ! frontmatterEndPosition )
101
- return `${ formatted } ${ this . fileContent } ` ;
101
+ return this . insertTextAfterPositionInBody ( formatted , this . fileContent , targetPosition ) ;
102
+ }
102
103
103
- return this . insertTextAfterPositionInBody ( formatted , this . fileContent , frontmatterEndPosition ) ;
104
+ private async createInsertAfterIfNotFound ( formatted : string ) {
105
+ const insertAfterLine : string = this . replaceLinebreakInString ( await this . format ( this . choice . insertAfter . after ) ) ;
106
+ const insertAfterLineAndFormatted : string = `${ insertAfterLine } \n${ formatted } ` ;
107
+
108
+ if ( this . choice . insertAfter ?. createIfNotFoundLocation === CREATE_IF_NOT_FOUND_TOP ) {
109
+ const frontmatterEndPosition = this . file ? await this . getFrontmatterEndPosition ( this . file ) : - 1 ;
110
+ return this . insertTextAfterPositionInBody ( insertAfterLineAndFormatted , this . fileContent , frontmatterEndPosition ) ;
111
+ }
112
+
113
+ if ( this . choice . insertAfter ?. createIfNotFoundLocation === CREATE_IF_NOT_FOUND_BOTTOM ) {
114
+ return `${ this . fileContent } \n${ insertAfterLineAndFormatted } ` ;
115
+ }
104
116
}
105
117
106
118
private async getFrontmatterEndPosition ( file : TFile ) {
107
119
const fileCache = await this . app . metadataCache . getFileCache ( file ) ;
108
120
109
121
if ( ! fileCache || ! fileCache . frontmatter ) {
110
122
log . logMessage ( "could not get frontmatter. Maybe there isn't any." )
111
- return 0 ;
123
+ return - 1 ;
112
124
}
113
125
114
126
if ( fileCache . frontmatter . position )
115
127
return fileCache . frontmatter . position . end . line ;
116
128
117
- return 0 ;
129
+ return - 1 ;
118
130
}
119
131
120
132
private insertTextAfterPositionInBody ( text : string , body : string , pos : number ) : string {
0 commit comments