@@ -270,14 +270,22 @@ export class ButtonActionRunner {
270
270
}
271
271
272
272
async runReplaceInNoteAction ( action : ReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
273
- const replacement = action . templater
274
- ? await this . plugin . internal . evaluateTemplaterTemplate ( action . replacement , filePath )
275
- : action . replacement ;
273
+ if ( action . fromLine > action . toLine ) {
274
+ throw new Error ( 'From line cannot be greater than to line' ) ;
275
+ }
276
276
277
277
const content = await this . plugin . internal . readFilePath ( filePath ) ;
278
278
279
279
let splitContent = content . split ( '\n' ) ;
280
280
281
+ if ( action . fromLine < 0 || action . toLine > splitContent . length + 1 ) {
282
+ throw new Error ( 'Line numbers out of bounds' ) ;
283
+ }
284
+
285
+ const replacement = action . templater
286
+ ? await this . plugin . internal . evaluateTemplaterTemplate ( action . replacement , filePath )
287
+ : action . replacement ;
288
+
281
289
splitContent = [
282
290
...splitContent . slice ( 0 , action . fromLine - 1 ) ,
283
291
replacement ,
@@ -301,14 +309,22 @@ export class ButtonActionRunner {
301
309
throw new Error ( 'Position of the button in the note is unknown' ) ;
302
310
}
303
311
304
- const replacement = action . templater
305
- ? await this . plugin . internal . evaluateTemplaterTemplate ( action . replacement , filePath )
306
- : action . replacement ;
312
+ if ( position . lineStart > position . lineEnd ) {
313
+ throw new Error ( 'Position of the button in the note is invalid' ) ;
314
+ }
307
315
308
316
const content = await this . plugin . internal . readFilePath ( filePath ) ;
309
317
310
318
let splitContent = content . split ( '\n' ) ;
311
319
320
+ if ( position . lineStart < 0 || position . lineEnd > splitContent . length + 1 ) {
321
+ throw new Error ( 'Position of the button in the note is out of bounds' ) ;
322
+ }
323
+
324
+ const replacement = action . templater
325
+ ? await this . plugin . internal . evaluateTemplaterTemplate ( action . replacement , filePath )
326
+ : action . replacement ;
327
+
312
328
splitContent = [
313
329
...splitContent . slice ( 0 , position . lineStart ) ,
314
330
replacement ,
@@ -335,6 +351,10 @@ export class ButtonActionRunner {
335
351
336
352
let splitContent = content . split ( '\n' ) ;
337
353
354
+ if ( action . line < 1 || action . line > splitContent . length + 1 ) {
355
+ throw new Error ( 'Line number out of bounds' ) ;
356
+ }
357
+
338
358
const replacement = action . templater
339
359
? await this . plugin . internal . evaluateTemplaterTemplate ( action . value , filePath )
340
360
: action . value ;
0 commit comments