How to port hooks.newPage to v2?

How to port hooks.newPage in v2?

This is my v1 template hooks.

---
description: "Permanent Note template"
tags: template
hooks.newPage:
  forPrefix: "Permanents/"
  suggestedName: "Permanents/"
  confirmName: true
frontmatter:
  tags: permanent
  dateCreated: "{{today}}"
---

# {{@page.name}}

Now, I want to port this to v2.

I can use editor:pageCreating as Zef suggested.

event.listen {
  name = "editor:pageCreating",
  run = function(e)
    if not e.data.name:startsWith("Permanents/") then
      return
    end
    return {
      text = "Template text here",
      perm = "rw"
    }
  end
}

But how to cramp that frontmatter and it’s body in text?

I use hooks.newPage extensively. In v1, this was trivial, so losing it without a clear v2 example makes migration a bit rough.

Thanks for SB :cupcake:

The Library/Std/Page Templates implementation is already pretty similar. If you look at that and you have some experience with space lua, you should be able to make a drop in replacement for the hook.

But as far as your question goes … you just put it in there … that’s all. The text property is going to be the content of the page 1:1.

you just put it in there

Thanks. then it sould be something like this.

event.listen {
  name = "editor:pageCreating",
  run = function(e)
    if not e.data.name:startsWith("Permanents/") then
      return
    end

    local today = os.date("%Y-%m-%d")
    local fullPath  = editor.getCurrentPage()
    local title
    for part in fullPath:gmatch("[^/]+") do
      title = part -- will end up with the last one
    end

    local text = table.concat({
      "---",
      "created: \"" .. today .. "\"",
      "---",
      "",
      "# " .. title
    }, "\n")

    return {
      text = text,
      perm = "rw"
    }
  end
}
1 Like