Nuxt.js: How to include local images in Markdown blog content?

Viewed 2658

I created a blog in Nuxt.js which uses Markdown for my articles. When writing my first article, I realized I can't include images in my markdown article from my assets folder. It only works if it's a link like the example below:

Markdown Image:

alt text

How can insert an image in Nuxt.js Markdown from this location? assets/images/blog/trees.png

3 Answers

place your images in static folder with this path :

static/images/img1.png

In markdown file, use this syntax:

![image alt text](/images/img1.png)

In vue files, use like this :

<template>
  <img src="/images/img1.png" />
</template>

or for use with content :

1- in content/articles/new-post.md :

---
title: Title
description: This is description
img: /images/img1.png
alt: Article 1
---

## Example

2- in vue file (pages/index.vue) :

<template>
  <div>
    <div v-for="article of articles" :key="article.slug">
      <h1>{{ article.title }}</h1>
      <img
        v-if="article.img"
        class="h-48 xxlmin:w-1/2 xxlmax:w-full object-cover"
        :src="article.img"
      />
    </div>
  </div>
</template>

<script>
export default {
  async asyncData({ $content, params }) {
    const articles = await $content('articles', params.slug)
      .only(['title', 'description', 'img'])
      .fetch()

    return {
      articles,
    }
  },
}
</script>

notes :

  1. place your images in static folder for example (static/images/*)
  2. use '/' in start of addresses true => /images/img1.png false => images/img1.png
Related