【已解决】我想创建一个过滤属性表,点击按钮便可以筛选出不同来源的文章

最后在 CHATGPT 的帮助下成功了,代码如下:

// Don't change this code

const {createButton} = app.plugins.plugins["buttons"]
const tp = app.plugins.plugins["templater-obsidian"].templater.current_functions_object

let current = dv.current()
let currentFile = app.vault.getAbstractFileByPath(current.file.path)

const changeFilter = async (buttonName) => {
  let propName = "filter-source"
  let val = buttonName === "All" ? null : buttonName
  app.fileManager.processFrontMatter(currentFile, (frontmatter) => {
    frontmatter[propName] = val
  })
}

const filterButton = async (name) => {
  createButton({
    app,
    el: this.container,
    args: {
      name: name
    },
    clickOverride: {
      click: async () => await changeFilter(name),
      params: []
    }
  })
}

const filterFunction = async () => {
  let filter = current["filter-source"]
  if (filter != null) {
    filteredPages = filteredPages.filter(p => p.source === filter)
  }
}

// Only edit the code below

let pages = dv.pages('"001 素材收藏"')
let filteredPages = [...pages]

// Apply filters
await filterFunction()

// Add filter buttons
await filterButton("All")
await filterButton("哔哩哔哩")
await filterButton("知乎")
await filterButton("小红书")

let headers = ["Name", "Source"]
let rows = filteredPages.map(p => [p.file.link, p.source])

dv.table(headers, rows)

1 个赞