Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for images in fields #58

Open
SkepticMystic opened this issue Mar 25, 2021 · 5 comments
Open

Support for images in fields #58

SkepticMystic opened this issue Mar 25, 2021 · 5 comments

Comments

@SkepticMystic
Copy link
Contributor

@SkepticMystic SkepticMystic commented Mar 25, 2021

I am not sure if this is possible, but maybe it can be done.
I would like to be able to link to a remote or local image in a yaml field, and have dataview render that image in preview mode.

So something like:

---
image: ![](URLtoimage.com/image.jpg)
# OR
image: [](URLtoimage.com/image.jpg)
# OR
image: URLtoimage.com/image.jpg
---

Where you could then have a table calling for the image field

table image from ""

I know yaml won't render the image in preview mode, but perhaps dataview could have an image type which passes the image path to <img src="{{path}}"> or something.

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Mar 30, 2021

Yes, that's possible. I'm wondering what the most convienent syntax would be - probably just the markdown link syntax (![]...), as you suggest?

@SkepticMystic
Copy link
Contributor Author

@SkepticMystic SkepticMystic commented Mar 30, 2021

For external images, I think [ ]( ) works. Ideally one shouldn't have to put an ! to make it work, but that's small.

Would this feature be able to work with local images to? I guess the img tag can take an internal src? Because then Wikilink images might work too ![[image.png]]

@symph0nic
Copy link

@symph0nic symph0nic commented Apr 12, 2021

I like this idea too - using ![image] to show local images in a result table would be fab.

@M-bot M-bot changed the title Feature Request: Support for images in fields Support for images in fields Jul 22, 2021
@calpa
Copy link

@calpa calpa commented Sep 7, 2021

I like this idea

@nkoder
Copy link

@nkoder nkoder commented Sep 9, 2021

Interesting! It might make my custom JS code obsolete.

Below I share my handmade image-from-frontmatter rendering approach for some "how do they overcome of such feature" context 😄


Currently, in YAML front matter I have e.g.:

thumbnail: "084 Interstellar Backdoor (x1).png"

then in dataviewjs I have:

const {DataviewHelpers} = customJS;

dv.table(
	[
		"thumbnail",
		"something else",
	],
	dv.pages(`"${dv.current().file.folder}"`)
		.filter(page => page.file.name !== dv.current().file.name)
		.map(page => [
			renderThumbnailOf(page),
			"other value",
		])
);

function renderThumbnailOf(page) {
	const path = `${page.file.folder}/${page.thumbnail}`;
	const containerStyle = `
		height: 100px;
		width: 100px;
	`;
	return `
		<div style="${containerStyle}">
			${DataviewHelpers.renderThumbnail({
				thumbnailPath: path,
				type: "bg",
			})}
		</div>
	`.trim();
}

and then, last but not least, shared helper function:

class DataviewHelpers {
    renderThumbnail({thumbnailPath, type}) {
        const abstractFile = app.vault.getAbstractFileByPath(thumbnailPath);
        const resourcePath = abstractFile
            ? app.vault.getResourcePath(abstractFile)
            : null;

        if (!resourcePath) return null;

        if (type === "bg") {
            const style = `
                width: 100%;
                height: 100%;
                background-image: url('${resourcePath}');
                background-repeat: no-repeat;
                background-position-x: center;
                background-position-y: center;
                background-size: contain;
            `;
            return `<div style="${style}"></div>`;
        }

        if (type === "img") {
            const style = `
                width: 100%;
                object-fit: contain;
            `;
            return `<img style="${style}" src="${resourcePath}">` ;
        }

        throw Error(`Thumbnail type "${type}" is not supported.`)
    }
}

Another use case I have is with multiple thumbnails (I render them all inside a single table cell), like:

thumbnails:
  - "066 Grumman F-11 Tiger - F11 Keycap (social media).png"
  - "067 Kung Fury Time Travel - Enter and Backslash Pipe Keycaps (social media).png"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants