Advanced query to pull all blocks with a specific tag, but exclude properties blocks ("frontmatter")

I’m trying to build a template for “topic pages”, i.e., pages that include all blocks with a specific tag.

Currently, I’m still experimenting on a single example page, without trying to add the extra complexity layer of templating.

The idea is as below (very simple, and solutions for this can be found in a number of places). This is actually very similar to the Linked References auto-included block at the bottom of pages, but I’d like some extra formatting power. (But if anyone can link me to the query powering Linked References, I’d be happy to start building on top of that.)

This solution to what I understood to be a similar problem doesn’t work unfortunately.

  • The page (named Foo Bar) has some frontmatter (page properties), and pulls in all blocks with the tag foo-bar in an Advanced Query:

    type:: topic
    topic:: foo-bar
    alias:: foo-bar
    
    - ## Notes
            - Something on the Foo Bar page itself
    - #+BEGIN_QUERY
      {:title [:h2 "Related blocks"]
       :query [:find (pull ?b [*])
               :where
               [?p :block/name "foo-bar"]
               [?b :block/refs ?p]]
      :result-transform (fn [r] (map (fn [m] (assoc m :block/collapsed? true)) r))
          :group-by-page? false
          :remove-block-children? true
          :breadcrumb-show? false
      }
      #+END_QUERY
    
  • This works fine in principle, but the properties block is pulled in as well:

I’ve searched here and the wider web and tried to find a solution for excluding either blocks that are on the same page as the query, or blocks that contain an alias:: property, but couldn’t get it to work.

This query by @Siferiax is elegant, but also includes the property block.

Does anyone have any ideas how to achieve excluding the property block?

Many thanks!

  • Firstly some considerations:
    • Front matter in Logseq MD is buggy, as it is often confused with the first block in unpredictable ways.
      • To ensure that the properties in the first block are understood as page-properties, should check the page data in Developer mode.
    • Aliases in Logseq MD are messy, as there is no low-level connection between a page and its aliases.
      • i.e. aliases are mostly high-level conventions that sometimes are taken in consideration, but sometimes are not.
  • With those out of the way, we can:
    • either exclude all blocks that contain alias:: with:

      (not
        [?b :block/properties ?props]
        [(get ?props :alias)]
      )
      
    • or define a rule for aliases:

        :in $ %
      
      ...
      
      :rules [
        [(self-or-alias ?a ?p)
          [(= ?a ?p)]
        ]
        [(self-or-alias ?a ?p)
          [?p :block/alias ?a]
        ]
        [(self-or-alias ?a ?p)
          [?a :block/alias ?p]
        ]
      ]
      
    • then use that rule to exclude the property block of the referenced page:

      (not
        [?b :block/left ?left]
        (self-or-alias ?left ?p)
      )
      
    • or use that rule to exclude the property block of the query page:

      :inputs [:query-page]
      
      ...
      
        :in $ ?q-p-name %
      
      ...
      
        (not
          [?b :block/left ?left]
          [?q-p :block/name ?q-p-name]
          (self-or-alias ?left ?q-p)
        )