Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image with caption - vuepress

I'm trying to create a component in vuepress to display an image with its caption. When I hardcode the image path, the image appears but this way I will not have a reusable component. I already try with props, but it doesn't work either.

Here is how I already tried:

<template>
    <figure>
      <!-- <img src="../../guides/contribute/images/typora-tela1.png" alt=""/> -->
      <img :src="imagesrc" alt=""/>
      <figcaption>Legenda: {{ caption }} - {{ src }}</figcaption>
    </figure>
</template>
<script>
...
props: ['src'],
computed: {
    imagesrc () {
      return '../../guides/contribute/images/' + this.src // this.image
    }
  }
...
</script>

On my README.md I call the component like this: <captioned-image src="filename.png" caption="Caption Example" /> but the image doesn't appear.

How can I fix this issue? Is it possible to do this with markdown only?

like image 728
Vitor Carvalho Avatar asked Sep 14 '18 16:09

Vitor Carvalho


People also ask

How to add caption to images in WordPress?

Take your mouse over to an image you want to edit, and you will notice that a few links would appear below it. You need to click on Edit Image link. This will open image in the WordPress image editor screen. You can add caption to your image here.

How do I add images to a vuepress document?

The easiest way is to the images at /docs/.vuepress/public You can refer to the images in markdown via ! [Logo] (/logo.svg) NOTE: There is no compilation checking, so it could refer to a broken link.

How to reference an image in vuepress/public?

With a base URL, to reference an image in .vuepress/public, you’d have to use URLs like /bar/image.png. But this is brittle if you ever decide to change the base. To help with that, VuePress provides a built-in helper $withBase (injected onto Vue’s prototype) that generates the correct path:

How to deploy vuepress to a non-root URL?

In such cases, you can put them inside .vuepress/public and they will be copied to the root of the generated directory. If your site is deployed to a non-root URL, you will need to set the base option in .vuepress/config.js.


Video Answer


1 Answers

In markdown (without a Vue component) you can use html,

<figure>
  <img src='../../guides/contribute/images/typora-tela1.png'>
  <figcaption>Caption Example</figcaption>
</figure>

To make the CaptionedImage.vue component work I think you need to put the images in the /.vuepress/public/ folder.

The difference (as I understand it) is that within the markdown the image path is handled at compile time, but for the component the image path is resolved at runtime.

Anything placed in /.vuepress/public/ is available at runtime referenced from the page root.

This works for me:

project structure

<project root folder>
  docs
    .vuepress
      components
        CaptionedImage.vue
      public
        images
          myImage.jpg
    ReadMe.md

CaptionedImage.vue

<template>
  <figure>
    <img :src="imagesrc" alt=""/>
    <figcaption>Legenda: {{ caption }} - {{ src }}</figcaption>
  </figure>
</template>
<script>
export default {
  props: ['src', 'caption'],
  computed: {
    imagesrc () {
      return './images/' + this.src
    }
  }
}
</script>

ReadMe.md

<CaptionedImage src="myImage.jpg" caption="Caption Example"></CaptionedImage>
like image 85
Richard Matsen Avatar answered Oct 06 '22 12:10

Richard Matsen