1- import { Notice , Plugin , stringifyYaml , TFile } from 'obsidian' ;
1+ import { parseYaml , Plugin , stringifyYaml , TFile } from 'obsidian' ;
22import { DEFAULT_SETTINGS , MetaBindPluginSettings , MetaBindSettingTab } from './settings/Settings' ;
3- import { InputField } from './InputField ' ;
3+ import { MarkdownInputField } from './MarkdownInputField ' ;
44import { getFileName , isPath , removeFileEnding } from './Utils' ;
5+ import { Logger } from './Logger' ;
56
67export default class MetaBindPlugin extends Plugin {
78 settings : MetaBindPluginSettings ;
89
10+ activeMarkdownInputFields : MarkdownInputField [ ] ;
11+ markDownInputFieldIndex : number ;
12+
913 async onload ( ) {
1014 await this . loadSettings ( ) ;
1115
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 ;
1617
18+ this . activeMarkdownInputFields = [ ] ;
19+ this . markDownInputFieldIndex = 0 ;
1720
1821 this . registerMarkdownPostProcessor ( ( element , context ) => {
1922 const codeBlocks = element . querySelectorAll ( 'code' ) ;
@@ -23,24 +26,51 @@ export default class MetaBindPlugin extends Plugin {
2326 const isInputField = text . startsWith ( 'INPUT[' ) && text . endsWith ( ']' ) ;
2427 // console.log(context.sourcePath);
2528 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 ;
2731 }
2832 }
2933 } ) ;
3034
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+ }
3339 } ) ) ;
3440
3541 this . addSettingTab ( new MetaBindSettingTab ( this . app , this ) ) ;
3642 }
3743
3844 onunload ( ) {
45+ for ( const activeMarkdownInputField of this . activeMarkdownInputFields ) {
46+ activeMarkdownInputField . unload ( ) ;
47+ }
48+ }
49+
50+ registerMarkdownInputField ( markdownInputField : MarkdownInputField ) {
51+ this . activeMarkdownInputFields . push ( markdownInputField ) ;
52+ }
3953
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+ }
4070 }
4171
4272 async updateMetaData ( key : string , value : any , file : TFile ) {
43- // console.log('update', key, value);
73+ Logger . logDebug ( `updating ' ${ key } : ${ value } ' in ' ${ file . path } '` ) ;
4474
4575 if ( ! file ) {
4676 console . log ( 'no file' ) ;
@@ -51,12 +81,14 @@ export default class MetaBindPlugin extends Plugin {
5181 const regExp = new RegExp ( '^(---)\\n[\\s\\S]*\\n---' ) ;
5282 fileContent = fileContent . replace ( regExp , '' ) ;
5383
54- let metadata : any = this . getMetaDataForFile ( file ) ;
84+ let metadata : any = await this . getMetaDataForFile ( file ) ;
85+ // console.log(metadata);
5586 if ( ! metadata ) {
5687 return ;
5788 }
5889
5990 metadata [ key ] = value ;
91+ // console.log(metadata);
6092
6193 fileContent = `---\n${ stringifyYaml ( metadata ) } ---` + fileContent ;
6294 await this . app . vault . modify ( file , fileContent ) ;
@@ -82,8 +114,12 @@ export default class MetaBindPlugin extends Plugin {
82114 return files ;
83115 }
84116
85- getMetaDataForFile ( file : TFile ) : any {
117+ async getMetaDataForFile ( file : TFile ) : Promise < any > {
118+ // console.log(`reading metadata for ${file.path}`);
119+
86120 let metadata : any ;
121+
122+ /* metadata cache is unreliable and might not be updated yet
87123 try {
88124 metadata = this.app.metadataCache.getFileCache(file).frontmatter;
89125 } catch (e) {
@@ -98,6 +134,26 @@ export default class MetaBindPlugin extends Plugin {
98134 } else {
99135 metadata = {};
100136 }
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);
101157
102158 return metadata ;
103159 }
0 commit comments