@@ -13,9 +13,14 @@ import {
1313 TwitterEmbed ,
1414 VimeoEmbed ,
1515 YouTubeEmbed ,
16+ GenericPreviewEmbed ,
1617} from "./embeds" ;
17- import { debounce , Debouncer , MarkdownView , Plugin } from "obsidian" ;
18- import { DEFAULT_SETTINGS , PluginSettings } from "./settings" ;
18+ import { debounce , Debouncer , MarkdownView , Plugin , TFile } from "obsidian" ;
19+ import {
20+ DEFAULT_SETTINGS ,
21+ GenericPreviewMetadata ,
22+ PluginSettings ,
23+ } from "./settings" ;
1924import { SimpleEmbedPluginSettingTab } from "./settings-tab" ;
2025import { buildSimpleEmbedsViewPlugin } from "./view-plugin" ;
2126
@@ -39,6 +44,15 @@ export default class SimpleEmbedsPlugin extends Plugin {
3944 processedMarkdown : Debouncer < [ ] > ;
4045 currentTheme : "dark" | "light" ;
4146
47+ genericPreviewEmbed = new GenericPreviewEmbed ( ) ;
48+ genericPreviewCache = null as {
49+ [ url : string ] : GenericPreviewMetadata ;
50+ } | null ;
51+ genericPreviewCacheFile =
52+ this . app . vault . configDir +
53+ "/plugins/obsidian-simple-embeds/genericPreviewCache.json" ;
54+ cacheFileLoadPromise = null as Promise < void > ;
55+
4256 async onload ( ) {
4357 console . log ( `Loading ${ this . manifest . name } v${ this . manifest . version } ` ) ;
4458 await this . loadSettings ( ) ;
@@ -57,7 +71,7 @@ export default class SimpleEmbedsPlugin extends Plugin {
5771
5872 this . registerMarkdownPostProcessor ( ( el , ctx ) => {
5973 const anchors = el . querySelectorAll (
60- "a.external-link" ,
74+ "a.external-link"
6175 ) as NodeListOf < HTMLAnchorElement > ;
6276 anchors . forEach ( ( anchor ) => {
6377 this . _handleAnchor ( anchor ) ;
@@ -77,8 +91,27 @@ export default class SimpleEmbedsPlugin extends Plugin {
7791 } ) ;
7892 } ) ;
7993 }
80- } ) ,
94+ } )
8195 ) ;
96+
97+ // Load file for generic preview cache
98+ const loadCacheFile = async ( ) => {
99+ if (
100+ ! ( await this . app . vault . adapter . exists ( this . genericPreviewCacheFile ) )
101+ ) {
102+ await this . app . vault . create ( this . genericPreviewCacheFile , "{}" ) ;
103+ }
104+ try {
105+ const contents = JSON . parse (
106+ await this . app . vault . adapter . read ( this . genericPreviewCacheFile )
107+ ) ;
108+ this . genericPreviewCache = contents ;
109+ } catch ( e ) {
110+ console . error ( "Error reading generic preview cache file" ) ;
111+ console . error ( e ) ;
112+ }
113+ } ;
114+ this . cacheFileLoadPromise = loadCacheFile ( ) ;
82115 }
83116
84117 onunload ( ) {
@@ -100,6 +133,19 @@ export default class SimpleEmbedsPlugin extends Plugin {
100133 } ) ;
101134 }
102135
136+ async saveGenericPreviewCache (
137+ link : string ,
138+ metadata : GenericPreviewMetadata
139+ ) {
140+ if ( this . genericPreviewCacheFile ) {
141+ this . genericPreviewCache [ link ] = metadata ;
142+ await this . app . vault . adapter . write (
143+ this . genericPreviewCacheFile ,
144+ JSON . stringify ( this . genericPreviewCache )
145+ ) ;
146+ }
147+ }
148+
103149 private _getCurrentTheme ( ) : "dark" | "light" {
104150 return document . body . classList . contains ( "theme-dark" ) ? "dark" : "light" ;
105151 }
@@ -117,7 +163,7 @@ export default class SimpleEmbedsPlugin extends Plugin {
117163
118164 const replaceWithEmbed = this . shouldReplaceWithEmbed (
119165 a . innerText ,
120- isWithinText ,
166+ isWithinText
121167 ) ;
122168 const fullWidth = a . innerText . includes ( "|fullwidth" ) ;
123169 // Remove any allowed properties:
@@ -141,9 +187,21 @@ export default class SimpleEmbedsPlugin extends Plugin {
141187 href ,
142188 fullWidth ,
143189 this . settings . centerEmbeds ,
144- this . settings . keepLinksInPreview ,
190+ this . settings . keepLinksInPreview
145191 ) ;
146192 this . _insertEmbed ( a , embed ) ;
193+ } else {
194+ if ( this . settings . replaceGenericLinks ) {
195+ // fall back to creating a generic embed
196+ const embed = this . createEmbed (
197+ this . genericPreviewEmbed ,
198+ href ,
199+ fullWidth ,
200+ this . settings . centerEmbeds ,
201+ this . settings . keepLinksInPreview
202+ ) ;
203+ this . _insertEmbed ( a , embed ) ;
204+ }
147205 }
148206 }
149207
@@ -162,7 +220,7 @@ export default class SimpleEmbedsPlugin extends Plugin {
162220 link : string ,
163221 fullWidth : boolean ,
164222 centered : boolean ,
165- keepLinks : boolean ,
223+ keepLinks : boolean
166224 ) {
167225 const container = document . createElement ( "div" ) ;
168226 container . classList . add ( "embed-container" ) ;
@@ -171,6 +229,7 @@ export default class SimpleEmbedsPlugin extends Plugin {
171229 container ,
172230 this . settings ,
173231 this . currentTheme ,
232+ this
174233 ) ;
175234 if ( fullWidth ) {
176235 embed . classList . add ( "full-width" ) ;
0 commit comments