@@ -44,12 +44,12 @@ export class QueryAPI {
4444 * const paths = engine.query.filesWithMetadata((file, cache, tags) => tags.includes("Foo") ? file.path : undefined);
4545 * ```
4646 */
47- public filesWithMetadata < T > ( query : ( file : TFile , cache : CachedMetadata | null , tags : string [ ] ) => T | undefined ) : T [ ] {
47+ public filesWithMetadata < T > ( query : ( file : TFile , cache : CachedMetadata | undefined , tags : string [ ] , frontmatterTags : string [ ] ) => T | undefined ) : T [ ] {
4848 validateAPIArgs (
4949 z . object ( {
5050 query : z
5151 . function ( )
52- . args ( this . apiInstance . validators . tFile , this . apiInstance . validators . cachedMetadata . nullable ( ) , z . string ( ) . array ( ) )
52+ . args ( this . apiInstance . validators . tFile , this . apiInstance . validators . cachedMetadata . optional ( ) , z . string ( ) . array ( ) , z . string ( ) . array ( ) )
5353 . returns ( z . unknown ( ) ) ,
5454 } ) ,
5555 { query } ,
@@ -60,7 +60,64 @@ export class QueryAPI {
6060 . map ( file => {
6161 const cache = this . apiInstance . app . metadataCache . getFileCache ( file ) ;
6262 const tags = cache ? ( getAllTags ( cache ) ?? [ ] ) : [ ] ;
63- return query ( file , cache , tags ) ;
63+ const frontmatterTags = ( cache ?. frontmatter ?. tags as string [ ] | undefined ) ?? [ ] ;
64+ return query ( file , cache ?? undefined , tags , frontmatterTags ) ;
65+ } )
66+ . filter ( x => x !== undefined ) ;
67+ }
68+
69+ public outgoingLinks ( file : TFile ) : { file : TFile ; cache : CachedMetadata | undefined ; tags : string [ ] ; frontmatterTags : string [ ] } [ ] {
70+ validateAPIArgs ( z . object ( { file : this . apiInstance . validators . tFile } ) , { file } ) ;
71+
72+ const metadata = this . apiInstance . app . metadataCache . getFileCache ( file ) ;
73+ if ( ! metadata ) {
74+ return [ ] ;
75+ }
76+
77+ const links = metadata . links ?? [ ] ;
78+ const files = links . map ( link => this . apiInstance . app . metadataCache . getFirstLinkpathDest ( link . link , file . path ) ) . filter ( file => file != null ) ;
79+ const uniqueFiles = Array . from ( new Set ( files ) ) ;
80+
81+ return uniqueFiles . map ( file => {
82+ const metadata = this . apiInstance . app . metadataCache . getFileCache ( file ) ;
83+ return {
84+ file : file ,
85+ cache : metadata ?? undefined ,
86+ tags : metadata ? ( getAllTags ( metadata ) ?? [ ] ) : [ ] ,
87+ frontmatterTags : ( metadata ?. frontmatter ?. tags as string [ ] | undefined ) ?? [ ] ,
88+ } ;
89+ } ) ;
90+ }
91+
92+ public incomingLinks ( file : TFile ) : { file : TFile ; cache : CachedMetadata | undefined ; tags : string [ ] ; frontmatterTags : string [ ] } [ ] {
93+ validateAPIArgs ( z . object ( { file : this . apiInstance . validators . tFile } ) , { file } ) ;
94+
95+ const files = this . apiInstance . app . vault . getMarkdownFiles ( ) ;
96+
97+ return files
98+ . map ( file => {
99+ const metadata = this . apiInstance . app . metadataCache . getFileCache ( file ) ;
100+ if ( ! metadata ) {
101+ return undefined ;
102+ }
103+
104+ const links = metadata . links ?? [ ] ;
105+
106+ if (
107+ links . some ( link => {
108+ const linkFile = this . apiInstance . app . metadataCache . getFirstLinkpathDest ( link . link , file . path ) ;
109+ return linkFile ?. path === file . path ;
110+ } )
111+ ) {
112+ return {
113+ file : file ,
114+ cache : metadata ,
115+ tags : getAllTags ( metadata ) ?? [ ] ,
116+ frontmatterTags : ( metadata ?. frontmatter ?. tags as string [ ] | undefined ) ?? [ ] ,
117+ } ;
118+ }
119+
120+ return undefined ;
64121 } )
65122 . filter ( x => x !== undefined ) ;
66123 }
0 commit comments