如何查看最近写了哪些笔记呢?

如何查看最近写了哪些笔记呢?

需求

我会把每天的笔记按分类,写在不同的文档里。想有一个自动的视图,能看到今天写了哪些笔记。

比如今天看到一个软件不错,就记录在【windows > 软件使用】页面里。就希望自动更新的视图里能展示:

2024-07-27

  • windows > 软件使用

    • 写的笔记内容

应该通过什么功能、插件来实现呢?或者写代码如何实现

求大佬解答

实现

如果想自定义,还得 js,这里建议安装 data-query 插件

image.png

然后就可以配合 SQL 实现这种效果吧

image

代码如下:

//!js
// 过滤笔记名,不支持模糊匹配
const boxName = ""
//过滤路径,注意这个路径不包括笔记名,支持模糊匹配,相当于SQL里的like
const path = ""
const getBoxByName = (name)=> {
  return Array.from(document.querySelectorAll("ul.b3-list[data-url]"))
    .filter(book=>book.querySelector("li[data-type='navigation-root'] span.b3-list-item__text")?.innerText === name)[0]?.getAttribute("data-url") || ""
}
const query = async (path, boxName)=>{
  // 数据查询
  let dq = DV.DataQuery()
  // sql 内容可以按需查询
  path = path ? `and hpath like '%${path}%'` : ""
  const box = boxName ? `and box = '${getBoxByName(boxName)}'` : ""
  let sql = `select * from blocks where type IN ('l', 'b', 'p') ${path} ${box} and markdown <> '' and parent_id in (select id from blocks where type = 'd') order by updated desc`
  let blocks = await dq.sql(sql).query()

  // 返回列表块
  // 引用块其实就是一个 html 片段,返回的字符串列表,只需要返回 html 片段即可达到生成双向链接类似的效果
  let dv = new DV(protyle,item,top)
  let days = []
  let boxes = {}
  blocks.map(b=>{
    // 获取笔记名
    const box = b.sqlData.box
    if(!boxes[box]){
      boxes[box] = document.querySelector("ul.b3-list[data-url='"+box+"'] li[data-type='navigation-root'] span.b3-list-item__text")?.innerText || "";
    }
    // 格式化日期
    let day = b.sqlData.updated.substring(0,8)
    day = day = day.substr(0, 4) + "-" + day.substr(4, 2) + "-" + day.substr(6, 2)
    if(!days.includes(day)) {
      days.push(day);
      dv.addElement(`<div data-type="strong" style="padding: .3em 4px;margin: 4px 0;font-size: 2em;font-weight: 600;line-height: 1.5em;">${day}</div>`)
    }
    // 生成块内容
    dv.addElement(`<div style="color:#aaa;">${boxes[box]}${b.sqlData.hpath}</div>`)
    dv.addElement(`<div data-type="block-ref" data-subtype="d" data-id="${b.sqlData.id}" style="border-top:1px solid #888;padding:15px 0;">${b.blockItem.block.content}</div>`)
  })
  dv.show()
}
return query(path, boxName)

以上代码参考了 mohuishou 大佬分享的脚本:https://github.com/zxhd863943427/siyuan-plugin-data-query/issues/2

使用方法

输入嵌入块 {{}} 然后把上述代码粘贴进去即可。

t5

这里仅仅是抛砖引玉,更个性化的需求可以根据自身情况进行修改。

文章来源:https://ld246.com/article/1722038210145

image.png

留下你的脚步
推荐阅读