Wilson@思源

目 录

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

需求

我会把每天的笔记按分类,写在不同的文档里。想有一个自动的视图,能看到今天写了哪些笔记。
比如今天看到一个软件不错,就记录在【windows > 软件使用】页面里。就希望自动更新的视图里能展示:
2024-07-27
windows > 软件使用
写的笔记内容
应该通过什么功能、插件来实现呢?或者写代码如何实现
求大佬解答

实现

如果想自定义,还得 js,这里建议安装 data-query 插件
然后就可以配合 SQL 实现这种效果吧
代码如下:
js
//!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(`
${day}
`) } // 生成块内容 dv.addElement(`
${boxes[box]}${b.sqlData.hpath}
`) dv.addElement(`
${b.blockItem.block.content}
`) }) dv.show() } return query(path, boxName)
以上代码参考了 mohuishou 大佬分享的脚本:https://github.com/zxhd863943427/siyuan-plugin-data-query/issues/2

使用方法

输入嵌入块 {{}} 然后把上述代码粘贴进去即可。
这里仅仅是抛砖引玉,更个性化的需求可以根据自身情况进行修改。
文章来源:https://ld246.com/article/1722038210145