Skip to content

Images in markdown relative to content  #1240

Closed

Description

@garvincasimir

Hi,
I like the idea of having my images close to my content so I create my articles like this:

post/my-post.md
post/images/my-post-img.png

This displays the images on the single template when ugly urls are turned on but does not display it on the list pages or on any page when ugly urls are turned off. The relative path for the image never gets transformed even with canonifyurls turned on. Maybe there is a setting I am messing?

![my img](images/my-post-img.png)

I see issue #147 had a discussion that might be related to this. The images are being copied but I am not getting the correct urls in the generated pages.

Any thoughts on this?

Activity

  1. bep commented on Jul 2, 2015

    @bep
    Member

    This is currently not possible, but it is (or should be) in front of @spf13's brain ...

    Currently you have to have to path relative to the context root (/post/images/...).

  2. self-assigned this
    on Jul 2, 2015
  3. garvincasimir commented on Jul 2, 2015

    @garvincasimir
    Author

    I would send a pull request but the go code makes me feel dumb. But seriously great to see @spf13 take up this issue. I know it complicates rendering because the link to the image would vary based on the final path of the page it is rendered on.

  4. tcurdt commented on Aug 9, 2015

    @tcurdt

    I would love to have support for

    posts/2015/some-post-slug/index.md
    posts/2015/some-post-slug/image.png
    posts/2015/some-post-slug/archive.zip
    posts/2015/other-post-slug/index.md
    

    and I want would like that to appear in the output as

    /2015/some-post-slug/index.html
    /2015/some-post-slug/image.png
    /2015/some-post-slug/archive.zip
    /2015/other-post-slug/index.html
    
  5. tcurdt commented on Aug 9, 2015

    @tcurdt

    This actually seems to be working out of the box now.
    Getting rid of "posts" folder is a problem though. A work around is to use:

    permalinks:
      post: /:year/:slug/
    

    which isn't quite the same thing though.

  6. SvenDowideit commented on Sep 10, 2015

    @SvenDowideit
    Contributor

    @spf13 have you got some ideas on how to implement this? we need it for the Docker docs, but pointers will get me up to speed faster :) (its on my list of things for the next 4 days :)

  7. SvenDowideit commented on Sep 14, 2015

    @SvenDowideit
    Contributor

    initial version added in #1398

  8. AlexZeitler commented on Jan 14, 2016

    @AlexZeitler

    Is there any progress on using images in markdown relative to content?

  9. jygastaud commented on Jan 14, 2016

    @jygastaud
    Contributor

    @AlexZeitler : As suggest in #147 I'm using the following workaround.

    Contents are in content/project/my-content/index.md folder
    Images are stored in static/project/my-content/my-image.png folder

    And in my markdown I can do ![my-image](my-image.png)

    Hugo just will copy the structure without the first "static" folder so generation the html file will have acces to the image.

    Hope it helps!

  10. AlexZeitler commented on Jan 14, 2016

    @AlexZeitler

    @jygastaud Thanks for your reply.

    I created this structure now:

    content/blog/test-with-path/index.md
    static/blog/test-with-path/mypng.png
    

    In the md I referenced it this way ![some png](mypng.png)

    I also tried

    content/blog/test-with-path/index.md
    static/blog/test-with-path/mypng.png/mypng.png
    

    None did work.

  11. jygastaud commented on Jan 14, 2016

    @jygastaud
    Contributor
  12. garvincasimir commented on Jan 15, 2016

    @garvincasimir
    Author

    @jygastaud please take a look at the original issue again. If you only ever display images on the actual post page then you can work around it. However, if your list pages display posts with images where the image is located relative to the content then the image will not display. Hugo would probably need to take the mark down and write different urls to the same asset based on the context of the page render.

    For example, your list page is www.yoursite.com/blog/ and it has your last 3 posts. A post at www.yoursite.com/blog/mypost.html with a relative image in /blog/images/img1.jpg can use a relative url in mypost.html to reference that image but that url will be incorrect for displaying your last 3 posts. It also gets tricky when you think about ugly vs no ugly urls. My hope was that Hugo could compensate for these different nuances and always write the correct url to the asset. I hope that clarifies it.

  13. jygastaud commented on Jan 15, 2016

    @jygastaud
    Contributor

    @garvincasimir oh sorry I missed the part that explains that it works on single page but not on list :/
    Sorry for the noise.

  14. wysie commented on Jun 28, 2016

    @wysie

    This issue gets more complicated when permalinks are used. For now I've just turned off permalinks so I can put images in the same folder as my content. It'd be good if the images gets copied along to the permalink folder structure instead of just the generated HTML file. Thanks!

  15. 24 remaining items

  16. added 3 commits that reference this issue on Dec 26, 2017
    a203455
    0da20fa
    48a1b3d
  17. cdupont commented on Apr 12, 2018

    @cdupont

    Hi guys,
    how to make this work with both Hugo and Github?
    I have my documentation in markdown with images in the same folder. I would like to render it both with Hugo and Github...
    I use Hugo V0.38 with bundles.
    If I understood, I need to add some code in the markdown, such as:

    {{ $logo := .Resources.GetByPrefix "logo" }}
    

    But this won't work with Github.

  18. venky999 commented on Sep 15, 2020

    @venky999

    Any thoughts on how it works on both GitHub and Hugo

  19. RMStoica-zivver commented on Sep 16, 2020

    @RMStoica-zivver

    @cdupont obviously hugo shortcodes aren't supported in GitHub and probably never will be
    one should never use them if one wishes to maintain portability

  20. nhoizey commented on Sep 16, 2020

    @nhoizey

    If there is no Hugo based solution, I wonder if you could:

    • use standard Markdown syntax for images, so that they show up in Github,
    • use Hugo to generate the pages HTML with your own templates,
    • and then transform the HTML with images-responsiver, a Node.js library (disclaimer: I created it)

    It should work well with a few pages, but probably be pretty slow with many.

  21. cdupont commented on Sep 16, 2020

    @cdupont

    Actually, I got it working both in Github and vanilla Hugo.
    See here for example: https://github.com/Waziup/waziup.io/blob/master/content/documentation/wazidev/sensors/index.md
    I address the images like this:

    ![LM35 sensor](./media/lm35.png)
    

    The images are showing in Github.

  22. nhoizey commented on Sep 16, 2020

    @nhoizey

    @cdupont perfect! 👍

  23. sn0ot commented on Dec 31, 2020

    @sn0ot

    For those still looking for a solution to this issue (as it is not documented in the hugo docs) I will leave a solution here.

    To achieve relative image paths in Hugo in markdowns files, you should organize your content using Hugo's newish Page Bundles structure, specifically Leaf Bundles. This requires using an index.md file (not to be confused with _index.md used in branch bundles).

    You are probably used to writing posts inside of a markdown file with the name of the file as the post title , for example post_about_blah.md and blah_blah_post.md. And linking images from the static/ directory.

    |--content
    |  \__posts
    |  |  |--post_about_blah.md  
    |  |  |--blah_blah_post.md  
    |--static
    |  \__images
    |  |  |--picture_1.jpg   
    |  |  |--picture_2.jpg               
    |  |  |--picture_3.jpg   
    |  |  |--picture_4.jpg
    |  |  |--picture_5.jpg   
    |  |  |--picture_6.jpg
    

    Images would be rendered as such:

    • http://examplesite.com/images/picture_1.jpg
    • http://examplesite.com/images/picture_2.jpg
    • http://examplesite.com//images/picture_3.jpg
    • http://examplesite.com//images/picture_4.jpg
    • http://examplesite.com//images/picture_5.jpg
    • http://examplesite.com//images/picture_6.jpg

    And therefore, images would have to be linked from inside the markdown posts as such:

    • ![image](images/picture_1.jpg)
    • ![image](images/picture_2.jpg)
    • ![image](images/picture_3.jpg)
    • ![image](images/picture_4.jpg)
    • ![image](images/picture_5.jpg)
    • ![image](images/picture_6.jpg)

    Now you have to keep track of which image goes with which post, no name conflicts, etc.

    To link images in your markdown file relative to your post, consider the following leaf bundle directory tree:

    content
     \__posts 
     |  \__post_about_blah
     |  |  |--index.md
     |  |  |--picture_1.jpg
     |  |  \__images
     |  |  |  |--picture_2.jpg               
     |  |  |  |--picture_3.jpg      
     |  \__blah_blah_post
     |  |  |--index.md
     |  |  |--picture_1.jpg
     |  |  \__images
     |  |  |  |--picture_2.jpg               
     |  |  |  |--picture_3.jpg      
    

    In the leaf bundle directory tree example above, index.md contains the "words" of your post and the directory in which index.md is (in our case content/posts/post_about_blah and content/posts/blah_blah_post) will be the generated url. The images will be rendered to to following links

    • http://examplesite.com/posts/post_about_blah/picture_1.jpg
    • http://examplesite.com/posts/post_about_blah/images/picture_2.jpg
    • http://examplesite.com/posts/post_about_blah/images/picture_3.jpg
    • http://examplesite.com/posts/blah_blah_post/picture_1.jpg
    • http://examplesite.com/posts/blah_blah_post/images/picture_2.jpg
    • http://examplesite.com/posts/blah_blah_post/images/picture_3.jpg

    Therefore inside of the respective index.md files you can link to images as follows:

    • ![image](picture_1.jpg)
    • ![image](images/picture_2.jpg)
    • ![image](images/picture_3.jpg)
    • ![image](picture_1.jpg)
    • ![image](images/picture_2.jpg)
    • ![image](images/picture_3.jpg)

    NOTE: I made a poor choice of directory names and it looks like linking is the almost the same - so read carefully :)

  24. ACupofAir commented on Dec 5, 2021

    @ACupofAir

    Hi, I like the idea of having my images close to my content so I create my articles like this:

    post/my-post.md
    post/images/my-post-img.png
    

    This displays the images on the single template when ugly urls are turned on but does not display it on the list pages or on any page when ugly urls are turned off. The relative path for the image never gets transformed even with canonifyurls turned on. Maybe there is a setting I am messing?

    ![my img](images/my-post-img.png)
    

    I see issue #147 had a discussion that might be related to this. The images are being copied but I am not getting the correct urls in the generated pages.

    Any thoughts on this?

    I find a feasible method: just follow this office tutor, and change the hook, here is my render-image.html

    <p class="md__image">
      {{ if .Title }}
      <img src="{{ .Destination  }}" alt="{{ .Text }}">
      {{ else }}
      <img src="../{{ .Destination }}" alt="{{ .Text }}">
      {{ end }}
    </p>

    If you need ref a image link with http://xxxx, you need link it like this: ![image](https://xxx "title"
    If you need ref a image in the res fold you just link it ![image](res/image.png)

  25. github-actions commented on Jan 14, 2022

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  26. locked as resolved and limited conversation to collaborators on Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions