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

Resolve assets in markdown content #106

Open
TixieSalander opened this issue Jun 5, 2020 · 103 comments
Open

Resolve assets in markdown content #106

TixieSalander opened this issue Jun 5, 2020 · 103 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@TixieSalander
Copy link

I would like to know if it's possible to have the webpack assets resolution inside of markdown content ? To display webpack handeled's images.

Reproduction link: https://codesandbox.io/s/dawn-snowflake-fv7l4?file=/content/post.md

  1. I have an image in the assets folder
  2. I link the image in the md file ![](~/assets/photo.jpg) (or <img src="~/assets/photo.jpg"> in html) the same way I would have in a component template

I'm not interested in putting files in the static folder since there is no compression and no hash for cache-busting.

@TixieSalander TixieSalander added the question Further information is requested label Jun 5, 2020
@dtmzr
Copy link

dtmzr commented Jun 6, 2020

As far as I know, this is currently not supported out of the box.

Maybe you can solve this with a component that dynamically loads the image for you, something like this?
https://codesandbox.io/s/nuxt-content-106-hwhbv?file=/content/post.md

@TixieSalander
Copy link
Author

Yeah I thought about a solution like that, but not really compliant with content management tools :/

@Atinux
Copy link
Member

Atinux commented Jun 8, 2020

Hi there,

The main issue here is that @nuxt/content is independent of Webpack, meaning that if you change anything in content/ you can directly run nuxt export (Nuxt 2.13) without having to wait for Webpack to build.

We are thinking of a way to support assets/images/ with Webpack dynamic import so it can still support nuxt export without having to build again (expect if you add an image in assets/images/)

@TixieSalander TixieSalander changed the title A way to resolve assets in markdown content Resolve assets in markdown content Jun 25, 2020
@TixieSalander
Copy link
Author

I see it was removed from the todo list. Any update on the situation of this feature request?

@MattFaz
Copy link

MattFaz commented Jul 10, 2020

Bumping, would love an update seeing as it has been removed from the To do?

@benjamincanac
Copy link
Member

Don't pay too much attention to the todo list, we use it internally to plan the next release.

We have considered this issue and will be working on it soon.

@Atinux
Copy link
Member

Atinux commented Jul 13, 2020

The tricky part is to not involve Webpack for it when using images since it would force a rebuild and loose te speed of content update.
This is why it is not an such easy task 😁

@dtmzr
Copy link

dtmzr commented Jul 13, 2020

@Atinux might overwriting the html img tag by a custom component with the same attributes work for this case?
Maybe even as an opt in? 🤔

@Atinux
Copy link
Member

Atinux commented Jul 13, 2020

Indeed your solution was quite good actually, if you can open a PR so we can work on it it would be great :)

@Spunkie
Copy link

Spunkie commented Jul 13, 2020

The tricky part is to not involve Webpack

@Atinux Maybe I'm missing something but why wouldn't I want webpack involved doing it's thing? Wouldn't I want webpack to rerun if content involving a webpack asset gets updated?

@Atinux
Copy link
Member

Atinux commented Jul 14, 2020

The idea behing content is that you can change your Markdown without rebuilding your entire Nuxt app, making it faster redeployment.

Tracking assets is a bit more tricky in that case since Content does not depend of Webpack at all.

@dtmzr
Copy link

dtmzr commented Jul 15, 2020

I fiddled around with it today and think I understood the way in which it could be implemented.

I would be really interested in your thoughts about the API design.
Should we support both, markdown images and HTML tags?

![image alt text](~/assets/localimage.jpg)
<img src="~/assets/localimage.jpg" alt="image alt text>

I have a few doubts about using the default tags and attributes because in this case, we would've to check if the developer is trying to load a local asset or remote image.
Maybe we could avoid this check by adding a custom attribute:

<img local-src="~/assets/localimage.jpg" alt="image alt text>

@dr1ss
Copy link

dr1ss commented Jul 16, 2020

Yeah both should be supported, this will give more control since you can't do basic things like center an image with markdown

@davydnorris
Copy link

Is it possible to store the images under the content directory itself instead of in the assets directory? Then you could create a structure like:

content/articles/my-new-article/index.md
content/articles/my-new-article/img/pic1.jpg
content/articles/my-new-article/img/pic2.jpg
content/articles/my-new-article/img/pic3.jpg

which would keep all the content together.

@dr1ss
Copy link

dr1ss commented Jul 20, 2020

Yes but this should not be the only solution.. again to have more control pointing to a specific folder is useful in case someone wants to use a CDN/reverse proxy to serve the images.

@MuhaddiMu
Copy link

MuhaddiMu commented Jul 25, 2020

@davydnorris suggestion was helpful to me

I created a global Image component to work around with that and stored my images inside the /content/blog/my-blog-slug/images/

<template>
  <img :src="imgSrc()" :alt="alt" />
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      required: true
    }
  },
  methods: {
    imgSrc() {
      try {
        return require(`~/content${this.src}`)
      } catch (error) {
        return null
      }
    }
  }
}
</script>

Make sure to register this component globally.
Inside components/global/Image.vue and set the components:true in nuxt.config.js

<image
          v-if="blog.Featured_Image"
          :src="blog.dir + '/images/' + blog.Featured_Image"
          :alt="blog.Title"
        >
</image>

Inside Markdown:

<image src="/blog/my-blog-slug/images/Scripts.png" alt="Scripts"></image>

I hope this will be helpful to someone. 🙌

@davydnorris
Copy link

I did the same thing but I am getting warnings when I compile the files of the form:

WARNING in ./content/blog/2020-02-15-blog-number-3/index.md 1:2
Module parse failed: Assigning to rvalue (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> ---
| title: 'Blog #3 Headline'
| description: Lorem Ipsum type stuff
 @ . sync ^\.\/content.*$ ./content/blog/2020-02-15-blog-number-3/index.md
 @ ./node_modules/babel-loader/lib??ref--2-0!./node_modules/vue-loader/lib??vue-loader-options!./components/BlogImage.vue?vue&type=script&lang=js&
 @ ./components/BlogImage.vue?vue&type=script&lang=js&
 @ ./components/BlogImage.vue
 @ ./node_modules/babel-loader/lib??ref--2-0!./node_modules/vue-loader/lib??vue-loader-options!./pages/blog/_slug.vue?vue&type=script&lang=js&
 @ ./pages/blog/_slug.vue?vue&type=script&lang=js&
 @ ./pages/blog/_slug.vue
 @ ./.nuxt/router.js
 @ ./.nuxt/index.js
 @ ./.nuxt/client.js
 @ multi ./.nuxt/client.js

I thought I could ignore the warning, however when I then try to run export or dev, the process hangs and never completes, and I see the above warning in the generated javascript instead of the actual content.

I'm not a webpack/babel config file expert, but it looks like the error is from webpack when it goes looking for the image files. I assume you could write a rule to exclude the md files somehow? But then would that mess up the content generation?

Any help would be greatly appreciated

@MuhaddiMu
Copy link

Unfortunately, this is the only solution worked for me. I have the same warnings. However, it doesn't affect the build process.

@davydnorris
Copy link

OK so I have made progress...

I couldn't work out why my images were being converted during the build but their paths weren't - then I realised that the images I happened to be using were IMG_xxxx.JPG and I had them in the code as IMG_xxxx.jpg. As soon as I made them exactly the same they all got picked up.

Still trying to get the custom image component inside the .md to work - @MuhaddiMu did you register yours as a global?

@MuhaddiMu
Copy link

@davydnorris My Image component is registered globally. I have a working example, you can check that too by cloning https://github.com/MuhaddiMu/Portfolio

@davydnorris
Copy link

Many thanks - I have now got it working 🥇

The components directive doesn't seem to do exactly what it says in the guide. I had set up a subdirectory specifically for blog components, called 'content', and had registered that in my nuxt config with:

components: [
  '~/components/content'
]

but it didn't seem to work. I then moved the components into the top level and set components:true, but still no luck.

It finally worked when I copied your example exactly and added a 'global' subdirectory with components:true.

Thank you for all your assistance!!

@djmtype
Copy link

djmtype commented Jul 29, 2020

Besides images used in HTML and markdown, what about images referenced in frontmatter? I've had to add these to static/images in order for this to work instead of assets.

img: /images/journey-ux.jpg
alt: Rocket soaring through space

@davydnorris
Copy link

This were actually the first ones I got to work. I used the same approach as above but could use a normal component in my _slug.vue and preview components:

<template>
  <div>
    <v-img
      :src="require(`@/content${post.dir}/img/${post.image}`)"
      :alt="post.title"
      height="400"
    />
    <h1>{{ post.title }}</h1>
    <p>
      {{ post.description }}
    </p>
    <nuxt-content
      :document="post"
    />
  </div>
</template>

<script>
export default {
  async asyncData ({ params, error, $content }) {
    try {
      const postPath = `/blog/${params.slug}`;
      const [post] = await $content('blog', { deep: true })
        .where({ dir: postPath })
        .fetch();
      return { post };
    } catch (err) {
      error({
        statusCode: 404,
        message: 'Page could not be found'
      });
    }
  }
};
</script>

in your case you've already included the image subdirectory so just adjust the template accordingly

@lucien144
Copy link

@davydnorris Regarding the Module parse failed: Assigning to rvalue (1:2) error. Using ignore-loader solved this for me:

// nuxt.config.js
// ...
build: {
	extend(config, { isDev, isClient }) {
		config.module.rules.push({
			test: /\.md$/i,
			loader: 'ignore-loader'
		})
	}
}
// ...

@davydnorris
Copy link

Many thanks @lucien144 - that's exactly what I was hoping someone would know! Will try that tomorrow

@TixieSalander
Copy link
Author

My original point was to be able using the webpack asset management for images (compression, cache-busting), while writing posts on content management tools. For that, it's needs to work with vanilla , and not some custom vuejs elements.

@ZeanQin
Copy link

ZeanQin commented Aug 13, 2020

Hi there,

The main issue here is that @nuxt/content is independent of Webpack, meaning that if you change anything in content/ you can directly run nuxt export (Nuxt 2.13) without having to wait for Webpack to build.

We are thinking of a way to support assets/images/ with Webpack dynamic import so it can still support nuxt export without having to build again (expect if you add an image in assets/images/)

What about linking to assets that are not images? For example, linking to a local pdf file with [A file](/assets/sample.pdf) in the markdown? It's not mentioned on this thread but I presume it's essentially the same issue?

@renoirb
Copy link

renoirb commented Sep 10, 2020

Yes. Assets from Markdown written with fully qualified URL ~/images/ to be resolvable either from Vue pages/ or components/ components as equal to from a Markdown file. I'm assuming we could have the place where to store those as configurable. Either from static, or from content, or assets. So we avoid having more than one notation for the same resource and the resource resolver in its context (remark's toTHML, or WebPack, or vite) to pick which ever its running.

@teegee
Copy link

teegee commented Oct 6, 2021

Has anyone overcome this? I also have images organized inside /content/posts/[slug] folders on my blog, and I'd love to leverage nuxt image for thumbnails as it's growing rather slow.

I gave up for now, even after I gave in and tried moving some images to assets/static and couldn't get anything to work. It's hard to find relevant examples of nuxt-image use, and I don't even know for if it is supposed to handle this use case in the first place (creating thumbnails somewhere in the static build or dev process).

Some replies earlier in this issue, someone posted a more custom image transform procedure, but that seems rather involved and I had hoped that's what nuxt-image could be used for. Another fallback would be to just generate thumbnails completely manually using a script, and put those in content as well.

I had a look at your github/blog. Looks nice, but it's super slow to load indeed, with that many images. I've starred it, in the hope that you'll find a brilliant solution :)

@brianssparetime
Copy link

Has anyone overcome this? I also have images organized inside /content/posts/[slug] folders on my blog, and I'd love to leverage nuxt image for thumbnails as it's growing rather slow.

I gave up for now, even after I gave in and tried moving some images to assets/static and couldn't get anything to work. It's hard to find relevant examples of nuxt-image use, and I don't even know for if it is supposed to handle this use case in the first place (creating thumbnails somewhere in the static build or dev process).

This was going to be my next move - try seeing if I can get it working out of /static or /assets and create a parallel folder structure there.

Some replies earlier in this issue, someone posted a more custom image transform procedure, but that seems rather involved and I had hoped that's what nuxt-image could be used for. Another fallback would be to just generate thumbnails completely manually using a script, and put those in content as well.

I saw the bit above, and I think that's probably over my head right now in terms of implementation. I agree this was exactly what I was hoping nuxt-image would do. I've also tried the Pivale image loader package, but that doesn't seem to be actively maintained and I couldn't get it to work or figure out why it wasn't.

At this point, I'll probably just write some python to generate the smaller sizes since I can do that pretty easily, but I'd love to have it done dynamically instead of having to do it each time. Perhaps I can integrate that into the build process somehow...

I had a look at your github/blog. Looks nice, but it's super slow to load indeed, with that many images. I've starred it, in the hope that you'll find a brilliant solution :)

Thanks!

@davydnorris
Copy link

One of the ones above is my implementation and, for me, it was super simple, and it's working really well. I use it for my blog on my site https://www.olivegroveit.com.au/blog.

All images for each blog entry are stored under the blog folder and the image component loads them. I've since enhanced the component and added sizing, justification and wrap props so that I can have images embedded in text that wraps around them

@brianssparetime
Copy link

One of the ones above is my implementation and, for me, it was super simple, and it's working really well. I use it for my blog on my site https://www.olivegroveit.com.au/blog.

All images for each blog entry are stored under the blog folder and the image component loads them. I've since enhanced the component and added sizing, justification and wrap props so that I can have images embedded in text that wraps around them

Thanks - I have something similar to this comment and what you describe here with my v-img component here.

While your site does look great (and I actually searched you to see if I could find your code), I couldn't tell if your solution addresses my problem.

I want to reduce load time by serving smaller image files. I'm not concerned with the presentation or resizing images in the sense of how they appear - that's already handled. Adding props just resizes the display of the image on the client side and I need to reduce it on the server before its sent to the client.

@davydnorris
Copy link

Yeah you're right - my approach uses a single image size but I do spend a bit of time to adjust them to something reasonable before I use them

@brianssparetime
Copy link

I have those too, but this will still operate by fetching a single (max-size) image and scaling it down. It would be nice if a few sizes are available, so that thumbnail images etc can download a much smaller file.

This raises a good point.... if I generate the thumbnail images some other way (either storing them in GH or having some script called on the server as part of the build process, which will make deployment... not fast), the next question would be how to provide those as a srcset. But most of the srcset packages I've seen also want to handle their own image creation.

I suppose the easy way would be to change my v-img component so that it links to the full size but has the img prop point to a reduced size but still not thumbnail, and have my postcard point to the thumbnail size and just do strip manip on the slug+base image name to get there.

@brianssparetime
Copy link

Yeah you're right - my approach uses a single image size but I do spend a bit of time to adjust them to something reasonable before I use them

Thanks - I appreciate your suggestions, but I'd like to avoid doing that if I can.

One other question for you though.... it looks like you've figured how to set the og:image header to the correct deployed url for your image. I'm struggling with figuring out how to do that. Any advice?

@davydnorris
Copy link

This is how I did it:

    head () {
      return {
        title: this.post.title,
        meta: [
          { hid: 'description', name: 'description', content: this.post.description },
          { hid: 'og:title', property: 'og:title', content: this.post.title },
          { hid: 'og:description', property: 'og:description', content: this.post.description },
          { hid: 'og:type', property: 'og:type', content: 'article' },
          { hid: 'og:image', property: 'og:image', content: 'https://www.olivegroveit.com.au' + require(`@/content${this.post.dir}/img/${this.post.image}`) },
          { hid: 'og:url', property: 'og:url', content: 'https://www.olivegroveit.com.au' + this.$route.fullPath },
          { hid: 'twitter:image:alt', name: 'twitter:image:alt', content: this.post.title + ' hero image' }
        ]
      };
    },

@brianssparetime
Copy link

This is how I did it:

    head () {
      return {
        title: this.post.title,
        meta: [
          { hid: 'description', name: 'description', content: this.post.description },
          { hid: 'og:title', property: 'og:title', content: this.post.title },
          { hid: 'og:description', property: 'og:description', content: this.post.description },
          { hid: 'og:type', property: 'og:type', content: 'article' },
          { hid: 'og:image', property: 'og:image', content: 'https://www.olivegroveit.com.au' + require(`@/content${this.post.dir}/img/${this.post.image}`) },
          { hid: 'og:url', property: 'og:url', content: 'https://www.olivegroveit.com.au' + this.$route.fullPath },
          { hid: 'twitter:image:alt', name: 'twitter:image:alt', content: this.post.title + ' hero image' }
        ]
      };
    },

Thank you! That's been bugging me almost as much as the image loading.

@teegee
Copy link

teegee commented Oct 6, 2021

I have those too, but this will still operate by fetching a single (max-size) image and scaling it down. It would be nice if a few sizes are available, so that thumbnail images etc can download a much smaller file.

This raises a good point.... if I generate the thumbnail images some other way (either storing them in GH or having some script called on the server as part of the build process, which will make deployment... not fast), the next question would be how to provide those as a srcset. But most of the srcset packages I've seen also want to handle their own image creation.

I suppose the easy way would be to change my v-img component so that it links to the full size but has the img prop point to a reduced size but still not thumbnail, and have my postcard point to the thumbnail size and just do strip manip on the slug+base image name to get there.

I just searched youtube for nuxt image and watched one by 'Coding with Ben'. There's a few other videos as well, and it does look like nuxt-image should do exactly the sort of resizing you (and I) want. Time for some more experimenting..

@brianssparetime
Copy link

brianssparetime commented Oct 6, 2021

I just searched youtube for nuxt image and watched one by 'Coding with Ben'. There's a few other videos as well, and it does look like nuxt-image should do exactly the sort of resizing you (and I) want. Time for some more experimenting.

In the meantime, I banged out a python script, added a requirements.txt and runtime.txt to set up python requirements and python version respectively, and prepended to my build command in netlify "python3 ./gen_tn.py && ". It's now generating thumbnails on deploy.

But I haven't rewritten the v-img component to use the new files yet. I'm now wondering if I can avoid having to keep the thumbnails locally in dev - somehow give require the generated thumbnail location in production, but just use the fullsize when in dev....

EDIT:

I consider this just a temporary hack - I'd still very much like to do this in a real way using nuxt-image if possible. I just don't have the knowledge/time to deep dive. If someone else gets it to work and has code to share, I'd love to see it!

@brianssparetime
Copy link

But I haven't rewritten the v-img component to use the new files yet. I'm now wondering if I can avoid having to keep the thumbnails locally in dev - somehow give require the generated thumbnail location in production, but just use the fullsize when in dev....

So now I have, which brings me to webpack errors when I try to rewrite the require(...) inside vimg so that it points to the generated thumnail files instead of the original fullrez.

I think I need to learn more about webpack's module search, but any advice? And an extra thank you for all the help you've provided already!

@davydnorris
Copy link

davydnorris commented Oct 6, 2021

If you have a look way back up the thread, there were some changes we had to make to get webpack to play nice with the images under the content - this may help you work it out. The other thing I found was that the image name and its extension were case sensitive, so that's something you may need to check too

// nuxt.config.js
// ...
build: {
	extend(config, { isDev, isClient }) {
		config.module.rules.push({
			test: /\.md$/i,
			loader: 'ignore-loader'
		})
	}
}
// ...

@brianssparetime
Copy link

If you have a look way back up the thread, there were some changes we had to make to get webpack to play nice with the images under the content - this may help you work it out. The other thing I found was that the image name and its extension were case sensitive, so that's something you may need to check too

Thanks!

I've got this (or something very close to it in my nuxt.config.js. And I thought about the case sensitive bit - I'm only testing with a single post where everything's lower case.

But I hadn't noticed that GH was hiding quite a few replies above in a fold - I think I have some more to go read about the rehype approach. And this comment was interesting - I've tried now fixing the suffix extension as a literal to see if that made webpack happy, but not yet.

return require(`~/content${this.dirp}/.imgs/${this.src.replace(/\.[^/.]+$/, '')}_large.png`)

Also, I think I may have been reading this thread when I first wrote the vimg component a while back - it's suspiciously similar to what you were doing earlier. If so, I can maybe find a way to eliminate the ${this.dirp} hack which I use to make sure vimg has the slug (aka the post folder inside content) by specifying a relative path in the markdown (e.g. "./image.jpg") instead of passing it through a prop with dirp. (and yes I named it that because it's a derpy solution). That same comment above has some possibilities for this, but I'm hesitant to get into rehype because I know so little and my odds of making that work seem slim if I don't know enough to overcome this.

@brianssparetime
Copy link

Ok. I think I've got it.

Turns out the problem was with the dot in the name I chose for the sub folder (.imgs). If I changed that, everything works.

Code in the SO question and pull request on my repo summarizing the final changes.

Thanks again to everyone.

And would still like to be using nuxt-image instead :)

@jasenmichael
Copy link

jasenmichael commented Dec 30, 2021

I just copy all images to the static directory at build time, and use relative paths for images in markdown, this way the content folder could even be a separate repo, and the images will still show in that gh repo.

at the top of your nuxt.config.js

const fs = require('fs')
const path = require('path')
const copyContentImages = (src, dest, ignore = []) => {
  const exists = fs.existsSync(src)
  const stats = exists && fs.statSync(src)
  const isDirectory = exists && stats.isDirectory()
  if (isDirectory) {
    if (!fs.existsSync(dest) || !fs.statSync(src).isDirectory()) {
      fs.mkdirSync(dest)
    }
    fs.readdirSync(src).forEach((childItemName) => {
      copyContentImages(
        path.join(src, childItemName),
        path.join(dest, childItemName),
        ignore
      )
    })
  } else {
    const ext = path.extname(src)
    if (!ignore.includes(ext)) {
      fs.copyFileSync(src, dest)
    }
  }
}
copyContentImages('content', 'static', ['.md'])

export default {
    // the-rest-of-nuxt-config.........
}

@ManasMadrecha
Copy link

ManasMadrecha commented Jul 9, 2022

@jasenmichael

  1. Does this approach of yours to copy + paste the images in the static folder preserves the creationTime of the images? If not, can we think of some way to do that?
  2. And are the images simply saved inside the one static folder or are they saved in recursive folders inside static folder based on how they were structured inside folders in content folder?
  3. Also, can you figure out a way to ignore some images which are present in the content folder, but nowhere used in any .md file? Because generating them and uploading them would be unnecessary; they are simply meant to be kept as draft local images.

@brianssparetime
Copy link

I tried something similar (I'd link it but after searching for 10 min I can't find it in my repo) where I wrote a python script that would, on deployment, essentially mirror the /content folder structure inside /static (moving only files fitting an image filename pattern, leaving md and other things alone).

I found it ran too slowly - copying several gigs around caused my build process on Netlify to timeout.

You might want to run a short test to make sure you won't have the same problem if you take this approach.

@tobiasdiez
Copy link

I'm a bit lost in the long discussion here: what is the currently recommended workaround for Nuxt 3 + Content v2?

@ManasMadrecha
Copy link

@tobiasdiez

  1. Try this Nuxt Content Images #1152 (comment). It assumes you have images alongside markdown files. So, you can use node's fs.cp to copy images into public folder.

  2. Also, you can use nuxt-img module component (currently v1 for nuxt3) without actually writing nuxt-img for every image in markdown by following this: nuxt-image integration #911 (comment)

@Nugine
Copy link

Nugine commented Mar 18, 2023

VitePress can reference static assets in markdown files.

@sealedsins
Copy link

sealedsins commented Apr 1, 2023

After playing with this issue for a while I found a relatively clean solution for this issue. First of all, you need a helper composable to be able to resolve assets dynamically.

const assets = import.meta.glob('~/assets/**/*', {
	import: 'default',
	eager: true,
});

export const useAsset = (src: string) => {
	return assets[src]?.toString() || src;
};

Then you have to replace a default image component from nuxt/content with a custom one that uses this composable. Save it as ~/components/content/prose-img.vue to make it work.

<script setup lang="ts">
defineProps<{
	src: string;
	width?: string | number;
	height?: string | number;
	alt?: string;
}>();
</script>

<template>
	<img v-bind="$props" :src="useAsset(src)" />
</template>

Now you could use Nuxt assets as native Markdown images. This works well for me.

![Test Image](/assets/test.png)

@ManasMadrecha
Copy link

ManasMadrecha commented Apr 1, 2023

@sealedsins Nice solution.

Can we modify it so that we can use images stored anywhere, e.g., even in the content folder or git repo (using multi sources feature of the Module) (and not just assets folder), and that too with relative links?

I tend to place my images inside the same folder as the blogspot md file.

@sealedsins
Copy link

sealedsins commented Apr 2, 2023

@ManasMadrecha I tried changing import glob from ~/assets/**/* to my content folder path and it seemed to work at first... But then it started causing some weird HMR bugs (new assets was not available until server restart).

Probably that was caused by a non-typical structure of my project folder (I have content folder outside of my src folder), so you could try that method yourself and see if it works for you ;)

@davestewart
Copy link
Contributor

davestewart commented Apr 4, 2023

As of Nuxt 3, you can use the Nuxt Content Assets module:

Nuxt Content Assets logo

You can save your images along with your markdown content, then at runtime, the module copies images to a build-time folder, replaces any matching relative paths with public ones, and serves the images using Nitro.

At build time, images are copied to the final build output.

It even supports relative paths in frontmatter so you can get funky with custom components, etc.

Zero config, and no more workarounds!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests