-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
'relative path' field for uploaded images #325
Comments
@MarcCoet a simple, non-breaking approach would be to accept a
|
This issue should be at least helped by #350. |
@MarcCoet FWIW a quick workaround is to just edit the markdown of your post to the correct relative path from within netlify cms (ie, just toggle the 'Markdown' switch and edit the path) It'll break the preview, but so far it appears to push fine. here's my config:
|
I still think this use case should be supported, but it merits further discussion. |
Hey! 👋 I took a first quick shot at what @erquhart outlined in #325 (comment) over the weekend. Previews don't work yet (haven't looked into the code much) but this does enough to let a friend of mine that I'm helping with his portfolio site author his stuff – relative paths are correctly inserted in Markdown. My context is exactly like discussed in #843 (using Gatsby). I would love to provide a PR for this eventually but most probably will need some guidance. |
@fk that's awesome! In the time since I wrote that comment, I've come to avoid "switches" that affect other settings in the config. A better approach that's still non-breaking would be to just check if the path begins with one or two dots and a slash and don't prepend a slash if so. We'd want to test it out a bit for unexpected consequences, though. Are you up for giving that a shot? |
Hey @erquhart! Yes! Up for anything! ;-) :-D I also have been thinking a little about how to approach this feature in the meantime. I didn't have much time, so I only came as far as to look into I also started to wonder if approaching this change from what I believe is a quite specific, "Gatsby" point of view, is the right thing to do to actually land a PR that is beneficial to anyone else – in other words: "What are other use cases for a relative public path (which couldn't be resolved by just changing the location of the media folder)?" is a question I figured I should ask here before going any further. A quick recap of my specific use case: For Gatsby to be able to process URLs in Markdown (process images, copy linked files), it requires them to be relative (I still have to check back with @KyleAMathews on that, but I think "it's a Webpack thing") – basically what surfaced in #843. Ideally I thought netlify-cms could resolve the path to be inserted in Markdown relative to the Markdown source and the location of the media folder. But somehow I feel like I'm missing something obvious still…
Yes, totally can follow your thoughts. FWIW, I've been seeing sindresorhus/is-relative-url being used in Gatsby.
Yes! How about I adjust my fork to look at the path/remove the new |
@fk apologies for the delay - that would be awesome. The problem and solution can really approached quite abstractly: for myriad reasons, folks sometimes want to set their media folder via relative path. I can't think of other specific use cases off the top of my head. |
Hugo is going to be supporting image processing soon (before Jan 1!), and if I understand correctly, it will only work with relatively placed images. We probably want to prioritize this a little more. |
@erquhart I don't quite understand how the media library will handle relatively placed media. Will it just walk through all the directories to find files that match the Also, what do we do if an image is added to one entry, and then it is added to a different post via the media library? Do we just add the path relatively to the old location, or would we copy the actual image to the second location as well? |
I found another workaround. Maybe it will be useful for somebody else. The path added to the markdown files by Netlify CMS can be adjusted and set (as Gatsby requires) to the relative path from the particular node (markdown file) the image is used in. Adding the following code to
This assumes that
Also, the name of the markdown fields this would apply for is hardcoded above as |
You shouldn't mutate node data. The data should be treated as immutable (I'm actually surprised that worked as I thought we were already passing a copy of data to APIs to prevent exactly this sort of thing. Instead use https://www.gatsbyjs.org/docs/bound-action-creators/#createNodeField |
Ok, makes sense. I actually tried to use the spread operator to avoid mutation, but somehow it didn't work. Will try with createNodeField |
how would the graphQL query look like with This works:
But nicer would be:
which fails with: |
If you want to link to a file node, you need to add |
Hi everyone! |
Hi @PaulEibensteiner - welcome, you're in the right place! And your solution sounds awesome! Can you put your code in a gist and provide a link? |
@PaulEibensteiner great job. I was planning to eventually get to creating a widget for a Hugo shortcode, but have been really busy with some other fun stuff that will be coming soon. |
Sure!
Then the shortcode in the layouts/shortcodes directory: Now the custom editor widget: I have a 1-year knowledge of self taught javascript so this might be a complete mess - also I didn't quite understand the documentation about building a custom widget. So I just added the following code at the bottom of my static/admin/index.html (after the <script src="https://unpkg.com/netlify-cms@^1.0.0/dist/cms.js"></script> tag):
ps: I might have messed up the naming of elements because I already changed some stuff in the original project but omitted it here for 'simplicity' - I hope it works. Pls let me know if anything is flawed or if my explanations are confusing |
@PaulEibensteiner This is good stuff. Don't worry about the widget, I will eventually formalize yours into an npm module if you don't get to it first. Again, great job. Also, yes Hugo is pretty Awesome! |
@talves Thank you, yeah, I don't really know about npm and that stuff ˆˆ But that would be awesome... |
We have been discussing creating a place for people to publish information about their custom widgets on the NetlifyCMS site somewhere. You are right, and a plan to have a full information format will help quite a bit. I am sure we will get there. We just need someone in the community to take it on when they get time. I might tackle it with some other stuff I am doing, but it is lower on the priority at the moment. I created an issue for discussion here, rather than us keep hijacking this issue. |
@zionis137 Responding to an old comment of yours #325 (comment) If you use
then you should be able to query your image node like this:
As I understand it, any paths that Gatsby can identify as a file system path will be converted to file nodes in the query results in which case you can access relativePath, and childImageSharp, etc. |
Thanks, that works great, I used a trick like this to let me leave the images in src/static whilst letting me process them in remark-images. Doing that means the netlify CMS previews work as expected as well. |
I've found this working damn great! |
Has anyone tried this with an animated gif file? I previously used @danielmahon plugin and had that working...but when my client just decided they wanted a gif...I'm now getting errors that |
@joshuarule check the ‘gatbsy-image’ docs but I think GIFs and SVGs aren’t supported...maybe? |
@joshuarule I'm assuming this is still the case? gatsbyjs/gatsby#7317 (comment) |
relative paths of media files fixes decaporg#325
* Allow for relative paths of media files fixes #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 (#2580)
* 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)
Hi,
I am starting to use Netlify CMS with Gatsby 1.0.
I need a relative path to fetch pictures with the graphQL layer. So in my frontmatter I would like to have a field like:
fieldName: ../images/myImage.jpg
Of course the last part ("myImage.jpg") would be different for each entry depending on the file I upload.
Is there a way to auto generate a string field in the frontmatter that would concatenate a string of my choice (for example '../images/') with the name of a picture I may upload in an image field of the same entry ?
I also tried to set:
public_folder: "../images"
This would theoretically change all my image fields in all collections to get that leading string in the file path but when I create a new file with the CMS I get an auto opening '/'... So in my frontmatter:
image: /../images/myImage.jpg
instead of
image: ../images/myImage.jpg
Of course this breaks my groove as well.
What is the best way to get around this issue?
The text was updated successfully, but these errors were encountered: