1
- import { Notice , Plugin , stringifyYaml , TFile } from 'obsidian' ;
1
+ import { parseYaml , Plugin , stringifyYaml , TFile } from 'obsidian' ;
2
2
import { DEFAULT_SETTINGS , MetaBindPluginSettings , MetaBindSettingTab } from './settings/Settings' ;
3
- import { InputField } from './InputField ' ;
3
+ import { MarkdownInputField } from './MarkdownInputField ' ;
4
4
import { getFileName , isPath , removeFileEnding } from './Utils' ;
5
+ import { Logger } from './Logger' ;
5
6
6
7
export default class MetaBindPlugin extends Plugin {
7
8
settings : MetaBindPluginSettings ;
8
9
10
+ activeMarkdownInputFields : MarkdownInputField [ ] ;
11
+ markDownInputFieldIndex : number ;
12
+
9
13
async onload ( ) {
10
14
await this . loadSettings ( ) ;
11
15
12
- // This creates an icon in the left ribbon.
13
- const ribbonIconEl = this . addRibbonIcon ( 'dice' , 'Sample Plugin' , ( evt : MouseEvent ) => {
14
-
15
- } ) ;
16
+ Logger . plugin = this ;
16
17
18
+ this . activeMarkdownInputFields = [ ] ;
19
+ this . markDownInputFieldIndex = 0 ;
17
20
18
21
this . registerMarkdownPostProcessor ( ( element , context ) => {
19
22
const codeBlocks = element . querySelectorAll ( 'code' ) ;
@@ -23,24 +26,51 @@ export default class MetaBindPlugin extends Plugin {
23
26
const isInputField = text . startsWith ( 'INPUT[' ) && text . endsWith ( ']' ) ;
24
27
// console.log(context.sourcePath);
25
28
if ( isInputField ) {
26
- context . addChild ( new InputField ( codeBlock , text , this , context . sourcePath ) ) ;
29
+ context . addChild ( new MarkdownInputField ( codeBlock , text , this , context . sourcePath , this . markDownInputFieldIndex ) ) ;
30
+ this . markDownInputFieldIndex += 1 ;
27
31
}
28
32
}
29
33
} ) ;
30
34
31
- this . registerEvent ( this . app . vault . on ( 'modify' , ( ) => {
32
- // console.log('file modified')
35
+ this . registerEvent ( this . app . vault . on ( 'modify' , async abstractFile => {
36
+ if ( abstractFile instanceof TFile ) {
37
+ await this . updateMarkdownInputFieldsOnFileChange ( abstractFile as TFile ) ;
38
+ }
33
39
} ) ) ;
34
40
35
41
this . addSettingTab ( new MetaBindSettingTab ( this . app , this ) ) ;
36
42
}
37
43
38
44
onunload ( ) {
45
+ for ( const activeMarkdownInputField of this . activeMarkdownInputFields ) {
46
+ activeMarkdownInputField . unload ( ) ;
47
+ }
48
+ }
49
+
50
+ registerMarkdownInputField ( markdownInputField : MarkdownInputField ) {
51
+ this . activeMarkdownInputFields . push ( markdownInputField ) ;
52
+ }
39
53
54
+ unregisterMarkdownInputField ( markdownInputField : MarkdownInputField ) {
55
+ this . activeMarkdownInputFields = this . activeMarkdownInputFields . filter ( x => x . uid !== markdownInputField . uid ) ;
56
+ }
57
+
58
+ async updateMarkdownInputFieldsOnFileChange ( file : TFile ) {
59
+ const metadata = await this . getMetaDataForFile ( file ) ;
60
+
61
+ for ( const activeMarkdownInputField of this . activeMarkdownInputFields ) {
62
+ if ( ! activeMarkdownInputField . file || ! activeMarkdownInputField . isBound ) {
63
+ continue ;
64
+ }
65
+
66
+ if ( activeMarkdownInputField . file . path === file . path ) {
67
+ activeMarkdownInputField . updateValue ( metadata [ activeMarkdownInputField . boundMetadataField ] ) ;
68
+ }
69
+ }
40
70
}
41
71
42
72
async updateMetaData ( key : string , value : any , file : TFile ) {
43
- // console.log('update', key, value);
73
+ Logger . logDebug ( `updating ' ${ key } : ${ value } ' in ' ${ file . path } '` ) ;
44
74
45
75
if ( ! file ) {
46
76
console . log ( 'no file' ) ;
@@ -51,12 +81,14 @@ export default class MetaBindPlugin extends Plugin {
51
81
const regExp = new RegExp ( '^(---)\\n[\\s\\S]*\\n---' ) ;
52
82
fileContent = fileContent . replace ( regExp , '' ) ;
53
83
54
- let metadata : any = this . getMetaDataForFile ( file ) ;
84
+ let metadata : any = await this . getMetaDataForFile ( file ) ;
85
+ // console.log(metadata);
55
86
if ( ! metadata ) {
56
87
return ;
57
88
}
58
89
59
90
metadata [ key ] = value ;
91
+ // console.log(metadata);
60
92
61
93
fileContent = `---\n${ stringifyYaml ( metadata ) } ---` + fileContent ;
62
94
await this . app . vault . modify ( file , fileContent ) ;
@@ -82,8 +114,12 @@ export default class MetaBindPlugin extends Plugin {
82
114
return files ;
83
115
}
84
116
85
- getMetaDataForFile ( file : TFile ) : any {
117
+ async getMetaDataForFile ( file : TFile ) : Promise < any > {
118
+ // console.log(`reading metadata for ${file.path}`);
119
+
86
120
let metadata : any ;
121
+
122
+ /* metadata cache is unreliable and might not be updated yet
87
123
try {
88
124
metadata = this.app.metadataCache.getFileCache(file).frontmatter;
89
125
} catch (e) {
@@ -98,6 +134,26 @@ export default class MetaBindPlugin extends Plugin {
98
134
} else {
99
135
metadata = {};
100
136
}
137
+ */
138
+
139
+ let fileContent : string = await this . app . vault . read ( file ) ;
140
+ const regExp = new RegExp ( '^(---)\\n[\\s\\S]*\\n---' ) ;
141
+ let frontMatter = regExp . exec ( fileContent ) [ 0 ] ;
142
+ if ( frontMatter === null ) {
143
+ return { } ;
144
+ }
145
+ // console.log(frontMatter);
146
+ frontMatter = frontMatter . substring ( 4 ) ;
147
+ frontMatter = frontMatter . substring ( 0 , frontMatter . length - 3 ) ;
148
+ // console.log(frontMatter);
149
+
150
+ metadata = parseYaml ( frontMatter ) ;
151
+
152
+ if ( ! metadata ) {
153
+ metadata = { } ;
154
+ }
155
+
156
+ //console.log(metadata);
101
157
102
158
return metadata ;
103
159
}
0 commit comments