Convenience commands for page renaming

Some small convenience commands to rename either branch or leaf of the page path, saving you the rather small effort of typing out the full path when renaming:

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
}

Both commands just pass the hard work to the renamePageCommand function from the index plug, so references should stay valid after renaming :smiley:
Import from here.

5 Likes