@@ -2,7 +2,7 @@ import {Notice, Plugin, TFile, TFolder} from 'obsidian';
22import { DEFAULT_SETTINGS , MediaDbPluginSettings , MediaDbSettingTab } from './settings/Settings' ;
33import { APIManager } from './api/APIManager' ;
44import { 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' ;
66import { OMDbAPI } from './api/apis/OMDbAPI' ;
77import { MediaDbAdvancedSearchModal } from './modals/MediaDbAdvancedSearchModal' ;
88import { MediaDbSearchResultModal } from './modals/MediaDbSearchResultModal' ;
@@ -27,7 +27,7 @@ export default class MediaDbPlugin extends Plugin {
2727
2828 // add icon to the left ribbon
2929 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 ) ) ,
3131 ) ;
3232 ribbonIconEl . addClass ( 'obsidian-media-db-plugin-ribbon-class' ) ;
3333
@@ -45,13 +45,13 @@ export default class MediaDbPlugin extends Plugin {
4545 this . addCommand ( {
4646 id : 'open-media-db-search-modal' ,
4747 name : 'Add new Media DB entry' ,
48- callback : ( ) => this . createMediaDbNote ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
48+ callback : ( ) => this . createMediaDbNotes ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
4949 } ) ;
5050 // register command to open id search modal
5151 this . addCommand ( {
5252 id : 'open-media-db-id-search-modal' ,
5353 name : 'Add new Media DB entry by id' ,
54- callback : ( ) => this . createMediaDbNote ( this . openMediaDbIdSearchModal . bind ( this ) ) ,
54+ callback : ( ) => this . createMediaDbNotes ( this . openMediaDbIdSearchModal . bind ( this ) ) ,
5555 } ) ;
5656 // register command to update the open note
5757 this . addCommand ( {
@@ -85,7 +85,7 @@ export default class MediaDbPlugin extends Plugin {
8585 this . modelPropertyMapper = new ModelPropertyMapper ( this . settings ) ;
8686 }
8787
88- async createMediaDbNote ( modal : ( ) => Promise < MediaTypeModel [ ] > ) : Promise < void > {
88+ async createMediaDbNotes ( modal : ( ) => Promise < MediaTypeModel [ ] > , attachFile ?: TFile ) : Promise < void > {
8989 let models : MediaTypeModel [ ] = [ ] ;
9090 try {
9191 models = await modal ( ) ;
@@ -96,51 +96,78 @@ export default class MediaDbPlugin extends Plugin {
9696
9797 for ( const model of models ) {
9898 try {
99- await this . createMediaDbNoteFromModel ( await this . apiManager . queryDetailedInfo ( model ) ) ;
99+ await this . createMediaDbNoteFromModel ( await this . apiManager . queryDetailedInfo ( model ) , attachFile ) ;
100100 } catch ( e ) {
101101 console . warn ( e ) ;
102102 new Notice ( e . toString ( ) ) ;
103103 }
104104 }
105105 }
106106
107- async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel ) : Promise < void > {
107+ async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , attachFile ?: TFile ) : Promise < void > {
108108 try {
109109 console . log ( 'MDB | Creating new note...' ) ;
110110 // console.log(mediaTypeModel);
111111
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+ }
113121
114- if ( this . settings . templates ) {
115- fileContent += await this . mediaTypeManager . getContent ( mediaTypeModel , this . app ) ;
122+ metadata = Object . assign ( attachFileMetadata , metadata ) ;
116123 }
117124
118- const fileName = replaceIllegalFileNameCharactersInString ( this . mediaTypeManager . getFileName ( mediaTypeModel ) ) ;
119- const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } /${ fileName } .md` ;
125+ debugLog ( metadata ) ;
120126
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 ) ;
124131 }
125132
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 ;
129138 }
130139
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+ }
132160
133- // open file
161+ const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
162+
163+ // open file
164+ if ( openFile ) {
134165 const activeLeaf = this . app . workspace . getUnpinnedLeaf ( ) ;
135166 if ( ! activeLeaf ) {
136167 console . warn ( 'MDB | no active leaf, not opening media db note' ) ;
137168 return ;
138169 }
139170 await activeLeaf . openFile ( targetFile , { state : { mode : 'source' } } ) ;
140-
141- } catch ( e ) {
142- console . warn ( e ) ;
143- new Notice ( e . toString ( ) ) ;
144171 }
145172 }
146173
@@ -155,7 +182,7 @@ export default class MediaDbPlugin extends Plugin {
155182 delete metadata . position ; // remove unnecessary data from the FrontMatterCache
156183 metadata = this . modelPropertyMapper . convertObjectBack ( metadata ) ;
157184
158- console . log ( metadata ) ;
185+ debugLog ( metadata ) ;
159186
160187 if ( ! metadata ?. type || ! metadata ?. dataSource || ! metadata ?. id ) {
161188 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 {
255282 continue ;
256283 }
257284
258- await this . createMediaDbNote ( async ( ) => selectedResults ) ;
285+ await this . createMediaDbNotes ( async ( ) => selectedResults , appendContent ? file : null ) ;
259286 }
260287 }
261288
0 commit comments