Macros and Commands - let's share

do you use custom macros and commands ? let’s share !

I’ll start:

making it easier to add iframes:

in my workflow I often use iframes to embed web references in logseq pages :
Logseq_bjarke_ingels_20220808_navvFHIV95
then I can quickly pull text or images

  1. define a macro named ‘iframe’ in config.edn
 :macros {
 "iframe" "<iframe src=\"$1\" style=\"width:calc(100% - 4px);height:650px\"></iframe>" 
}
  1. then define related commands :
:commands [
  ["iframe" [[:editor/input "{{{iframe }}}" {:backward-pos 3}]]]
  ["iframe-wikipedia" [[:editor/input "{{{iframe https://en.wikipedia.org/wiki/}}}" {:backward-pos 3}]]]
  ["iframe-google" [[:editor/input "{{{iframe https://www.google.com/search?q=}}}" {:backward-pos 3}]]]
  ["iframe-google-images" [[:editor/input "{{{iframe https://www.google.com/search?tbm=isch&q=}}}" {:backward-pos 3}]]]
 ]

**usage: ** now everytime I use slash command /iframe I can quickly embed a url or I can use /iframe-wikipedia | /iframe-google | /iframe-google-images followed by a search term and get embedded results from which I can pull data without switching to a browser window

17 Likes

Hello. I am new to LoqSeq, jumping from Roam.
Where do you place this code?
thanks in advance

Welcome to the community @DavidPlummerMD! :wave:

Depending on how technically proficient you are, I’d warn against using macros if you’re new to Logseq.

As the OP points out, you’d need to change the config.edn file. If you don’t know what that is, I suggest you first get some more experience with simpler stuff like a daily template: How to Set Up an Automated Daily Template in Logseq

It also helps if you get experience with queries, starting with simple queries and then slowly move up to Datalog queries. See #learning-sprints:queries for learning resources.

2 Likes

I am brand new to macro and commands. But I really wanted status labels and this post helped me achieve that.

  • {{pill grey,inactive}} {{pill blue,info}} {{pill green,success}} {{pill yellow,warning}} {{pill red,important}}
    
  • image

config.edn

 :macros {
     "pill" "<span class=\"pill pill-$1\">$2</span>"
 }

 :commands [
    ["pill-red" [[:editor/input "{{pill red,}}" {:backward-pos 2}]]]
    ["pill-yellow" [[:editor/input "{{pill yellow,}}" {:backward-pos 2}]]]
    ["pill-blue" [[:editor/input "{{pill blue,}}" {:backward-pos 2}]]]
    ["pill-green" [[:editor/input "{{pill green,}}" {:backward-pos 2}]]]
    ["pill-grey" [[:editor/input "{{pill grey,}}" {:backward-pos 2}]]]
 ]

custom.css

html[data-theme=dark] {
  --red: #cb4b16;
  --orange: #ffb86c;
  --yellow: #b58902;
  --blue:#656aa4;
  --green: #219d85;
  --grey: #586e75;
}

span.pill {
  border-radius: var(--ls-border-radius-low);
  background-color: var(--ls-page-inline-code-bg-color);
  border: 1px solid;
  padding-left: 0.3em;
  padding-right: 0.3em;
}

span.pill-red {
  border-color: var(--red);
  color: var(--red);
}

span.pill-yellow {
  border-color: var(--yellow);
  color: var(--yellow);
}

span.pill-blue {
  border-color: var(--blue);
  color: var(--blue);
}

span.pill-green {
  border-color: var(--green);
  color: var(--green);
}

span.pill-grey {
  border-color: var(--grey);
  color: var(--grey);
}

span.inline div {
  display:inline;
}

Note: The last rule for span.inline div is because Logseq will wrap the macros in divs that are blocks, otherwise forcing each label onto a new line.


The following are things I learnt that others who are new to this might benefit from.

  • You can press Ctrl + Shift + I to open an inspector that lets you examine how Logseq is rendering your macro.
  • To define multiple macros, put each on a new line – coming from PHP, this is a strange format to me :slight_smile:
  • The :backward-pos value in the command tells Logseq where to position the cursor after selecting the command.

What I still don’t know is if there is a difference between the using two or three pairs of { to wrap a macro. I.e. {{macro}} vs {{{macro}}}

13 Likes

Thank you @cannibalox for this inspiration.

I was wondering if it’s possible to render a template via macro directly. That would expand the functionality like supertags in tana.

I’m not a coder nor have any background knowledge of coding; just a PKM enthusiast trying to optimize and enjoy daily note taking processes.

Seems like there is some discussion on same lines here

How can I create nested blocks with a macro or a command?
Is it possible to have a macro that expands into multiple blocks possibly nested?
Or a command that creates more blocks?

I would also appreciate pointers for: documentation or source code files defining :editor/input and :backward-pos

are you looking for templates ? what are you trying to achieve with macros ?
https://docs.logseq.com/#/page/templates

:editor/input will write the string to the block
:backward-pos is a way to move the caret x characters backwards to wait for user input at the correct location once a macros has been entered, I don’t have links to docs, but you should into the examples in config.edn

Hi Cannibalox, thank you for your answer.
I was thinking about an easier method of writing question-answer LogSeq flashcards.

I was wondering if macros could be used to do something like:

  1. Type the following in a LogSeq block: {{makecard question answer}}

  2. Press enter

  3. Have the macro to expand into the following:
    image

I’m aware of templates but they don’t seem to accept arguments (i.e. I can’t input “quesiton” nor “answer” to a template), if they did they would be perfect for this I think.

Do you think I can somehow use :editor/new-block in a macro to achieve this?

1 Like

afaik, you can’t create flashcards using macros (not sure we can create sub bullet via macros, and I’m not a flashcards user myself).
that said, I would recommend a text expander like Espanso, Autohotkey or fastkeys… although not dedicated to logseq, these tools work great in conjunction with logseq (you can search the discord for scripts or use-cases examples).
alternatively, you can try logseq plugins like : Smart Blocks, Power Blocks or Full House Templates that should theorically achieve what you want.

1 Like

Hi all, thanks for these macros, pretty interesting. Have any of you figured out a way to produce a macro which turns dynamic variables into strings? My goal is to build a template that creates a series of pages based on the page the template is currently built on. Thanks!

You totally can achieve this with 🏛 Full House Templates plugin

Demo

2023-09-06 12.46.23

@jpb Probably your task could be solved with my plugin too. But I need more details to be sure.

1 Like

Hi, you might want to look into this:

If I am not mistaken, you can have exactly what you want → turn dynamic variables into editable strings, stripping the “dynamic” from the Macro with @mentaloid’s code here;

1 Like

RIght on the money, thanks

1 Like

Where did you learn about :editor/input and :backward-pos ?

It really helps with even simpler things like:

:commands [
        ["macro" [[:editor/input "{{}}" {:backward-pos 2}]]]
      
 :macros {
        "t" "<%time%> "
        "ramen" "<%time%> Ate 🍜"}

or just

:commands [
        ["ramen" "{{ramen}}"]]

Hi All,

where can I find documentation to learn how to use commands and macros in the config.edn file?

thanks

1 Like

I use this to emulate missing checkbox support.

:macros {
   "cx" "[:input {:type \"checkbox\" :checked :1}]"
   "c" "[:input {:type \"checkbox\"}]"
 }
1 Like

There is a section that covers Macros briefly, referenced in the Templates page listed in docs.logseq.com. I have not seen anything on how to build custom commands in the official docs. But I do not fully grasp their usefulness over templates. They cannot have Dynamic variables like %time% can they? And they are limited to one block without properties only.

They differ from Templates in that they are defined in config.edn. As are Macros. Templates are or can be defined in a page.

I imagine that command/macros could have the benefit to be faster to call with few types. instead of /template and then template name.
Do you know any solution to call the template faster?

Try 🏛 Full House Templates plugin.

  1. It allows you to insert templates faster via UI with fuzzy search:
    ⌘T → s → ↩︎ (x3 key presses, but depends on all templates list)
    2023-11-17 03.54.54

  2. And it allows you to render frequently used templates by creating a :command:

     :commands
     [
     	["psy client session                                             [.]"
             [[:editor/input "{{renderer :template, psy → session, :page [[клиент ]], :online}}" {:backward-pos 13}]] ],
     ]
    

    /. → ↩︎ (x3 key presses)
    2023-11-17 03.43.35

3 Likes