Dataview - can I collect screenshots as well?

What I’m trying to do

Hi all! I’m a complete beginner with Obsidian and Dataview so I apologize if this question sounds naive and there is an easy solution to my issue, that I’m not aware of.
I’m using Dataview to collect snippets of text from different notes across my whole collection of notes that have a specific header. Here is the code I use, I got it from a different post here in the forum.

const headings = ['Example']
const pages = dv.pages()
const output = {}
headings.forEach(x => output[x] = [])
for (const page of pages) {
  const file = app.vault.getAbstractFileByPath(page.file.path)
  const contents = await app.vault.read(file)
  for (let heading of headings) {
    heading = heading.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')
    const regex = `(^|\n)#+ ${heading}\r?\n(.*?)(\n#+ |\n---|$)`
    for (const block of contents.match(new RegExp(regex, 'isg')) || []) {
      const match = block.match(new RegExp(regex, 'is'))
      output[heading].push({
        title: file.basename,
        text: match[2].trim()
      })
    }
  }
}
Object.keys(output).forEach(heading => {
  dv.header(1, heading)
  output[heading].forEach(entry => {
    dv.header(4, entry.title)
    dv.paragraph(entry.text)
  })
})

The code works very well to get all the written parts of the notes. But the notes also contain screenshots that I would like to have in my ‘collection’-notes. However, with my code I am only getting the written parts and this instead of the picture itself (example): Screenshot 2023-07-27 at 09.44.32.png

Is there another way to embed screenshots and pictures or something else I can do so I can solve this issue?

Thank you!

Hi! Thanks for your answer, but I’m not sure I understand what you mean. I embed the screenshots in the original notes as ![[Screenshot_example]]. But when I use the above code snippet to recall them in another note they just show up as IMG000.jpg.
I’m not sure where you meant to put the brackets.

Oh hello my old code snippet :stuck_out_tongue:

The problem is that Dataview doesn’t support embeds, because Obsidian doesn’t expose a method for it.

There’s a copy/paste method you could use, which might be perfectly fine depending on how often your data is updated.

Change the snippet like this:

const headings = ['Example']
const pages = dv.pages()
const output = {}
headings.forEach(x => output[x] = [])
for (const page of pages) {
  const file = app.vault.getAbstractFileByPath(page.file.path)
  const contents = await app.vault.read(file)
  for (let heading of headings) {
    heading = heading.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')
    const regex = `(^|\n)#+ ${heading}\r?\n(.*?)(\n#+ |\n---|$)`
    for (const block of contents.match(new RegExp(regex, 'isg')) || []) {
      const match = block.match(new RegExp(regex, 'is'))
      output[heading].push({
        title: file.name,
        path: file.path,
        heading: heading
      })
    }
  }
}
Object.keys(output).forEach(heading => {
  const sections = output[heading].map(x => `![[${x.path}#${x.heading}]]`).join('\n')
  dv.paragraph('```\n' + sections + '\n```')
})

Then when Dataview processes it, it will return the needed embed Markdown:

![[Daily notes/2022-12-04.md#Example section]]
![[Daily notes/2022-12-06.md#Example section]]

You just copy and paste that into your note, and all the sections will appear with all their images and formatting. (Dataview provides a nice “Copy” button when you hover over the result which makes the process slightly easier.)

Hi!
Haha, your original code has made my life so much easier already, so thank you very much!
I’ve tried the new code you provided, and it works perfectly! Thank you again!
Is there something that could be added to this code so I include the name of the note in the snippet it provides me? Above the example header?

Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.