Rename embedded assets

This is:

Usage

  • At the end of file preload.js, normally inside ...\Logseq\app-0.9.13\resources\app\js, add these lines:
    contextBridge.exposeInMainWorld('renameasset', (path, oldName, newName, cb)=>{
      const assetsPath = path + "/assets/"
      fs.rename(assetsPath + oldName, assetsPath + newName, cb)
    })
    
    • This gets affected by updates.
  • Add a macro inside file config.edn, inside macros{}:
:rename-asset "<div class='kit' data-kit='renameasset' data-new-name='$1' />"
  • This code requires having kits inside file custom.js
  • Inside page RenameAsset in Logseq, put the following code in a javascript code-block:
const LS = logseq.api
const Msg = logseq.Module.Msg
const rename = window.renameasset
if (!rename) return Msg.error("missing renameasset")

logseq.kits.setStatic(function renameasset(div){
    const blockId = div.closest(".ls-block").getAttribute("blockid")
    const block = LS.get_block(blockId)
    const parent = LS.get_block(block.parent.id)
    const content = parent.content
    const index = content.lastIndexOf("/")
    const oldName = content.slice(index + 1, content.indexOf(")"))
    const newName = (div.dataset.newName !== "$1") ? div.dataset.newName
        : oldName.slice(0, oldName.indexOf("_")) + oldName.slice(oldName.lastIndexOf("."))
    const newContent = content.slice(0, index + 1) + newName + ")"
    const path = LS.get_current_graph().path
    rename(path, oldName, newName, (err)=>{
        if (err) return Msg.error(err.message)

        LS.update_block(parent.uuid, newContent)
        LS.remove_block(block.uuid)
        Msg.success("done")
    })
})
  • Under any (*) embedded asset (looks like ![text](link) ) add a temporary child-block:
    • Put the following content:
      • If it is auto-named, to fix its name:
      {{rename-asset}}
      
      • To use a different name:
      {{rename-asset somename.ext}}
      
    • This should:
      • rename the file
        • doesn’t check for same-named file
      • rename the link
      • delete the temporary block
    • (*) The parsing is simplistic, so any extra content in the asset’s block may interfere.