Is it possible to add a block ref in Markdown format " [Refferenced-Block-Title]( ((Block-Ref)) ) " by JavaScript Macro?

Can use the following kit:

  • Add a macro inside file config.edn , inside macros{} :
    :ref "<span class='kit' data-kit='blockref' data-ref='$1'>[ref]( $1 )</span>"
    
  • Create a page with the kit’s name
    • in this case BlockRef
  • Put inside a javascript code-block with this code:
    logseq.kits.setStatic(function blockref(span){
        const uuid = span.dataset.ref.slice(2, -2)
        const block = logseq.api.get_block(uuid)
        if (!block) return
    
        var title = block.content.slice(0, 64)
        const index = title.indexOf("\n")
        if (index > -1) title = title.slice(0, index)
    
        const blockId = span.closest(".ls-block").getAttribute("blockid")
        const sourceContent = logseq.api.get_block(blockId).content
        const ref = span.dataset.ref
        const target = "{{ref " + ref + "}}"
        const markdown = "[" + title + "]( " + ref + " )"
        logseq.api.update_block(blockId, sourceContent.replace(target, markdown))
    })
    
  • Now when typing something like {{ref pasted-ref}}
    • one space only
    • check target in the code
  • If the ref’s block is found, it will be replaced with [block-title]( pasted-ref )
    • check markdown in the code
    • for the desired title’s maximum length, adjust number 64 in the code
2 Likes