@@ -3,6 +3,7 @@ import { getAPI } from 'obsidian-dataview';
33
44export default class UniqueMetadataKeysPlugin extends Plugin {
55 api = getAPI ( ) ;
6+ keyColorMap = new Map < string , number > ( ) ;
67
78 onload ( ) {
89 this . addCommand ( {
@@ -25,13 +26,44 @@ export default class UniqueMetadataKeysPlugin extends Plugin {
2526 }
2627 uniqueKeys . delete ( "file" ) ;
2728
29+ // Define a list of categorical colors as single integers
30+ const colors = [
31+ 9991159 , 13948116 , 16772608 , 4280421 , 16113345 ,
32+ 9474192 , 4617584 , 15790320 , 12320668 , 16185078 ,
33+ 32896 , 15132410 , 10040346 , 16777128 , 8388608 ,
34+ 11184895 , 8421376 , 16761035 , 255 , 8421504
35+ ] ;
36+
37+ // Map each unique key to a color
38+ Array . from ( uniqueKeys ) . forEach ( ( key , index ) => {
39+ const color = colors [ index % colors . length ] ; // Cycle through colors
40+ this . keyColorMap . set ( key , color ) ;
41+ } ) ;
42+
2843 console . log ( 'Unique Metadata Keys with Link Values:' ) ;
29- uniqueKeys . forEach ( key => console . log ( key ) ) ;
44+ this . keyColorMap . forEach ( ( color , key ) => console . log ( `${ key } : ${ color } ` ) ) ;
45+ }
46+
47+ getColorForKey ( key : string ) : number | undefined {
48+ return this . keyColorMap . get ( key ) ;
49+ }
50+
51+ getMetadataKeyForLink ( sourceId : string , targetId : string ) : string | null {
52+ const sourcePage = this . api . page ( sourceId ) ;
53+ if ( ! sourcePage ) {
54+ return null ;
55+ }
56+
57+ for ( const [ key , value ] of Object . entries ( sourcePage ) ) {
58+ if ( this . isLink ( value ) && value . path === targetId ) {
59+ return key ;
60+ }
61+ }
62+
63+ return null ;
3064 }
3165
3266 isLink ( value : any ) : boolean {
33- // Check if the value is a link object
3467 return typeof value === 'object' && value . hasOwnProperty ( 'path' ) ;
3568 }
3669}
37-
0 commit comments