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

Link in markdown when outputting to subdir #369

Closed
asuyou opened this issue Aug 26, 2022 · 3 comments
Closed

Link in markdown when outputting to subdir #369

asuyou opened this issue Aug 26, 2022 · 3 comments

Comments

@asuyou
Copy link

asuyou commented Aug 26, 2022

Is the default behavior when outputing to a subdir and markdown for the image link not to be in the subdir?
E.g.
Running ./node_modules/.bin/mmdc -i src/main.md -o dist/main_updated.md leads to markdown with output like this

![diagram](./main_updated-1.svg)

Would it not be better for it to the link into the subdir? e.g.

![diagram](./dist/main_updated-1.svg)

I apologize for the initial PR actually hit enter and for bad wording

@asuyou asuyou changed the title Output to Output to subdir Aug 26, 2022
@asuyou asuyou changed the title Output to subdir Link in markdown when outputting to subdir Aug 26, 2022
@aloisklink
Copy link
Member

The vast majority of Markdown renderers/parsers assume that image links are relative to the current markdown file's directory.

So, the image link won't contain the subdir, because the markdown is also in the subdir, so they're both in the same directory.

This is because it's the same way .html files work, if you have an image with <img src="./mermaid-1.svg" alt="diagram">, it will look for that file in the same folder.

For example, if your markdown file is /projectFolder/dist/main_updated.md, most Markdown renderers assume that a link to ./main_updated-1.svg will be:
/projectFolder/dist/ + ./main_updated-1.svg = /projectFolder/dist/main_updated-1.svg.

If you instead use ./dist/main_updated-1.svg, then those Markdown renderers will look for the file at:
/projectFolder/dist/ + ./dist/main_updated-1.svg = /projectFolder/dist/dist/main_updated-1.svg, which would not exist.

There are some rare Markdown renderers/parsers that don't follow that standard. Old alpha versions of docusaurus v2 didn't work that way, hence why I made a remark plugin that stores the image files within the markdown file (see https://github.com/aloisklink/remark-mermaid-dataurl).

I'm going to mark this as "Won't fix". But let us know though if there's a Markdown program which works otherwise! It might be worth re-opening this if there's demand for this feature (we could hide it behind an option like --linksRelativeTo=working-dir)!

@aloisklink aloisklink closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2022
@asuyou
Copy link
Author

asuyou commented Aug 31, 2022

Thanks for the in-depth response and info!

Currently I am using pandoc and it seems to work based on directory it is run in

For example, if you have a project layout layout like this...

projectFolder
- src
- - main.md
- - img1.png
- - img2.png

If you run pandoc from projectFolder, you will need to link images with something like ![](src/img1.png)

I managed to work around this using --resource-path from the pandoc manual so I apologize for not finding this in advance. I agree that this is too niche of an issue to reopen.

Thanks again!

@aloisklink
Copy link
Member

Ah, Pandoc is a bit different. Most Markdown parsers are designed to make HTML/websites with multiple pages.

However, Pandoc is designed to combine multiple Markdown files into a singe PDF document. It's great if you're know LaTeX, but it is a bit confusing from a HTML background.

For Pandoc (>=2.14), I'd recommend using the official rebase_relative_paths extension: https://pandoc.org/MANUAL.html#extension-rebase_relative_paths It's the official way make images/links relative to the input markdown file: jgm/pandoc#3752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants