Make custom macros that replace themselves with their value on first run

Create a relatively simple kit like this:

  • Add kits to file custom.js if not already there.
  • Pick names:
    • a name for the kit
      • in this example expandmacro
    • a name for each special macro (i.e. a macro that runs only the first time)
      • in this example mymacro
  • Use carefully the picked names to define each special macro inside macros{} of file config.edn
    • wrap the normal output of each macro within the div of the kit
    • in this example:
:mymacro "<div class='kit' data-kit='expandmacro'>use $1 here</div>"
  • Create a page with the kit’s name
    • in this example ExpandMacro
  • Put inside a javascript code-block with this code:
logseq.kits.setStatic(function expandmacro(div){
    const blockId = div.closest(".ls-block").getAttribute("blockid");
    const content = logseq.api.get_block(blockId).content;
    const macroStart = content.indexOf("{{" + div.closest(".macro").dataset.macroName);
    const macroEnd = content.indexOf("}}", macroStart) + 2;
    logseq.api.update_block(blockId, content.slice(0, macroStart) + div.innerHTML + content.slice(macroEnd));
});

Now whenever typing a special macro (e.g.: {{mymacro something}} ), it should get replaced (e.g. with use something here ).

Consider posting here your case of using this functionality, to inspire other users.

Hi,

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:

 :time "<div class='kit' data-kit='expandmacro'> <%time%></div>"

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:
image

I am not sure why nothing happens. This might be the same reason the macro isn’t replacing itself with the text …

You are the first user reporting an issue with kits. Here are some steps:

  • Ensure that custom.js is allowed to run.
  • Ensure that you have copied the whole code inside custom.js
    • Indeed css and other macros are optional.
      • You may want to temporarily add these too, to help with investigation.
  • But if you don’t get “kits ok” when Logseq starts, nothing is expected to work.
    • This is a pop-up at the top-right of the screen.
    • Indeed this is triggered by the last command.
    • To investigate why it doesn’t get executed:
      • open the console with Ctrl+ Shift + i
      • check for any errors there
        • preferably right after starting Logseq, to have a clean console
      • report back with any findings