You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Markdown image directive and html <img> have different paths. #991
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.
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.
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)?
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.
Reacted by Sami Farhat, Xiaoven and Utkarsh Tiwari
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:
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.
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.
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:
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.
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.
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?
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.
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.
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.
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.
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:
However, while pandoc correctly recognizes image links like  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.
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:
Activity
waylan commentedon Jul 17, 2016
As documented, MkDocs will alter internal links. Depending on how the
use_directory_urlssetting 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)?
ostrokach commentedon Jul 17, 2016
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.pnginside a mardown filetodo.md, the link becomes:todo/todo/my_large_image.png. This is assuming the following directory structure:I can get the links to work correctly by adding
..(e.g.src="todo/my_large_image.png"becomessrc="../todo/my_large_image.png"). But then the images are not rendered correctly in every other MarkDown editor.Changing
use_directory_urlsdid not solve the problem.waylan commentedon Jul 18, 2016
Ah, I see your problem. If you have
use_directory_urls=True(the default), then you get: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 (withtodo/). If you were starting with this structure (where the img dir is not named the same as the Markdown file):Then, MkDocs would generate the following structure:
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: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.waylan commentedon Jul 18, 2016
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.
ostrokach commentedon Jul 18, 2016
Thanks for your help!
Regarding the following:
I renamed the
todofolder and links toimg, and addeduse_directory_urls: falseto mymkdocs.ymlfile. However, for me, runningmkdocs buildstill produces the following directory structure:The difference between
use_directory_urls: trueanduse_directory_urls: falseappears to be that, in the former, the URL for the todo page is athttp://127.0.0.1:8000/todo/and, in the latter, it is athttp://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.waylan commentedon Jul 18, 2016
Are you running your build with the
--cleanflag? 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.
waylan commentedon Jul 18, 2016
Okay, I'm confused. I've always assumed that
use_directory_urls: falsehas the behavior I described above. Apparently, however, regardless of the setting, all pages are nested in dirs and givenindex.htmlas a file name. WHY? Shouldn't we only be replacing the.mdfile extension with.htmland leaving it as-is otherwise (not creating nestedindex.htmlfiles)? I can fix this, but based on comments in the code, this behavior appears to be intentional. Any insight @d0ugal @tomchristie?d0ugal commentedon Jul 25, 2016
This behaviour has always existed to create "pretty" urls. For example
http://www.mkdocs.org/user-guide/configuration/rather thanhttp://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.
waylan commentedon Jul 25, 2016
I understand why the feature exists, however with the feature turned off, we get
http://www.mkdocs.org/user-guide/configuration/index.htmlrather thanhttp://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.
d0ugal commentedon Jul 25, 2016
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.
waylan commentedon Jul 25, 2016
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.
rmzelle commentedon Aug 4, 2016
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:
Both pandoc and mkdocs produce this:
However, while pandoc correctly recognizes image links like
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.
Document links in raw HTML
BraSDon commentedon Dec 4, 2023
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:EDIT: Apparently this only works when locally serving the documentation
Internal Refactor of Pages, Files, and Navigation (mkdocs#1504)
Fix Hop-3 documentation figure