```dataviewjs
// 获取所有带有#笔记标签的文档
let pages = dv.pages('#你的标签')
.sort(file => file.file.mtime, 'desc')
.filter(file => file.file);
// 异步获取文件内容
async function getFileContent(file) {
try {
if (file.content) return file.content;
const content = await dv.io.load(file.path);
return content || "无法获取内容";
} catch (e) {
console.error("读取文件内容失败:", e);
return "无法获取内容";
}
}
// 创建瀑布流容器
const masonryContainer = dv.el('div', '', {cls: 'masonry-container'});
masonryContainer.style.columnCount = '2'; // 两列布局
masonryContainer.style.columnGap = '15px'; // 列间距
// 创建瀑布流卡片
function createMasonryCard(file, content) {
const card = dv.el('div', '', {cls: 'masonry-card'});
card.style.breakInside = 'avoid'; // 防止卡片跨列断裂
card.style.boxSizing = 'border-box';
card.style.padding = '15px';
card.style.marginBottom = '15px'; // 卡片间距
card.style.borderRadius = '8px';
card.style.backgroundColor = 'var(--background-primary)';
card.style.border = '1px solid var(--background-modifier-border)';
card.style.display = 'inline-block'; // 重要:瀑布流布局需要
card.style.width = '100%'; // 占满容器宽度
// 添加文档标题和链接
const title = dv.el('h3', dv.fileLink(file.path, false, file.name));
title.style.marginTop = '0';
title.style.marginBottom = '10px';
card.appendChild(title);
// 添加预览内容
let preview = content;
if (preview.length > 200) {
preview = preview.substring(0, 200) + '...';
}
const previewDiv = dv.el('div', preview);
previewDiv.style.lineHeight = '1.5';
previewDiv.style.color = 'var(--text-normal)';
card.appendChild(previewDiv);
return card;
}
// 异步渲染所有文档
(async function() {
for (let page of pages) {
if (!page?.file) continue;
const content = await getFileContent(page.file);
const card = createMasonryCard(page.file, content);
masonryContainer.appendChild(card);
}
})();
需要安装dataview 并开启js
下面还有两段跟标题有关的瀑布流