Macro to auto-embed a block to the page of a tag

  • Inspiration
  • Usage
    • Typing something like {{emref tagname}} should:
      • replace it with #tagname
      • append an embedding of the current block at the end of page tagname
  • Preparation
    • Add a macro inside file config.edn , inside macros{} :
      :emref "<span class='kit' data-kit='embedreference' data-name='$1'>$1</span>"
      
    • The code below requires having kits inside file custom.js .
    • Inside page EmbedReference in Logseq, put the following code in a javascript code-block:
      logseq.kits.setStatic(function embedreference(span){
          const blockId = span.closest(".ls-block").getAttribute("blockid")
          const content = logseq.api.get_block(blockId).content
          const macroStart = content.indexOf("{{" + span.closest(".macro").dataset.macroName)
          const macroEnd = content.indexOf("}}", macroStart) + 2
          const tagname = span.dataset.name
          logseq.api.append_block_in_page(tagname, "{{embed ((" + blockId + "))}}")
          logseq.api.update_block(blockId, content.slice(0, macroStart) + "#" + tagname + content.slice(macroEnd))
      });