Skip to content

Instantly share code, notes, and snippets.

@22rw
Created May 18, 2025 21:36
Show Gist options
  • Save 22rw/d44c5b1aa25fe72b6ef63ccb094ac642 to your computer and use it in GitHub Desktop.
Save 22rw/d44c5b1aa25fe72b6ef63ccb094ac642 to your computer and use it in GitHub Desktop.

#meta

command.define {
  name = "Page: Rename Leaf",
  run = function()
    local page_name = editor.getCurrentPage()
    local l_start, l_end = page_name:find("[^/]+$")
    local path = page_name:sub(1, l_start - 1)
    local old_name = page_name:sub(l_start, l_end)
    local new_name = editor.prompt("Rename "..old_name.." to:", old_name)
    if new_name and #new_name != 0 then
      system.invokeFunction("index.renamePageCommand", 
        {oldPage = path..old_name, page = path..new_name}
      )
    end
  end
}

command.define {
  name = "Page: Rename Branch",
  run = function()
    local page_name = editor.getCurrentPage()
    local l_start, l_end = page_name:find("[^/]+$")
    local old_path = page_name:sub(1, math.max(l_start - 2, 0))
    local name = page_name:sub(l_start, l_end)
    if not #old_path > 0 then
      editor.flashNotification("Branch length is 0!", "error")
      return
    end
    local new_path = editor.prompt("Move "..name.." from "..old_path.." to:", old_path)
    if new_path then
      if #new_path > 0 then
        new_path = new_path..'/'
      end
      system.invokeFunction("index.renamePageCommand", 
        {oldPage = old_path..'/'..name, page = new_path..name}
      )
    end
  end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment