Lua comand to create a new page within the current directory

Some more simple functionality. A command for creating a new page within the same directory as the page you are currently editing. It creates a page called “/current/dir/New” which you can then quickly edit.

sub=sub or {}

-- Return the current directory path
function sub.dir()
  dir = editor.getCurrentPageMeta()
  -- Find last occurence of /
  i = string.find(dir.name, "/[^/]*$")
  path = string.sub(dir.name,1,i)
  return path
end

command.define {
  name = "Create Subpage",
  run = function()
    newpath = sub.dir()
    editor.navigate(newpath..'New')
  end
}

Trigger via an in-page button {[Create Subpage]} Or from within your top menu via the settings file. Note, I pair this with a page delete command to help me quickly manage pages.

ActionButtons:
- icon: file-plus
  command: "{[Create Subpage]}"
- icon: delete
  command: "{[Page: Delete]}"
1 Like

This is cool. You could add editor.prompt in the command to ask user for the name of new page.

On the side note. You can also press space or shift+space in the page selector (when the input is empty) it will try to prefill the input based on the page you are now. :slight_smile:

command.define {
  name = "new ",
  key = "Alt-Ctrl-n",
  run = function()
    local pageName=editor.prompt("page name",editor.getCurrentPage().."/")
    editor.navigate(pageName)
  end
}

another way to do it

I didn’t know shift+space action
I use this page to know shortcuts (Keyboard Shortcuts) but shift+space is missing.
Where can i find an exhaustive list of shortcuts? @marad

Nice addition(s), thankyou!

@marad @malys @zef do you know if there is a way to place the cursor at the end of the editor.prompt() input field?

This is the more compact reworked version. I currently prefer new as the default page name. Brilliant tips from both of you!

command.define {
  name = "Create Subpage",
  run = function()
    local name = editor.getCurrentPage()
    local i = string.find(name, "/[^/]*$")-- find last /
    local path = string.sub(name,1,i)..'new'
    local pagename = editor.prompt("page name",path) 
    editor.navigate(pagename)
  end
}
1 Like

I don’t know but i’m interested to know :slight_smile:

1 Like

That page lists all keyboard shortcuts active in the regular editor, not in the filter box UI (for command palette, page navigator). Page Picker lists the Shift-Space thing, although I should note that I removed it in v2 because I hit it by accident a lot lot. It would need a different shortcut to come back.

1 Like

FWIW, here is what I’ve been using for a while.
This also works when, for example, a page is created when a root page like index is opened:

Create page:

command.define {
  name = "Page: Create Page",
  run = function()
    local current = editor.getCurrentPage()
    local path = string.gsub(current, "/*[^/]+$", "")
    if path ~= "" then path = path .. "/" end
    local name = editor.prompt("Enter name of new page", path)
    if not name then return else editor.navigate(name) end
  end
}

And my Create subpage. Very similar to what has been posted before, but I don’t really prefer one-liners:

command.define {
  name = "Page: Create Subpage",
  run = function()
    local current = editor.getCurrentPage()
    local name = editor.prompt("Enter name of new subpage", current .. "/")
    if not name then return else editor.navigate(name) end
  end
}

Also looking for a solution to move the cursor to the end! Pressing the end key every time is getting annoying.

2 Likes