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

Support relative paths in markdown images #43

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

runofthemillgeek
Copy link
Contributor

Adds code from Hugo's built-in render-image hook and modifies it slightly to support relative image paths in markdown files.

Previously, we'd have to spell the absolute path from root of the hugo site or make directories for each individual post which is what Hugo recommend. But this was very inefficient. This change allows us to use editors like Obsidian, VSCode and have them render the images correctly. It also supports Hugo's page bundles for rendering images as a result. Based on the order, I believe page bundles are given priority so that works out.

i.e, if you have the following tree:

content
└── posts
   ├── _index.md
   ├── hello.md
   ├── images
   │  └── test.png
   └── test.png

You can now include images in hello.md in the following ways:

![](test.png)
![](images/test.png)
![](./test.png)
![](./images/test.png)

Adds code from Hugo's built-in render-image hook
and modifies it slightly to support relative image
paths in markdown files.

Previously, we'd have to spell the absolute path
from root of the hugo site or make directories for
each individual post which is what Hugo recommend.
But this was very inefficient. This change allows
us to use editors like Obsidian, VSCode and have
them render the images correctly. It also supports
Hugo's page bundles for rendering images as a
result. Based on the order, I believe page bundles
are given priority so that works out.

i.e, if you have the following tree:

```
content
└── posts
   ├── _index.md
   ├── hello.md
   ├── images
   │  └── test.png
   └── test.png
```

You can now include images in `hello.md` in the
following ways:

```
![](test.png)
![](images/test.png)
![](./test.png)
![](./images/test.png)
```
@tomfran
Copy link
Owner

tomfran commented Nov 5, 2024

Cool, this is backward compatible right?

@runofthemillgeek
Copy link
Contributor Author

@tomfran yeap, I tested the absolute path option (i.e /posts/test.png) as well as a page bundle like the posts you have on your personal blog. Both seem to work fine in my case.

@tomfran
Copy link
Owner

tomfran commented Nov 5, 2024

Thank you so much for testing

@tomfran tomfran merged commit 4e760d1 into tomfran:main Nov 5, 2024
@runofthemillgeek runofthemillgeek deleted the feat/md-img-relative-links branch November 5, 2024 21:33
@runofthemillgeek
Copy link
Contributor Author

Pulled the latest commit into my personal blog and ran a build. Looks all good even with existing paths. Converted paths to relative and published the built assets. This change is working as expected: https://blog.sangeeth.dev/posts/custom-domains-with-https-for-your-localhost-servers-on-macos/

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

Successfully merging this pull request may close these issues.

2 participants