estruyf / vscode-front-matter

Front Matter is a CMS running straight in Visual Studio Code. Can be used with static site generators like Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more...
https://frontmatter.codes
MIT License
1.91k stars 72 forks source link

Enhancement: Add setting for enforcing relative image paths #282

Closed rolule closed 2 years ago

rolule commented 2 years ago

Is your feature request related to a problem? Please describe. I am using Front Matter with the Gatsby framework and MDX files. My folder structure looks basically identical to the "Page and leaf bundles" example (except I use MDX files). When I add an image located in the same folder as the the index.mdx file, Front Matter always adds the absolute path to the image (/content/posts/leaf-bundle/image1.jpg) instead of the much shorter relative path (./image1.jpg). However, Gatsby MDX does not seem to support absolute image paths out of the box, so it fails creating the post when it encounters the image url.

Describe the solution you'd like I would love a setting that forces relative paths when adding images through Front Matter. In the example above, the path would then be only the image's file name and not the full absolute path. It should also apply to sibling leaf-nodes (../leaf-bundle-2/image2.jpg). This would make resolving (image) files in the same directory much easier in Gatsby, but I am sure this also applies to other frameworks as well.

Describe alternatives you've considered The alternative would be to find a Gatsby workaround for this, which I could not find yet unfortunately.

estruyf commented 2 years ago

Thank you for suggesting this enhancement @rolule. To what I said in #270, the media retrieval should be based on the content types that have the pageBundle property enabled.

estruyf commented 2 years ago

As I was trying to reproduce it, I noticed the logic of creating relative paths was already in place when inserting images into the article's content. Just to understand it a bit better:

estruyf commented 2 years ago

Logic has changed a bit to make sure that sub-folders are also supported in the scenario. Feel free to test out the latest beta version.

rolule commented 2 years ago

Hello @estruyf, thank you for the fast reply. I am using MacOS 12.2 (21D49). I was referring to adding the image into the front matter of the article. Adding the images to the content by clicking the WYSIWYG editor does not work in MDX files for me (might create a separate issue for this).

I tried the latest beta, but I did not notice any changes.

estruyf commented 2 years ago

Changes that were added, were to the relative paths in the content, not yet in the front matter.

rolule commented 2 years ago

I checked out the new Beta and it worked, thank you ! The path in the front matter is now ./image1.jpg. Maybe we can even leave out the./? Also, relatively resolving the sibling leaf-nodes did not work for me yet (e.g: ../leaf-bundle-2/image2.jpg)

estruyf commented 2 years ago

Wasn't completed yet, but thanks for verifying 🙏

estruyf commented 2 years ago

With this change, the relative paths work for the current bundle and sub-leafs.

rolule commented 2 years ago

Awesome ! Works like a charm. Thank you :)