I created custom.js and placed the js code in it as you instructed (" Put the following code in file custom.js (create inside folder logseq if missing):"). I haven’t bothered with the custom.css or the macro buttons because I will not use them, at least no plans atm.
I have reloaded Logseq and I haven’t seen any “kits ok” mssage. I see in the js code that it is the last command in the code and should appear when custom.js is loaded by logseq? Where should I look for it?
I can tell something didn’t work because I have created a macro:
which doesn’t get replaced with the time at the calling moment, but it always updates when i click that line and it enters edit mode and I leave edit mode (basically refreshing the macro);
What am I doing wrong? :-/
PS: I tried the example in your kits link:
I am not sure why nothing happens. This might be the same reason the macro isn’t replacing itself with the text …
Can this be because I have unpacked the exe to extract the insides so that I can use Logseq as a portable app without being forced to install it on a computer? I am using, basically, "Logseq-win-x64-0.8.9\Logseq-0.8.9-full.nupkg\lib\net45" so I don’t know what other tools am I missing that the .js would rely on to function as expected …
However, I don’t think there is anything else in the exe file that the .js functionality would depend on that isn’t in the net45 folder I extracted on a thumbdrive.
I see the following in Dev Tools > Console:
Thanks @mentaloid for sticking with this. If Logseq would provide a PortableApp I wouldn’t have to do stuff like this (it’s quie annoying to do this every update - i have updated 0.9.17 to 0.9.17 on this ocasion ).
The good:
I now see the “kits ok” message at Logseq startup;
It is not replaced with the time but rather functions the old way (same as :time "<%time%>" macro would), by updating this time every time i refresh (click on it to see the “{{time}}” text)
definition isn’t applied somehow, maybe the expandmacro function in expandmacro.md is not reached/applied… (is there a js command I can add to this function to show something on screen or whatever just so I can see if it’s actually run? - I tried with adding console.log("Expanding macro: " + macroName); to the function but I get nothing at the Logseq Dev Tools Console…)
The weird thing is that I have the markdown file under /pages (tried even copy/pasting it into the /logseq folder and I get a warning that I already have a page with that name so Logseq clearly finds/sees it). It contains your updated function:
Hmm… copied that function inside custom.js and all works ok
It somehow finds the /pages/expandmacro.md because, if I remove that file, I get a different first error message: instead of ran 0 codeblock(s) I get page not found:
ok, I created the expandmacro.md outside Logseq and had the dash for the block missing. I opened the page from Logseq and it added the dash (created the block containing the function) but I am sill having the same error.
This is what I get after I open the expandmacro page in logseq and I switch to the scratchpad page where I run my tests:
This is exactly what I was thinking, to have macros that are not dynamic but static -replacing themselves at runtime is something Logseq Maros shoudl have as a parameter or something. I wonder why nobody has posted any use cases …
Man, it’s no easy task to get uploads less than 4MB in here. I wanted for a quite some long time to showcase my need for macros that replace themselves, so here is my use case:
Logseq Dynamic Variables are available in Templates, Queries and Macros. They are not available to Custom Commands nor they are available to Journal Blocks/Pages. I wanted to make them available to daily usage in Blocks as well as in Block Templates created with Custom Commands;
Macros are the obvious solution to be able to insert Dynamic Variables in Blocks. But there is one fat problem. Macros are Dynamic themselves and they update every time the page is loaded. So I was stuck until this solution -luckily- became available; @Mentaloid
Now I can have Macros that “replace themselves with their result”. Awesome. So I created a few Macros that access Dynamic Variables:
:macros {
;; make Dynamic Variables accessible by running [custom macros that replace themselves with their value on first run](https://discuss.logseq.com/t/make-custom-macros-that-replace-themselves-with-their-value-on-first-run/20967/1)
:current-page "<div class='kit' data-kit='expandmacro'><% current page %></div>"
:time "<div class='kit' data-kit='expandmacro'><%time%></div>"
:today "<div class='kit' data-kit='expandmacro'><%today%></div>"
}
Finally, I added the folowing Macro to be able to access the natural language syntax for dates, ex: <%next Tuesday%>:
and it gets replaced with the appropriate date just as I would do by using the /date Command and then selecting, with mouse, the day I am interested in.
Here is a screen recording showcasing it (you might want to right click > “open image in new Tab” for larger viewpoint)…
Now, to be able to pre-fill some properties in Block Templates (yes, these are Templates for a single Block that I can easily access by typing “<template_name” instead of going the route → “/templ”{ENTER} + Template_Name{Enter}, which would ruin my Quick Capture experience):
I use a lot of Single-Block-“Templates” for every input i do in Logseq. I rarely do multi-block templates (the daily journal template is one of those, for example). I totally dislike to have to use /template {Enter}, then template name {Enter} and would rather do <template-name{Enter}. I also design my “single-block-teplates” (actually Custom Commands) to leave the cursor exactly where I feel it’s the most logical for quick capture of data (something Templates can’t do). And, because Custom Commands can’t use Dynamic Variables, I had to somehow make them work via Macros. But Normal Macros won’t do, as they are dynamic and change their result at every refresh. So @mentaloid 's solution is gold to my specific use case.
I’ve been looking for this since literally today and found today. This seems like it should become an official feature of Logseq. Thanks for the discussion here, very clear.
My usage is just writing complex templated, long winded latex equations for category theory. You can use the Katex macros but it requires custom.js. Going this far I found this post, which only required a little more and made the argument clear how it could be done. I just did this.
@mentaloid Is there a way to circumvent macros not being parsed inside Named Links?
If I do Ctrl+L I get “[]()” and the cursor is placed inside the square brackets, where I write my name for the link, then I go in between the round brackets and I call my macro “{{var next monday}}”. When I exit the Edit Mode for the Block I get nothing, the “{{…}}” is not parsed.
PS: I would extensively use this when writing days of week in my language: [Luni]([[Monday, 01.04.2024]]) but it’s impossible atm. I was wondering if you can see a solution for that by using Macros (it can be done by doing [Luni]( /date) and picking a date from the popup )…
I understand that and like the concept very mch. I was simply asking if configuring Synthesis would render expandmacro superseeded and I should use further evalonce instead of it, as it does the same thing and more.
Regarding Synthesis, can we maybe have a new thread under Synthesis - Logseq to gather practical usecases so it’s easier to grasp for people like me (non-programmers)?