@@ -34,31 +34,6 @@ export default class GraphLinkTypesPlugin extends Plugin {
3434 } ) ;
3535 }
3636
37- // Get the metadata key for a link between two pages
38- getMetadataKeyForLink ( sourceId : string , targetId : string ) : string | null {
39-
40- // Retrieve the source page
41- const sourcePage : Page | undefined = this . api . page ( sourceId ) ;
42- if ( ! sourcePage ) return null ;
43-
44- // Loop through the properties of the source page
45- for ( const [ key , value ] of Object . entries ( sourcePage ) ) {
46- // Check if the value is a link and matches the targetId
47- if ( this . isDataviewLink ( value ) && value . path === targetId ) {
48- return key ;
49- }
50- // Check if the value is an array of links and find a match
51- if ( Array . isArray ( value ) ) {
52- for ( const linkDataView of value ) {
53- if ( this . isDataviewLink ( linkDataView ) && linkDataView . path === targetId ) {
54- return key ;
55- }
56- }
57- }
58- }
59- return null ;
60- }
61-
6237 // Find the first valid graph renderer in the workspace
6338 findRenderer ( ) : CustomRenderer | null {
6439 let graphLeaves = this . app . workspace . getLeavesOfType ( 'graph' ) ;
@@ -266,6 +241,71 @@ export default class GraphLinkTypesPlugin extends Plugin {
266241 return { x : linkX * scale + panX , y : linkY * scale + panY } ;
267242 }
268243
244+ // Method to determine the type of a value, now a class method
245+ private determineLinkType ( value : any ) : LinkType {
246+ if ( typeof value === 'object' && value !== null && 'path' in value ) {
247+ return LinkType . DataviewLink ;
248+ } else if ( typeof value === 'string' && value . includes ( '](' ) ) {
249+ return LinkType . MarkdownLink ;
250+ } else if ( typeof value === 'string' ) {
251+ return LinkType . String ;
252+ } else if ( Array . isArray ( value ) ) {
253+ return LinkType . Array ;
254+ } else {
255+ return LinkType . Other ;
256+ }
257+ }
258+
259+ // Get the metadata key for a link between two pages
260+ getMetadataKeyForLink ( sourceId : string , targetId : string ) : string | null {
261+ const sourcePage : Page | undefined = this . api . page ( sourceId ) ;
262+ if ( ! sourcePage ) return null ;
263+
264+ for ( const [ key , value ] of Object . entries ( sourcePage ) ) {
265+ const valueType = this . determineLinkType ( value ) ;
266+
267+ switch ( valueType ) {
268+ case LinkType . DataviewLink :
269+ if ( value . path === targetId ) {
270+ return key ;
271+ }
272+ break ;
273+ case LinkType . MarkdownLink :
274+ if ( this . extractPathFromMarkdownLink ( value ) === targetId ) {
275+ return key ;
276+ }
277+ break ;
278+ case LinkType . Array :
279+ for ( const item of value ) {
280+ if ( this . determineLinkType ( item ) === LinkType . DataviewLink && item . path === targetId ) {
281+ return key ;
282+ }
283+ if ( this . determineLinkType ( item ) === LinkType . MarkdownLink && this . extractPathFromMarkdownLink ( item ) === targetId ) {
284+ return key ;
285+ }
286+ }
287+ break ;
288+ // Handle other cases as needed
289+ }
290+ }
291+ return null ;
292+ }
293+
294+ // Utility function to extract the file path from a Markdown link
295+ private extractPathFromMarkdownLink ( markdownLink : string ) : string {
296+ const match = markdownLink . match ( / \[ .* \] \( ( .* ?) \) / ) ;
297+ return match ? match [ 1 ] : '' ;
298+ }
299+
300+ // Utility function to check if a value is a Markdown link
301+ isMarkdownLink ( value : any , targetId : string ) : boolean {
302+ if ( typeof value === 'string' ) {
303+ const path = this . extractPathFromMarkdownLink ( value ) ;
304+ return path === targetId ;
305+ }
306+ return false ;
307+ }
308+
269309 // Utility function to check if a value is a link
270310 isDataviewLink ( value : any ) : boolean {
271311 return typeof value === 'object' && value . hasOwnProperty ( 'path' ) ;
@@ -340,3 +380,13 @@ interface CustomLink {
340380 y : number ;
341381 } ;
342382}
383+
384+ // Define the enum outside the class
385+ enum LinkType {
386+ DataviewLink ,
387+ MarkdownLink ,
388+ String ,
389+ Array ,
390+ Other
391+ }
392+
0 commit comments