Add support for importing images
henniaufmrenni opened this issue ยท 18 comments
One nice feat about mdx-bundler
is that it allows for image bundling through import statements.
https://github.com/kentcdodds/mdx-bundler#image-bundling
Implementing this would be especially helpful for the very limited next/image
, because there wouldn't be the need for width/size specification and it would provide a blur hash automatically.
Great point! Will investigate. Would you expect the images become part of the deployment or be uploaded somewhere else?
I personally thought for my use case abound bundling it with the MDX so it becomes part of the deployment.
I've done a quick investigation and I think there is a really great opportunity for Contentlayer to make working with images as part of your content much easier - especially in Next.js applications where for the <Image />
component you need to provide the size dimensions.
Based on the provided image-related instructions of mdx-bundler I've done a quick experiment and got things to work using the dataurl
"image embedding". For most cases this is probably not a great solution but it's already something that could be done in the meanwhile by users waiting for this feature.
I'm really excited about diving into this feature but it's just a matter about finding enough time for it as it's quite a serious time investment.
For my site, I have several products each with an image set. So I need access to a list for an image-carousel. I don't feel like my use-case is an edge-case.
I would really like to see the ability to just get a manifest for all images in my /public
directory. So like import { allImages } from '.contentlayer/data';
would give you just the paths for images from /public
. Right now, I'm just using a prebuild script to generate imageManifest.json
files. This works but feels like the kind of hackery that contentlayer
could help me get out of doing for an individual project.
I ended up using a computed field, parsing the doc, copying to public, and then having a map. Then wrapped my rendering document in a context provider, Then for my img
component replace it with a custom image component that utilizes next/image
and the size analyzed at build time.
{ './local.jpg': { size: {width:100,height:100}, url: '/location-in-public-[hash].jpg } }
I once upon a time used https://github.com/sergioramos/remark-copy-linked-files but usually in running stuff via mdx-bundler
the VFile doesn't have a file path.
Ideally if we doing a content layer from local files, then the cwd
would be set correctly and theoretically contentlayer
could avoid in some cases complex image management and leave it to remark plugins. But just a thought
An additional thought on this topic:
I'd like to be able to import SVGs in a Next.js app via Contentlayer and embed them instead of rendering an <img/>
with a data URL in order to apply styles (mainly fill color) to the SVGs. I've been thinking whether it's possible to leverage SVGR to convert an SVG to a React component in Contentlayer. Perhaps a field definition could look like this:
{
// ...
fields: {
// ...
icon: { type: "svg", required: true },
}
}
and the data received in the Next.js app would be a React component of the SVG that can be rendered like any other component, the only difference is that it isn't imported via import ... from ...
but provided by Contentlayer.
What do you think?
Those are all super interesting ideas. I'm hoping to get to working this feature in the near future but most likely still a few months away.
Being able to import images and having them included in the bundle as assets is something I would like. I currently achieve this in Next.js using @mdx-js/loader
and remark-mdx-images
and it allows me to use the Next.js <Image/>
component easily.
Just wanted to say +1 and also wondering if the new Next.js next/future/image
will make any of this work easier.
Will this support an array of images? In some cases if I wanted to add a carousel of images surrounding a blog post. I cannot seem to tell if the current proposal would allow for that.
Will this support an array of images? In some cases if I wanted to add a carousel of images surrounding a blog post. I cannot seem to tell if the current proposal would allow for that.
Good question. Probably not yet supported right now but will be supported in the future!
I think the issue is that the underlying mdx-bundler
is using esbuild
, which doesn't know the webpack config from next/image
. So, you can't simply put import img from "sample.png"
in your MDX file and call it a day. @mdx-js/loader
works fine here because of that.
Did Contentlayer already implement this feature? I see the release notes for 0.2.7 mention relative url for cover_image in markdown frontmatter. Is this not the same request in terms of development? So is it possible to import making use of this?