Embed images with dataviewjs

Hi,

I want to embed images with a certain text-string in their filenames by a dataview query. I already described my use case in this thread.

Lessons learned: better use dataviewjs instead of dataview for that task

I learned, that dataview only works with fields and metadata, written in .md-files. Therefore dataview is only capable to embed images, if we have added a field to a .md-file with the path of the image. Then dataview can work with that field and embed the image.

For me that kind of workaround (described in that thread) does not work, because I don’t want to query for fix file names or paths. I want to query for all filenames, which contain a certain string and embed them.

=> Therefore dataviewjs is probably the adequate tool to accomplish this.

My dataviewjs steps - help needed

I already can query for all .jpg-files in a “Media” folder and with a string of the current filename, with that code:

```dataviewjs  
const jpgFiles = app.vault.getFiles().filter(file => file.extension === 'jpg' && file.path.includes('Media') && file.name.includes(dv.current().file.name))  
dv.list(jpgFiles.map(file => dv.fileLink(file.path)));  
```

but I cannot yet embed theses images. I tried this code (used a snippet from this thread):

```dataviewjs  
const jpgFiles = app.vault.getFiles().filter(file => file.extension === 'jpg' && file.path.includes('Media') && file.name.includes(dv.current().file.name))  
dv.paragraph(`![[${jpgFiles}]]`);  
```

… what do I have to make dataviewjs embedding these images?

Thanks for any help!

kind regards,
Silias

1 Like

You’ve got an array of files (a “list” of files, or a “set” of files), but you’re trying to output the whole set as a single image.

Do this:

```dataviewjs  
const jpgFiles = app.vault.getFiles().filter(file => file.extension === 'jpg' && file.path.includes('Media') && file.name.includes(dv.current().file.name))  

jpgFiles.forEach(file => dv.paragraph(`![[${file.path}]]`))
```
2 Likes

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