Query to list blocks with invalid block/page references

Some of the markdown pages that I have are imported from external sources like Roam, Notion etc… So the block references and page references end up in the following format
[Block Description](../Path/To/Page)
OR sometimes
[Block Description]()

So I would like to create a query to identify any blocks in my graph which have links in the above format. Is there any way to do this?

What about two queries:

  • For [Block Description](../) :
    #+BEGIN_QUERY
    {:query [:find (pull ?b [*])
       :where
         [(str "(?i)" "\\[.*\\]\\(\\.\\.\\/.*\\)") ?regexp]
         [(re-pattern ?regexp) ?pattern]
         [?b :block/content ?content]
         [(re-find ?pattern ?content)]
     ]
    }
    #+END_QUERY
    
  • For [Block Description]() :
    #+BEGIN_QUERY
    {:query [:find (pull ?b [*])
       :where
         [(str "(?i)" "\\[.*\\]\\(\\)") ?regexp]
         [(re-pattern ?regexp) ?pattern]
         [?b :block/content ?content]
         [(re-find ?pattern ?content)]
     ]
    }
    #+END_QUERY
    

@mentaloid, the first query also lists images with ![image](../) syntax. How can we ignore this?

I tried this, but it doesn’t seem to be working:

\\[^!\\]\\[.*\\]\\(\\.\\.\\/.*\\)

Try adding (^|[^!]) at the beginning, i.e.:
"(^|[^!])\\[.*\\]\\(\\.\\.\\/.*\\)"

1 Like

Related Questions and feature requests here: