#网站/数字花园

const folder = "个人网站/数字花园";

// 1. 拉取所有符合条件的页面,排除“我的随笔”,并按创建时间倒序
let pages = dv.pages(`"${folder}"`)
  .filter(p => p.file.name !== "我的数字花园")
  .filter(p => p["dg-publish"] === true)
  .sort(p => p.file.ctime, 'desc');

// 2. 按 年-月 分组
let monthGroups = {};
for (let p of pages) {
  let dt = p.file.ctime;
  let key = `${dt.year}-${dt.month}`;
  if (!monthGroups[key]) monthGroups[key] = [];
  monthGroups[key].push(p);
}

// 3. 输出
for (let monthKey of Object.keys(monthGroups)) {
  // 月标题
  let [year, month] = monthKey.split('-');
  dv.paragraph(`**${year} 年 ${month} 月:**`);
  
  let groupPages = monthGroups[monthKey];
  // 确定当天列表的顺序(按创建时间排序已在 pages 中)
  let days = [...new Set(groupPages.map(p => p.file.ctime.day))];
  
  for (let day of days) {
    dv.paragraph(`\n${day} 号:`);
    // 当天的文章,按最后修改时间倒序
    let dayPages = groupPages
      .filter(p => p.file.ctime.day === day)
      .sort((a, b) => b.file.mtime.ts - a.file.mtime.ts);
    
    for (let p of dayPages) {
      let filepath = p.file.path.replace(/\$/, "");
      let link = `《${p.file.name}》`;
      let m = p.file.mtime;
      let modified = `${m.year}.${m.month}.${m.day}`;
      dv.paragraph(`${link} 『最后一次修改时间:${modified}』`);
    }
  }
  
  dv.paragraph(""); // 月份之间空行
}