@@ -15,7 +15,7 @@ import {
1515 YouTubeEmbed ,
1616 GenericPreviewEmbed ,
1717} from "./embeds" ;
18- import { debounce , Debouncer , MarkdownView , Plugin } from "obsidian" ;
18+ import { debounce , Debouncer , MarkdownView , Plugin , TFile } from "obsidian" ;
1919import { DEFAULT_SETTINGS , GenericPreviewMetadata , PluginSettings } from "./settings" ;
2020import { SimpleEmbedPluginSettingTab } from "./settings-tab" ;
2121import { buildSimpleEmbedsViewPlugin } from "./view-plugin" ;
@@ -40,6 +40,10 @@ export default class SimpleEmbedsPlugin extends Plugin {
4040 processedMarkdown : Debouncer < [ ] > ;
4141 currentTheme : "dark" | "light" ;
4242 genericPreviewEmbed = new GenericPreviewEmbed ( ) ;
43+ genericPreviewCache = { } as {
44+ [ url : string ] : GenericPreviewMetadata ;
45+ } ;
46+ genericPreviewCacheFile = "genericPreviewCache.json" ;
4347
4448 async onload ( ) {
4549 console . log ( `Loading ${ this . manifest . name } v${ this . manifest . version } ` ) ;
@@ -81,6 +85,20 @@ export default class SimpleEmbedsPlugin extends Plugin {
8185 }
8286 } ) ,
8387 ) ;
88+
89+ // Load file for generic preview cache
90+ if ( ! this . app . vault . adapter . exists ( this . genericPreviewCacheFile ) ) {
91+ await this . app . vault . create ( "genericPreviewCache.json" , "{}" ) ;
92+ }
93+ try {
94+ const contents = JSON . parse (
95+ await this . app . vault . adapter . read ( this . genericPreviewCacheFile )
96+ ) ;
97+ this . genericPreviewCache = contents ;
98+ } catch ( e ) {
99+ console . error ( "Error reading generic preview cache file" ) ;
100+ console . error ( e ) ;
101+ }
84102 }
85103
86104 onunload ( ) {
@@ -103,8 +121,13 @@ export default class SimpleEmbedsPlugin extends Plugin {
103121 }
104122
105123 async saveGenericPreviewCache ( link : string , metadata : GenericPreviewMetadata ) {
106- this . settings . genericPreviewCache [ link ] = metadata ;
107- await this . saveData ( this . settings ) ;
124+ if ( this . genericPreviewCacheFile ) {
125+ this . genericPreviewCache [ link ] = metadata ;
126+ await this . app . vault . adapter . write (
127+ this . genericPreviewCacheFile ,
128+ JSON . stringify ( this . genericPreviewCache )
129+ ) ;
130+ }
108131 }
109132
110133 private _getCurrentTheme ( ) : "dark" | "light" {
0 commit comments