If each project has its own page with a category, moving dozens of pages into matching folders by hand is slow. This adds a command that moves every project page into a folder named after its category. It uses SilverBullet's own rename, so links to the pages keep working.

Before you start

Each project page needs a tag and a category in its frontmatter, and must sit in one projects folder:

---
tags: project
category: Travel
---

Setup

  1. Open your CONFIG page, or any page you keep code on.
  2. Add a space-lua code block (three backticks followed by space-lua) and paste this code inside it.
  3. Change the two settings at the top if your tag or folder has another name. Keep the slash at the end of the folder.
  4. Run the command System: Reload.
-- Settings: the tag your project pages carry, and the folder they live in
FILE_BY_CATEGORY_TAG = "project"
FILE_BY_CATEGORY_FOLDER = "Projects/"

command.define {
  name = "Projects: File by Category",
  run = function()
    local folder = FILE_BY_CATEGORY_FOLDER
    local pages = query[
      from p = index.pages(FILE_BY_CATEGORY_TAG)
      where p.tag == "page" and p.category
        and string.sub(p.name, 1, string.len(folder)) == folder
    ](<
      from p = index.pages(FILE_BY_CATEGORY_TAG)
      where p.tag == "page" and p.category
        and string.sub(p.name, 1, string.len(folder)) == folder
    >)
    local moved = 0
    for _, p in ipairs(pages) do
      local leaf = string.match(p.name, "([^/]+)$")
      local target = folder .. tostring(p.category) .. "/" .. leaf
      if p.name ~= target then
        system.invokeFunction("index.renamePageCommand", {oldPage = p.name, page = target})
        moved = moved + 1
      end
    end
    editor.flashNotification("Moved " .. moved .. " project pages")
  end
}

Use

  • Run the command Projects: File by Category.
  • A page with category: Travel moves to Projects/Travel/ plus its page name.
  • A page whose category changed moves out of its old category folder into the new one.
  • Pages already in the right folder, and pages with no category, stay where they are.
  • Run it again whenever you add projects or change a category.