-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Allow for relative paths of media files #2394
Conversation
Preview proposed changes to netlifycms.org in the link below: Built with commit 80cf364 |
Preview proposed changes to the CMS demo site in the link below: Built with commit 80cf364 |
The required relative path is now calculated depending on the location of the collection of the current entry having the media inserted into. And the configuration option has now been changed to a boolean flag. This allows collections to not neccesarily all be in the same location relative to the media folder, and simplifies config.
While working on my other pull request #2397, I was able to work out how to get information about the current entity being edited from within I also introduced a new function in The image previews in |
Thanks for digging into this! The approach is dead on, resolving the relative path for individual files makes the mosts sense 👍 I know there's not a ton of existing coverage to add to (we're working on it), but we're requesting that new pull requests include adequate test coverage. Makes it much easier to ensure against regressions, cover the new code, and provide documentation-in-action of how new functionality is supposed to work. |
Sounds good. I'll add some tests for these changes. |
This moves more of the media path resolution logic into the action which makes it easier to unit test
I've added unit tests now, and reworked the action a little bit to make it easier to test too. To do this I also added |
Awesome, thanks for that! I think we can get this into beta and ask some Gatsby users to test it out. @Benaiah you good with this being merged? I don't think it impacts what you're working on, but wanted to be sure. |
Hey Sam - sorry, haven't forgotten about this, will get it merged and released soon. |
Fixed merge conflict, merging and releasing to beta. |
Released in 2.9.8-beta.2 - so sorry for the wait! |
Awesome! =) |
Thanks for doing this guys! I've been excited for this feature to get merged, but when I run an
Did the dependencies of |
You're totally right! Thanks for reporting this, I'll update when it's published. Sent with GitHawk |
* Allow for relative paths of media files fixes decaporg#325 * Switch to calculating the relative path based on collection The required relative path is now calculated depending on the location of the collection of the current entry having the media inserted into. And the configuration option has now been changed to a boolean flag. This allows collections to not neccesarily all be in the same location relative to the media folder, and simplifies config. * Clean up code and fix linting * Add unit tests to resolveMediaFilename() * Rework insertMedia action to fetch own config This moves more of the media path resolution logic into the action which makes it easier to unit test * Add unit tests for the mediaLibrary.insertMedia action * yarn run format * add dependabot config (decaporg#2580)
Summary
fixes #325
This is an initial attempt at fixing the long-standing issue of Netlify not supporting relative paths in the output in markdown, frontmatter, yaml etc...
I wasn't quite able to get it working the way I had hoped, which I'll go into in a little bit, but this at least should work for the majority of cases talked about on #325.(EDIT: I was able to get this working)In short, I've introduced a new configuration option
media_folder_relative
which when set totrue
,is used in place ofwill calculate the relative path to media files from the relevant collection's folder, instead of usingpublic_folder
, and no slash is prepended to the pathpublic_folder
ormedia_folder
. This work is based on the work @fk did in fk@2ebbed8Original Idea (now implemented):
What I was originally thinking after reading the thread in #325 was that it would be great if we could instead use a boolean configuration flag to indicate if paths should be relative, and then if that's the case, resolve what that relative path would be using the path of the Entry that we're inserting the media into. This has the added benefit of not requiring all collections to be in the same location relative to the media folder. e.e, something like this:
Would allow files in
posts
,tags
andcategories
to all relatively refer to images inmedia
. However I was unable to get this working as I wasn't sure how to obtain information of the current entry from within the context of the mediaLibrary actions and reducers, or from within theFileControl
component (i figured out how to pass a path fromFileControl
toresolvePath
though).For example,
media_folder
would becontent/media
. And forcontent/posts/hello_world.md
we'd resolveimage.png
to../media/image.png
. And fromtag_1.md
we'd resolve it to../../media/image.png
.Other Ideas Considered:
Just use
public_folder
, and don't prepend a slash if it starts with./
or../
.@erquhart had suggested this (#325 (comment)). However this has the downside that it is a breaking change for people that have misconfigurations of
public_folder
, but also it prevents us being able to also usepublic_folder
to display preview images in the CMS. I also felt that the name would be inconsistent with how we were treating that configuration variable (i.e. not as a public folder at all).Further Potential Improvements:
Implement the original idea(DONE)resolvePath
seems to be used in a number of different contexts, and it's not clear that they're all the same. For example it's now used for converting a single filename to a path to enter in to the markdown. It's also used to convert that path into a path to use when displaying the image in the CMS. It probably makes sense to split it out into different functions for the different contexts so this is clearer. (EDIT: this is partially done inresolveMediaFilename
now).resolvePath
, the image preview inFileControl
doesn't work in the case where the image:It would probably make sense to have it use the same mechanism as
loadMediaDisplayURL
in themediaLibrary
action.