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

Relative Path Images #9

Merged

Conversation

davidwesst
Copy link
Contributor

This is an incomplete PR that I put together to fix my own relative image issues for my 11ty project, BEFORE I noticed #8 where this is being discussed.

It's a bit of a hack, but I added some conditional logic when setting the src in the plugin, where if the file is not remote and does not exist in the default directory, it checks for is relative to the file it image comes from.

I realize the desired behaviour is a config flag, but I thought I would share this as part of the discussion. Maybe it will help a bit to inspire some people.

Anyway-- I've marked it as draft so people can take a look. I've also added some test data, but they are failing. I'm hoping to take a closer look at it soon to figure out the best way to include tests around the new logic.

Feedback and discussion is appreciated. :)

@solution-loisir
Copy link
Owner

Very promising! Your solution is using some Eleventy specific code which is inevitable I think. A flag would ensure the default remains neutral. Still, the same logic would apply. I feel that the flag name would have to reflect its Eleventy nature. Maybe something like eleventyResolveToProjectRoot: true and then set it to false to get the other behavior. Thanks for your contributions! Really appreciated! 🎉

@davidwesst
Copy link
Contributor Author

@solution-loisir Thanks for the feedback yesterday. I took some time to implement the flag as you suggested. I kept the reference to Image.Util.isRemoteUrl as it's also used on line 53. I figure if we want to remove that reference, it could be done as a separate issue. :)

I added a test that builds the site a second time, with only the flag enabled, so the test-eleventy test directory was updated. All the tests appear to be passing, but take a look and see if there is anything more you think it needs.

@davidwesst davidwesst marked this pull request as ready for review October 8, 2022 01:43
@davidwesst
Copy link
Contributor Author

Oh-- and before it can be merged it would need a version bump to 0.8.2 (or something). :)

@solution-loisir
Copy link
Owner

Great! I had a quick peek and it looks very nice! I'll do a proper review over the weekend when I have time. I'll let you know if there's anything. Thank you so much for this! 😊 Since this is a new feature, version will be bumped to 0.9.0!

@davidwesst
Copy link
Contributor Author

Woot! Sounds like a plan. Looking forward to the feedback. 😊

Copy link
Owner

@solution-loisir solution-loisir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I think about this, the more I feel that it should be left to users to implement for their own use case instead of forcing opinions. So providing a callback instead of a flag that would give access to env should do the trick. Something along this line:

    if(!Image.Util.isRemoteUrl(normalizedTokenAttributes.src) && resolvePath) {
      src = resolvePath(env);
    }

Or maybe even better just pass env to renderImage. I think this would be the most resilient in the ling run. DX could be dealt with by including examples in the docs (this PR would provide a good example for Eleventy!) Tell me what you think.

index.js Outdated
@@ -25,7 +35,10 @@ module.exports = function markdownItEleventyImg(md, {

const normalizedTokenAttributes = generateAttrsObject(token).addContentTo("alt").attrs;

const src = normalizedTokenAttributes.src;
let src = normalizedTokenAttributes.src;
if(!Image.Util.isRemoteUrl(normalizedTokenAttributes.src) && eleventyResolveToProjectRoot == false) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!Image.Util.isRemoteUrl(normalizedTokenAttributes.src) && eleventyResolveToProjectRoot == false) {
if(!Image.Util.isRemoteUrl(normalizedTokenAttributes.src) && env.page.inputPath && eleventyResolveToProjectRoot == false) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking that env.page.inputPath is not undefined. So we don't get an error when used outside Eleventy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes a lot of sense, especially combined with the callback you mentioned above. More specifically the resolvePath callback. Thinking of it in my own usecase, I'm not sure how I would implement just a path resolution logic through the renderImage method.

That being said, if you know how it would work, then feel free to share. :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yhea you're right, renderImage would not let you implement just a path resolution logic. You would have to implement the full rendering logic as well. It's probably better to go with a resolvePath callback, it's more straightforward. And in any case, renderImage will receive the "proper" path anyway.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for env.page.inputPath with the generic resolvePath callback is likely not relevant anymore. As any code tied to a specific context would be implemented in the callback which receive env.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed the resolvePath change, complete with the env variables. I also included the image path, although I'm unsure if that makes the most sense. Thoughts?

index.js Show resolved Hide resolved
@davidwesst
Copy link
Contributor Author

Alright-- updated version has been pushed. I added to our conversation about index.js above, but it feels a lot cleaner overall. I didn't include a typecheck on resolvePath, but it appears to be working with whatever logic the author wants to include. Plus, it defaults back to the original behaviour if nothing is passed, which simplifies the code (and removed the dependency on the Image object for that part).

I did, however, include the filepath to the image being managed. Not sure if that was implicit in our conversation or not, but figured I would mention it.

Lastly-- I updated my relative image test to use the new resolvePath parameter, and it works as expected.

Thoughts?

@solution-loisir
Copy link
Owner

That's great, I'm ready to merge! Thank you so much, it was a pleasure to work this out with you. This feature will be published under v0.9.0! I'll publish in a few days and I let you know. I will likely bring the remote path handling up top so it's above resolvePath and renderImage. That way, remote sources wont have to be handled by users. Cheers! 😄

@solution-loisir solution-loisir merged commit 74eec26 into solution-loisir:master Oct 18, 2022
@davidwesst
Copy link
Contributor Author

It was great working with you too! Thanks for being so responsive. Now to update my own 11ty project to use the latest version of this code. 😊

@davidwesst davidwesst deleted the ft/relative-path-images branch October 20, 2022 02:09
@davidwesst davidwesst restored the ft/relative-path-images branch October 20, 2022 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question about whether the relative path is base on current working dir or current md file?
2 participants