@@ -79,12 +79,25 @@ func RenderDataviewBlockWithIndex(v *Vault, idx *VaultIndex, raw string) templat
7979
8080func preprocessDataviewBlocks (s string , v * Vault ) string {
8181 re := regexp .MustCompile ("(?s)```dataview\\ s*\\ n(.*?)\\ n```" )
82+ if ! re .MatchString (s ) {
83+ return s
84+ }
85+ idx , err := v .BuildIndex ()
86+ if err != nil {
87+ return re .ReplaceAllStringFunc (s , func (m string ) string {
88+ parts := re .FindStringSubmatch (m )
89+ if len (parts ) != 2 {
90+ return m
91+ }
92+ return string (dataviewError (parts [1 ], err ))
93+ })
94+ }
8295 return re .ReplaceAllStringFunc (s , func (m string ) string {
8396 parts := re .FindStringSubmatch (m )
8497 if len (parts ) != 2 {
8598 return m
8699 }
87- return string (RenderDataviewBlock ( v , parts [1 ]))
100+ return string (RenderDataviewBlockWithIndex ( v , idx , parts [1 ]))
88101 })
89102}
90103
@@ -288,11 +301,12 @@ func splitTopLevel(s string, sep rune) []string {
288301
289302func evalDataviewRows (v * Vault , idx * VaultIndex , q dataviewQuery ) ([]dataviewRow , error ) {
290303 var rows []dataviewRow
304+ heavy := q .requiredHeavyFields ()
291305 if q .Kind == "TASK" {
292306 for _ , meta := range idx .Notes {
293307 if sourceMatches (meta , q .From ) {
294308 for _ , task := range extractTasksForNote (v , meta ) {
295- r := dataviewRow {Note : & meta , Task : & task , Data : dataviewBaseData (v , idx , meta )}
309+ r := dataviewRow {Note : & meta , Task : & task , Data : dataviewBaseData (v , idx , meta , heavy )}
296310 if whereMatches (r , q .Where ) {
297311 rows = append (rows , r )
298312 }
@@ -302,7 +316,7 @@ func evalDataviewRows(v *Vault, idx *VaultIndex, q dataviewQuery) ([]dataviewRow
302316 } else {
303317 for _ , meta := range idx .Notes {
304318 if sourceMatches (meta , q .From ) {
305- r := dataviewRow {Note : & meta , Data : dataviewBaseData (v , idx , meta )}
319+ r := dataviewRow {Note : & meta , Data : dataviewBaseData (v , idx , meta , heavy )}
306320 if whereMatches (r , q .Where ) {
307321 rows = append (rows , r )
308322 }
@@ -322,26 +336,31 @@ func evalDataviewRows(v *Vault, idx *VaultIndex, q dataviewQuery) ([]dataviewRow
322336 return rows , nil
323337}
324338
325- func dataviewBaseData (v * Vault , idx * VaultIndex , meta NoteMeta ) map [string ]any {
339+ type dataviewHeavyFields struct { Content , Inlinks bool }
340+
341+ func (q dataviewQuery ) requiredHeavyFields () dataviewHeavyFields {
342+ var exprs []string
343+ for _ , c := range q .Columns {
344+ exprs = append (exprs , c .Expr )
345+ }
346+ for _ , s := range q .Sorts {
347+ exprs = append (exprs , s .Expr )
348+ }
349+ exprs = append (exprs , q .Where , q .GroupBy , q .Flatten )
350+ joined := strings .Join (exprs , "\n " )
351+ return dataviewHeavyFields {Content : strings .Contains (joined , "file.content" ), Inlinks : strings .Contains (joined , "file.inlinks" )}
352+ }
353+
354+ func dataviewBaseData (v * Vault , idx * VaultIndex , meta NoteMeta , heavy dataviewHeavyFields ) map [string ]any {
326355 data := map [string ]any {}
327- if n , err := v .ReadNote (meta .RelPath ); err == nil {
328- data ["file.content" ] = n .Body
329- }
330- var inlinks []dataviewLink
331- stem := strings .TrimSuffix (filepath .Base (meta .RelPath ), filepath .Ext (meta .RelPath ))
332- noExt := strings .TrimSuffix (meta .RelPath , filepath .Ext (meta .RelPath ))
333- for _ , other := range idx .Notes {
334- if other .RelPath == meta .RelPath {
335- continue
336- }
337- for _ , target := range other .OutgoingWikiLinks {
338- if target == stem || target == noExt || target == meta .RelPath || strings .TrimSuffix (target , filepath .Ext (target )) == noExt {
339- inlinks = append (inlinks , dataviewLink {URL : other .URL , Text : noteFileName (other )})
340- break
341- }
356+ if heavy .Content {
357+ if n , err := v .ReadNote (meta .RelPath ); err == nil {
358+ data ["file.content" ] = n .Body
342359 }
343360 }
344- data ["file.inlinks" ] = inlinks
361+ if heavy .Inlinks {
362+ data ["file.inlinks" ] = idx .Inlinks [meta .RelPath ]
363+ }
345364 return data
346365}
347366
0 commit comments