recommend: ⭐⭐⭐ udpateDate: 2025-11-10

githubUrl: "https://github.com/ChenZhu-Xie/xczphysics_SilverBullet/blob/main/CONFIG/Add_Fields_for_Obj/Last_Opened-Page/Query.md"

${getVisitHistory()}

3rd try

function getVisitHistory()
  return query[
    -- from editor.getRecentlyOpenedPages "page"
    from editor.getRecentlyOpenedPages()
    where _.lastOpened
    select {ref=_.ref, lastVisit=os.date("%Y-%m-%d %H:%M:%S", _.lastOpened/1000)} 
    order by _.lastOpened desc
](
    -- from editor.getRecentlyOpenedPages "page"
    from editor.getRecentlyOpenedPages()
    where _.lastOpened
    select {ref=_.ref, lastVisit=os.date("%Y-%m-%d %H:%M:%S", _.lastOpened/1000)} 
    order by _.lastOpened desc
)
end

2nd try

${utilities.RecentlyOpenedPages(12)} 1. expose page picker s hidden attr lastopened #community #silverbullet 2. https://chatgpt.com/share/6908ca51-ebf4-8010-9900-25e55eacaa6a

utilities = utilities or {}

function utilities.RecentlyOpenedPages(limit)
  local raw = editor.getRecentlyOpenedPages()
  local pages = {}

  -- 如果 raw 已经是数组(ipairs 有元素),优先按数组拷贝
  for _, v in ipairs(raw) do
    pages[#pages + 1] = v
  end
  -- 如果没拿到(可能 raw 是 map),用 pairs 收集所有值
  if #pages == 0 then
    for _, v in pairs(raw) do
      pages[#pages + 1] = v
    end
  end

  -- 安全排序:把 lastOpened 转为 number,缺失或无法转为 0
  table.sort(pages, function(a, b)
    local aa = tonumber(a and a.lastOpened) or 0
    local bb = tonumber(b and b.lastOpened) or 0
    return aa > bb  -- 降序:最近的在前
  end)

  local lines = {}
  local n = math.min(limit or 5, #pages)
  for i = 1, n do
    local p = pages[i] or {}
    local last = tonumber(p.lastOpened) or 0
    local ts = math.floor(last / 1000) -- 毫秒 -> 秒
    table.insert(lines, string.format("- [%s](%s) (%s)",
      p.name or "?", os.date("%Y-%m-%d %H:%M", ts)))
  end
  return table.concat(lines, "\n")
end

1 st try

  1. https://chatgpt.com/share/68fa59ac-2e88-8010-aa2c-12021dda94fb
function utilities.RecentlyOpenedPages(limit)
  local pages = editor.getRecentlyOpenedPages()
  table.sort(pages, function(a,b)
    return (a.lastOpened or 0) > (b.lastOpened or 0)
  end)
  local lines, n = {}, math.min(limit or 5, #pages)
  for i = 1, n do
    local p = pages[i]
    local ts = math.floor((p.lastOpened or 0) / 1000)
    table.insert(lines, string.format("- [%s](%s) (%s)",
      p.name or "?", os.date("%Y-%m-%d %H:%M", ts)))
  end
  return table.concat(lines, "\n")
end