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

Image captions #892

Closed
4 tasks done
noClaps opened this issue Jul 1, 2022 · 4 comments
Closed
4 tasks done

Image captions #892

noClaps opened this issue Jul 1, 2022 · 4 comments
Labels
build Related to the build system enhancement New feature or request

Comments

@noClaps
Copy link

noClaps commented Jul 1, 2022

Is your feature request related to a problem? Please describe.

There is no easy way to have captions for images.

Describe the solution you'd like

Any image I'm adding as ![alt-text-here](/path/to/image/ "Text here"){.image} should have the caption be generated as "Text here" in the <figcaption> element below the image. The entire thing should be in a <figure> element and either the <img> or <figure> should have class="image".

So basically,

![Alt text here](/path/to/image/ "Text here"){.image}

should generate

<figure>
  <img class="image" src="/path/to/image/" alt="Alt text here">
  <figcaption>Text here</figcaption>
</figure>

This would allow to link to licenses and provide attribution to image sources.

Describe alternatives you've considered

I've tried using markdown-it-image-caption, markdown-it-figure-caption and mdfigcaption, but none were suitable for me.

The issue I'm having with all of these is that they're not letting me add my own classes to the <figure> element, so I can't theme each image individually. This is an issue since the images I'm using need to be of different sizes depending on where they are in the text, which classes allow me to do.

Additional context

No response

Validations

@brc-dd
Copy link
Member

brc-dd commented Jul 2, 2022

Sorry, but ![Alt text here](/path/to/image/ "Text here") should always generate <img src="/path/to/image/" alt="Alt text here" title="Text here" />. Its part of the CommonMark spec and we shouldn't change that. We can have some other syntax probably.

@brc-dd brc-dd added enhancement New feature or request build Related to the build system labels Jul 2, 2022
@noClaps
Copy link
Author

noClaps commented Jul 2, 2022

In that case, I wouldn't be opposed to having

![Alt text here](/path/to/image/ "Text here"){.class}
Caption
{.figclass}

generate

<figure class="figclass">
  <img src="/path/to/image/ alt="Alt text here" title="Text here">
  <figcaption>Caption</figcaption>
</figure>

where any text directly below the image would become a <figcaption> and the entire element a <figure>.

This could be applied for other resources as well, I imagine. Code blocks could be like:

    ```
    // code here
    ```
Caption here

@brc-dd
Copy link
Member

brc-dd commented Jul 2, 2022

You can use markdown-it-implicit-figures or its forks (like markdown-it-image-figures if you want title to be caption). Then in config you'll need to do this:

import { defineConfig } from 'vitepress'
import implicitFigures from 'markdown-it-implicit-figures'

export default defineConfig({
  markdown: {
    config: (md) => {
      md.use(implicitFigures, {
        figcaption: true,
        copyAttrs: '^class$'
      })
    }
  }
})

In markdown:

![alt](https://picsum.photos/536/354 'title'){.foo}

Then you can style your figure elements using figure.foo selector.

@brc-dd
Copy link
Member

brc-dd commented Jul 2, 2022

I am closing this as this can be easily solved with a markdown-it plugin.

Refer this for complete example: https://stackblitz.com/edit/vite-qyrr8h?file=docs%2Findex.md,docs%2F.vitepress%2Fconfig.js

@brc-dd brc-dd closed this as completed Jul 2, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
build Related to the build system enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants