@@ -2,7 +2,7 @@ import {Notice, Plugin, TFile, TFolder} from 'obsidian';
2
2
import { DEFAULT_SETTINGS , MediaDbPluginSettings , MediaDbSettingTab } from './settings/Settings' ;
3
3
import { APIManager } from './api/APIManager' ;
4
4
import { MediaTypeModel } from './models/MediaTypeModel' ;
5
- import { dateTimeToString , markdownTable , replaceIllegalFileNameCharactersInString , UserCancelError , UserSkipError } from './utils/Utils' ;
5
+ import { dateTimeToString , debugLog , markdownTable , replaceIllegalFileNameCharactersInString , UserCancelError , UserSkipError } from './utils/Utils' ;
6
6
import { OMDbAPI } from './api/apis/OMDbAPI' ;
7
7
import { MediaDbAdvancedSearchModal } from './modals/MediaDbAdvancedSearchModal' ;
8
8
import { MediaDbSearchResultModal } from './modals/MediaDbSearchResultModal' ;
@@ -27,7 +27,7 @@ export default class MediaDbPlugin extends Plugin {
27
27
28
28
// add icon to the left ribbon
29
29
const ribbonIconEl = this . addRibbonIcon ( 'database' , 'Add new Media DB entry' , ( evt : MouseEvent ) =>
30
- this . createMediaDbNote ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
30
+ this . createMediaDbNotes ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
31
31
) ;
32
32
ribbonIconEl . addClass ( 'obsidian-media-db-plugin-ribbon-class' ) ;
33
33
@@ -45,13 +45,13 @@ export default class MediaDbPlugin extends Plugin {
45
45
this . addCommand ( {
46
46
id : 'open-media-db-search-modal' ,
47
47
name : 'Add new Media DB entry' ,
48
- callback : ( ) => this . createMediaDbNote ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
48
+ callback : ( ) => this . createMediaDbNotes ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
49
49
} ) ;
50
50
// register command to open id search modal
51
51
this . addCommand ( {
52
52
id : 'open-media-db-id-search-modal' ,
53
53
name : 'Add new Media DB entry by id' ,
54
- callback : ( ) => this . createMediaDbNote ( this . openMediaDbIdSearchModal . bind ( this ) ) ,
54
+ callback : ( ) => this . createMediaDbNotes ( this . openMediaDbIdSearchModal . bind ( this ) ) ,
55
55
} ) ;
56
56
// register command to update the open note
57
57
this . addCommand ( {
@@ -85,7 +85,7 @@ export default class MediaDbPlugin extends Plugin {
85
85
this . modelPropertyMapper = new ModelPropertyMapper ( this . settings ) ;
86
86
}
87
87
88
- async createMediaDbNote ( modal : ( ) => Promise < MediaTypeModel [ ] > ) : Promise < void > {
88
+ async createMediaDbNotes ( modal : ( ) => Promise < MediaTypeModel [ ] > , attachFile ?: TFile ) : Promise < void > {
89
89
let models : MediaTypeModel [ ] = [ ] ;
90
90
try {
91
91
models = await modal ( ) ;
@@ -96,51 +96,78 @@ export default class MediaDbPlugin extends Plugin {
96
96
97
97
for ( const model of models ) {
98
98
try {
99
- await this . createMediaDbNoteFromModel ( await this . apiManager . queryDetailedInfo ( model ) ) ;
99
+ await this . createMediaDbNoteFromModel ( await this . apiManager . queryDetailedInfo ( model ) , attachFile ) ;
100
100
} catch ( e ) {
101
101
console . warn ( e ) ;
102
102
new Notice ( e . toString ( ) ) ;
103
103
}
104
104
}
105
105
}
106
106
107
- async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel ) : Promise < void > {
107
+ async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , attachFile ?: TFile ) : Promise < void > {
108
108
try {
109
109
console . log ( 'MDB | Creating new note...' ) ;
110
110
// console.log(mediaTypeModel);
111
111
112
- let fileContent = `---\n${ YAMLConverter . toYaml ( this . modelPropertyMapper . convertObject ( mediaTypeModel . toMetaDataObject ( ) ) ) } ---\n` ;
112
+ let metadata = this . modelPropertyMapper . convertObject ( mediaTypeModel . toMetaDataObject ( ) ) ;
113
+ if ( attachFile ) {
114
+ let attachFileMetadata : any = this . app . metadataCache . getFileCache ( attachFile ) . frontmatter ;
115
+ if ( attachFileMetadata ) {
116
+ attachFileMetadata = JSON . parse ( JSON . stringify ( attachFileMetadata ) ) ; // deep copy
117
+ delete attachFileMetadata . position ;
118
+ } else {
119
+ attachFileMetadata = { } ;
120
+ }
113
121
114
- if ( this . settings . templates ) {
115
- fileContent += await this . mediaTypeManager . getContent ( mediaTypeModel , this . app ) ;
122
+ metadata = Object . assign ( attachFileMetadata , metadata ) ;
116
123
}
117
124
118
- const fileName = replaceIllegalFileNameCharactersInString ( this . mediaTypeManager . getFileName ( mediaTypeModel ) ) ;
119
- const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } /${ fileName } .md` ;
125
+ debugLog ( metadata ) ;
120
126
121
- const folder = this . app . vault . getAbstractFileByPath ( this . settings . folder ) ;
122
- if ( ! folder ) {
123
- await this . app . vault . createFolder ( this . settings . folder . replace ( / \/ $ / , '' ) ) ;
127
+ let fileContent = `---\n${ YAMLConverter . toYaml ( metadata ) } ---\n` ;
128
+
129
+ if ( this . settings . templates ) {
130
+ fileContent += await this . mediaTypeManager . getContent ( mediaTypeModel , this . app ) ;
124
131
}
125
132
126
- const file = this . app . vault . getAbstractFileByPath ( filePath ) ;
127
- if ( file ) {
128
- await this . app . vault . delete ( file ) ;
133
+ if ( attachFile ) {
134
+ let attachFileContent : string = await this . app . vault . read ( attachFile ) ;
135
+ const regExp = new RegExp ( '^(---)\\n[\\s\\S]*\\n---' ) ;
136
+ attachFileContent = attachFileContent . replace ( regExp , '' ) ;
137
+ fileContent += '\n\n' + attachFileContent ;
129
138
}
130
139
131
- const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
140
+ await this . createNote ( this . mediaTypeManager . getFileName ( mediaTypeModel ) , fileContent ) ;
141
+ } catch ( e ) {
142
+ console . warn ( e ) ;
143
+ new Notice ( e . toString ( ) ) ;
144
+ }
145
+ }
146
+
147
+ async createNote ( fileName : string , fileContent : string , openFile : boolean = false ) {
148
+ fileName = replaceIllegalFileNameCharactersInString ( fileName ) ;
149
+ const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } /${ fileName } .md` ;
150
+
151
+ const folder = this . app . vault . getAbstractFileByPath ( this . settings . folder ) ;
152
+ if ( ! folder ) {
153
+ await this . app . vault . createFolder ( this . settings . folder . replace ( / \/ $ / , '' ) ) ;
154
+ }
155
+
156
+ const file = this . app . vault . getAbstractFileByPath ( filePath ) ;
157
+ if ( file ) {
158
+ await this . app . vault . delete ( file ) ;
159
+ }
132
160
133
- // open file
161
+ const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
162
+
163
+ // open file
164
+ if ( openFile ) {
134
165
const activeLeaf = this . app . workspace . getUnpinnedLeaf ( ) ;
135
166
if ( ! activeLeaf ) {
136
167
console . warn ( 'MDB | no active leaf, not opening media db note' ) ;
137
168
return ;
138
169
}
139
170
await activeLeaf . openFile ( targetFile , { state : { mode : 'source' } } ) ;
140
-
141
- } catch ( e ) {
142
- console . warn ( e ) ;
143
- new Notice ( e . toString ( ) ) ;
144
171
}
145
172
}
146
173
@@ -155,7 +182,7 @@ export default class MediaDbPlugin extends Plugin {
155
182
delete metadata . position ; // remove unnecessary data from the FrontMatterCache
156
183
metadata = this . modelPropertyMapper . convertObjectBack ( metadata ) ;
157
184
158
- console . log ( metadata ) ;
185
+ debugLog ( metadata ) ;
159
186
160
187
if ( ! metadata ?. type || ! metadata ?. dataSource || ! metadata ?. id ) {
161
188
throw new Error ( 'MDB | active note is not a Media DB entry or is missing metadata' ) ;
@@ -255,7 +282,7 @@ export default class MediaDbPlugin extends Plugin {
255
282
continue ;
256
283
}
257
284
258
- await this . createMediaDbNote ( async ( ) => selectedResults ) ;
285
+ await this . createMediaDbNotes ( async ( ) => selectedResults , appendContent ? file : null ) ;
259
286
}
260
287
}
261
288
0 commit comments