Last active
April 24, 2025 16:56
-
-
Save Moyf/90ec15eb31b8adb43313152f56943038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example | |
/** | |
```dataviewjs | |
dv.view("mediaViewInFolder", { | |
folder: "_global/emotion", | |
showTitle: true, | |
title: "My Emotion", | |
pagination: 9, | |
addAlt: true, | |
exclude: ["bad"], | |
size: "small" // small, medium, large | |
}) | |
``` | |
*/ | |
// 脚本内容 | |
const currentPage = dv.current(); | |
const addAlt = input?.addAlt ?? true; | |
// 如果传入了指定的文件夹,则使用对应文件夹路径 | |
const targetFolder = input?.folder || currentPage.file.folder; | |
const showTitle = input?.showTitle ?? false; | |
const title = input?.title ?? targetFolder.split(/[\\/]/).filter(Boolean).at(-1) ?? "媒体一览"; | |
const pagination = input?.pagination ?? 9; | |
const size = input?.size ?? ""; | |
const extensions = input?.extensions ?? ["png", "jpg", "jpeg", "gif", "webp", "mp4"]; | |
// 排除所有文件名中含有对应字符串的文件 | |
const exclude = input?.exclude ?? []; | |
// 要递归多层 children,太麻烦了,算叻 | |
// const tFolder = app.vault.getFolderByPath(targetFolder); | |
// console.log( tFolder.children); | |
const images = app.vault.getFiles(targetFolder) | |
.filter(file => file.path.startsWith (targetFolder) && extensions.includes(file.extension) && !exclude.some(ex => file.basename.includes(ex))); | |
// console.log(images); | |
let data = images.map(img => [ | |
img.path, | |
img.basename | |
]) | |
.sort(); | |
//.sort((a, b) => parseInt(a[0].match(/p(\d+)/)?.[1]??0, 10) - parseInt(b[0].match(/p(\d+)/)?.[1]??1, 10)); | |
const result = `\`\`\`gallery\n${showTitle ? `title:${title}\n` : ''}\n${size?`size: ${size}\n` : ''}\n${data.map(row => `${addAlt ? `alt:${row[1]}\n` : ''}![[${row[0]}]]`).join("\n")}\npagination:${pagination}\n\`\`\`` | |
dv.paragraph(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment