Skip to content

Markdown image directive and html <img> have different paths. #991

Closed

Description

@ostrokach

I am trying to use MkDocs to publish some of the notes that I have been writing in markdown.

Sometimes I there are large images that I wish to include in my docs. Since markdown does not allow you to specify the width of the image, I resort to using the html <img> tags.

For example, in my todo.md file, instead of:

![My large image](todo/my_large_image.png)

I have:

<p align="center">
<img src="todo/my_large_image.png" width="600px" >
</p>

However, while the markdown tag renders correctly, the html one does not. MkDocs looks for the image todo/todo/my_large_image.png. This is inconsistent with every markdown editor that I have tried, and GitLab markdown edit.

Any suggestions?

(and thanks for MkDocs, it looks great! :))


$ python --version
Python 3.5.1 :: Anaconda 4.0.0 (64-bit)

$ mkdocs --version
mkdocs, version 0.15.3

Activity

  1. waylan commented on Jul 17, 2016

    @waylan
    Contributor

    As documented, MkDocs will alter internal links. Depending on how the use_directory_urls setting is configured, the path to a page can change, so MkDocs adjusts the links to compensate. This behavior is on top of standard Markdown. However, it only applies to links made with Markdown syntax. Raw HTML is ignored, which explains the inconsistent behavior you are seeing.

    I understand why you are using Raw HTML, but I'm not clear on which links are "broken" (fail to point to the actual image). Is it the altered Markdown links which are incorrect (which means we have a bug), or are the raw HTML links incorrect (which means you will need to use something different to link to your images)?

  2. ostrokach commented on Jul 17, 2016

    @ostrokach
    Author

    Thanks for the reply!

    It is the raw html links that are rendered incorrect. The name of the markdown file containing the link gets prepended to the link. For example, if I have a link todo/my_large_image.png inside a mardown file todo.md, the link becomes: todo/todo/my_large_image.png. This is assuming the following directory structure:

    +-- todo.md
    +-- todo
        + my_large_image.png
    

    I can get the links to work correctly by adding .. (e.g. src="todo/my_large_image.png" becomes src="../todo/my_large_image.png"). But then the images are not rendered correctly in every other MarkDown editor.

    Changing use_directory_urls did not solve the problem.

  3. waylan commented on Jul 18, 2016

    @waylan
    Contributor

    This is assuming the following directory structure:

    +-- todo.md
    +-- todo
        + my_large_image.png
    

    Ah, I see your problem. If you have use_directory_urls=True (the default), then you get:

    +-- todo/
        +-- index.html
        +-- my_large_image.png
    

    But, MkDocs does not account for your img being in a directory with the same name as your Markdown file, so it steps you out (with ../) and then steps back in (with todo/). If you were starting with this structure (where the img dir is not named the same as the Markdown file):

    +-- todo.md
    +-- img/
        +-- my_large_image.png
    

    Then, MkDocs would generate the following structure:

    +-- todo/
        +-- index.html
    +-- img/
        +-- my_large_image.png
    

    In which case a link from the document to the image would correctly be ../img/my_large_image.png. Whats confusing you is the fact that your img dir is the same name as the Markdown file (without the file extension). So everything is working fine.

    As I mentioned before, the code that handles this URL manipulation ignores raw HTML, so yes, your raw HTML links would be broken and you would need to edit them manually. And yes, if you do so, they will be broken in every other environment. For the record, we have no intention of changing this in the foreseeable future. You should not use raw HTML to link internally when you have use_directory_urls=True.

    However, if you have use_directory_urls=False, then all that extra directory rearranging and URL munging does not happen. Your original structure would generate the following output structure:

    +-- todo.html
    +-- todo/
        +-- my_large_image.png
    

    And the link to the img would be todo/my_large_image.png, which should work in any environment. If that is not working, then I'll need more info about that.

  4. waylan commented on Jul 18, 2016

    @waylan
    Contributor

    We need to do a better job of warning people about these potential pitfalls in the documentation. I've given this the "Documentation" label to remind us to do that.

  5. ostrokach commented on Jul 18, 2016

    @ostrokach
    Author

    Thanks for your help!

    Regarding the following:

    However, if you have use_directory_urls=False, then all that extra directory rearranging and URL munging does not happen. Your original structure would generate the following output structure:

    +-- todo.html
    +-- todo/
        +-- my_large_image.png
    

    And the link to the img would be todo/my_large_image.png, which should work in any environment. If that is not working, then I'll need more info about that.

    I renamed the todo folder and links to img, and added use_directory_urls: false to my mkdocs.yml file. However, for me, running mkdocs build still produces the following directory structure:

    +-- todo/
        +-- index.html
    +-- img/
        +-- my_large_image.png
    

    The difference between use_directory_urls: true and use_directory_urls: false appears to be that, in the former, the URL for the todo page is at http://127.0.0.1:8000/todo/ and, in the latter, it is at http://127.0.0.1:8000/todo/index.html.

    I created a demo github repository so that you can see what is going on: https://github.com/ostrokach/mkdocs-test.

    The todo page is here: https://ostrokach.github.io/mkdocs-test/todo/index.html

    Note that both types of markdown work in GitHub preview: https://github.com/ostrokach/mkdocs-test/blob/master/docs/todo.md

    Having MkDocs produce the directory structure that you show for use_directory_urls: false, would be ideal. However, I understand if that would take too much work to implement at this point.

  6. waylan commented on Jul 18, 2016

    @waylan
    Contributor

    Are you running your build with the --clean flag? If not, then the problem may be that remnants of a previous build still exist.

    In any event, it would seem we have a bug here. I'll need to investigate later when I have some time.

  7. waylan commented on Jul 18, 2016

    @waylan
    Contributor

    Okay, I'm confused. I've always assumed that use_directory_urls: false has the behavior I described above. Apparently, however, regardless of the setting, all pages are nested in dirs and given index.html as a file name. WHY? Shouldn't we only be replacing the .md file extension with .html and leaving it as-is otherwise (not creating nested index.html files)? I can fix this, but based on comments in the code, this behavior appears to be intentional. Any insight @d0ugal @tomchristie?

  8. d0ugal commented on Jul 25, 2016

    @d0ugal
    Contributor

    This behaviour has always existed to create "pretty" urls. For example http://www.mkdocs.org/user-guide/configuration/ rather than http://www.mkdocs.org/user-guide/configuration.html. I've never been a huge fan of it, and it has caused many bugs, that I've tried to fix a few times but always seem to miss something.

    I thought I had fixed this particular bug in the past, but I guess not.

  9. waylan commented on Jul 25, 2016

    @waylan
    Contributor

    I understand why the feature exists, however with the feature turned off, we get http://www.mkdocs.org/user-guide/configuration/index.html rather than http://www.mkdocs.org/user-guide/configuration.html. I assumed this was a bug, however, the comments in the code seem to indicate it is intentional... which makes no sense to me.

    As both you and I assumed things work differently with the feature turned off than it actually does, perhaps one of us introduced bugs.

  10. d0ugal commented on Jul 25, 2016

    @d0ugal
    Contributor

    Right, so I can't fully speak for that decision as it predates me. However, I assume it is done for simplicity and to allow all the other relative path handling to remain the same.

  11. waylan commented on Jul 25, 2016

    @waylan
    Contributor

    I assume it is done for simplicity and to allow all the other relative path handling to remain the same.

    That was my assumption as well. However, I consider the decision to be a bad one. In fact, personally, I think the feature should be turned off by default with no URL munging. Then, if users really want "pretty URLs" they can turn the feature on. And I would think the feature (when on) should behave more like Jekyll's Extensionless Permalinks (which also avoids most of the URL munging we do). It would certainly simplify/eliminate all these annoying bugs and it is easy to add to the dev server.

    Of course, that would be a backward incompatible change. Even if we don't go that route, it is not clear to me what the correct way forward is here.

  12. rmzelle commented on Aug 4, 2016

    @rmzelle

    I'm testing mkdocs to see if I can switch from pandoc, and differences in file/url organization also trip up my migration. My existing source file layout is:

    index.md
    +-- topicA/
        +-- index.md
        +-- topicA.png
    +-- topicB/
        +-- index.md
        +-- topicB.png
    

    Both pandoc and mkdocs produce this:

    index.html
    +-- topicA/
        +-- index.html
        +-- topicA.png
    +-- topicB/
        +-- index.html
        +-- topicB.png
    

    However, while pandoc correctly recognizes image links like ![picture](topicA.png) in e.g. topicA/index.md (generating <img src="topicA.png" alt="picture">), mkdocs creates a broken link (<img alt="picture" src="../topicA.png">), expecting the image file to be present in the root directory.

    (I also have an issue where pandoc converts [topicA](topicA/) in the root's index.md to <a href="topicA/">topicA</a>, whereas mkdocs produces <a href="./topicA/">topicA</a> (Chrome interprets the latter as a "file:///" path to the topicA subdirectory instead of a link to show topicA/index.html))

    It would be great if I could keep my source documents untouched, and the pandoc output makes more sense to me than the mkdocs output.

  13. added this to the 1.0.0 milestone on Dec 2, 2016
  14. added a commit that references this issue on Jun 12, 2018
    88a9494
  15. BraSDon commented on Dec 4, 2023

    @BraSDon

    Ran into the same issue. I also required an extra ../ for mkdocs to find the image, however displaying it in pure markdown (e.g. in a jupyter notebook) did not work anymore then. The workaround I found is as follows:

    <div>
        <img 
            src="../assets/images/example.svg"
            onerror="this.onerror=null; this.src='assets/images/example.svg';"
        />
    </div>
    

    EDIT: Apparently this only works when locally serving the documentation

  16. added a commit that references this issue on Jan 18, 2025
    34ef3ca
  17. added a commit that references this issue on Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions