Newlines are removed in journal queries

So just to briefly tell people my experience: I taught myself python before, and teach myself clojure + datalog with a very top-down approach. I barely touch on computer science. There’s probably a better way to implement what I want to do with my query, but I don’t know how, lol.

So my advanced query has a :view property that only returns(?) a single string (no [:divs] or [:spans], i kinda don’t know how that works). When my query is on a page, the newlines work, but when I put it in my journal queries via config.edn, the \n are non-existent (and the formatting’s pretty bad too since it’s really just one string). I just want my newline to not be erased.

The query basically gives me a visual representation of whether or not I tagged certain pages on a journal page for the past few days.

Here’s my query!

#+BEGIN_QUERY
{
:title [:b "habits"]
:query [
    :find ?habitname ?d ?boolemoji
    :in $ ?dstart ?dyesterday ?htagname
    :keys habitname d boolemoji
    :where
    [?habitpage :page/name ?habitname]
    [?habitpage :page/tags ?htag]
    [?htag :page/name ?htagname]
    [?jpage :block/journal-day ?d]
    [(>= ?d ?dstart)]
    [(<= ?d ?dyesterday)]
    [?jblock :block/page ?jpage]
    (or
        (and [?jblock :block/refs ?habitpage] [(str "🟩") ?boolemoji])
        (and (not [?jblock :block/refs ?habitpage]) [(str "🟥") ?boolemoji])
    )
]
:inputs [:-30d :today "habits"]
:view( fn[qmaps]
    (def habitmap {})
    (doseq [qmap qmaps]
        (when (not= "🟩" (get-in habitmap [ (get-in qmap [:habitname]) (get-in qmap [:d]) ]) )
            (def habitmap
                (assoc-in habitmap [ (get-in qmap [:habitname]) (get-in qmap [:d]) ]
                    (get-in qmap [:boolemoji])
                )
            )
        )
    )
    (def outputvect [])
    (doseq [habit (keys (into (sorted-map) habitmap) ) ]
        (def datemap (habitmap habit))
        (def datemap (into (sorted-map) datemap) )
        (def outputvect (conj outputvect (into (vector) (vals datemap)) ) )
        (def outputvect (conj outputvect " " habit "\n" ))
    )
    (def outputvect (flatten outputvect))
    (def output (clojure.string/join outputvect))
    (str output) ;; the sole output of this query
)
}
#+END_QUERY

on a page it looks like this:

query on page

but as a journal query, it looks like this:

query on journal

I also appreciate any suggestions to optimize(?) it more, thanks!

To detect the cause of the difference, should compare the resulting html+css of the two cases (journal and normal page), using Ctrl + Shift + i. Maybe character \n is actually present (it is invisible anyway) but not respected, because of the value of some css attribute, like white-space. Can you check whether it has the same value in both cases? Maybe some journal-specific theme or plugin has changed its value. Try also enforcing it, by adding inside file custom.css a rule like this one:

.custom-query-results {
    white-space: pre-wrap;
}
1 Like

Performance wise, try to get your data set as small as possible as quickly as possible. So if your input is ?htagname, use it immediately.

[?htag :page/name ?htagname]
[?habitpage :page/tags ?htag]
[?habitpage :page/name ?habitname]

This way the database isn’t first getting all pages and then narrowing it down to those with a certain tag. Instead the database right away can only get those pages with a certain tag.

Not too familiar with clojure to comment on the rest, so I’ll just say kudos :wink:

1 Like

Wow! Why did I never know that there was an inspect tool? I inspected it, and the newlines were present. So I added the rule you said and it is now fixed! :heart::heart::heart: Thank you!

'tis hella ugly

Anyways, since it is literally just a string, the formatting’s kinda ugly. Hey at least it’s readable now. This is probably kind of outside the scope of this thread now, but if anyone here knows some clojure+css, send help :sob::pray::pray::pray:

1 Like

For proper formatting, should definitely return a [:div] of [:span]s, i.e. hiccup instead of string. It is much easier than what you have already achieved in clojure. I would guess that:

  • your (def outputvect (conj outputvect " " habit "\n" ))
  • could become (def outputvect [:span (conj outputvect " " habit [:br] )])
  • then the last three statements could be a single [:div outputvect]

Without string manipulation, the code should be easier to work with, and could apply whatever styling to each individual element.

2 Likes