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

8 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.

1 Like

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}}}

3 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