9
9
MarkdownPostProcessor ,
10
10
MarkdownPreviewRenderer ,
11
11
MarkdownFileInfo ,
12
+ TFile ,
12
13
} from "obsidian" ;
13
14
import { Direction , TaskCollector } from "./taskcollector-TaskCollector" ;
14
15
import { TaskCollectorSettingsTab } from "./taskcollector-SettingsTab" ;
@@ -82,7 +83,15 @@ export class TaskCollectorPlugin extends Plugin {
82
83
83
84
async editLines ( mark : string , lines ?: number [ ] ) : Promise < void > {
84
85
const activeFile = this . app . workspace . getActiveFile ( ) ;
85
- await this . app . vault . process ( activeFile , ( source ) : string => {
86
+ await this . editLinesInFile ( activeFile , mark , lines ) ;
87
+ }
88
+
89
+ async editLinesInFile (
90
+ file : TFile ,
91
+ mark : string ,
92
+ lines ?: number [ ] ,
93
+ ) : Promise < void > {
94
+ await this . app . vault . process ( file , ( source ) : string => {
86
95
return this . tc . markSelectedTask ( source , mark , lines ) ;
87
96
} ) ;
88
97
}
@@ -420,10 +429,13 @@ export class TaskCollectorPlugin extends Plugin {
420
429
el . querySelectorAll < HTMLInputElement > (
421
430
".task-list-item-checkbox" ,
422
431
) ;
423
- if ( ! checkboxes . length ) return ;
432
+ if ( ! checkboxes . length || ! ctx . sourcePath ) {
433
+ return ;
434
+ }
424
435
425
436
const isLivePreview =
426
437
! ! el . closest ( ".markdown-rendered" ) ;
438
+
427
439
this . tc . logDebug (
428
440
"markdown postprocessor" ,
429
441
`use context menu: ${ this . tc . cache . useContextMenu } ;` ,
@@ -433,6 +445,10 @@ export class TaskCollectorPlugin extends Plugin {
433
445
checkboxes ,
434
446
) ;
435
447
448
+ const targetFile = this . app . vault . getFileByPath (
449
+ ctx . sourcePath ,
450
+ ) ;
451
+
436
452
for ( const checkbox of Array . from ( checkboxes ) ) {
437
453
const section = ctx . getSectionInfo ( checkbox ) ;
438
454
if ( ! section ) continue ;
@@ -480,9 +496,11 @@ export class TaskCollectorPlugin extends Plugin {
480
496
this . tc ,
481
497
) ;
482
498
if ( mark ) {
483
- await this . editLines ( mark , [
484
- lineStart + line ,
485
- ] ) ;
499
+ await this . editLinesInFile (
500
+ targetFile ,
501
+ mark ,
502
+ [ lineStart + line ] ,
503
+ ) ;
486
504
}
487
505
} ,
488
506
) ;
0 commit comments