redlib icon indicating copy to clipboard operation
redlib copied to clipboard

Update embedding Reddit preview links to include captions and support i.redd.it embeds

Open ButteredCats opened this issue 1 year ago • 6 comments

Fixes the caption part of https://github.com/redlib-org/redlib/issues/91

Also makes the embedded images lazy load as a bonus.

Using the post in https://github.com/redlib-org/redlib/issues/91 it was kind of hard to tell what was a caption and what wasn't, but it was very clear in something like /r/test/comments/d9hta9/test_multiple_images/. Maybe the captions need tweaking CSS wise to stand out some more but I wasn't sure what the best way to do that was.

The code itself also isn't pretty. Especially removing the extra stuff from the image's caption. I couldn't find a better way to do that or to get the variable in scope for the if else where it determines whether it'll have a figcaption block or not. If there's a better way to go about any of it I heavily encourage you to just go ahead and change it.

ButteredCats avatar Apr 09 '24 22:04 ButteredCats

Sorry for the constant fixes I swear I test this stuff 😅

ButteredCats avatar Apr 09 '24 23:04 ButteredCats

how do i go about and run this locally (https://github.com/ButteredCats/redlib/tree/fix_preview_captions)

grandpa1946 avatar Apr 10 '24 17:04 grandpa1946

You can run:

git clone https://github.com/ButteredCats/redlib
cd redlib/
git checkout fix_preview_captions
cargo run

to test it out.

sigaloid avatar Apr 10 '24 18:04 sigaloid

Just ran into an issue with /r/australian/comments/1c3jn4x/australia_right_now/ where image_caption would be empty, and Redlib would panic. Now it checks if it's empty before trying to mess with it.

Also problems with specifically this comment /r/australian/comments/1c3jn4x/australia_right_now/kzioqh7/?context=3 where there's text inside of the paragraph before the image link, which is something I haven't seen till now. It would fail to embed because image_to_replace doesn't account for this. It seems Reddit refuses to embed things like that as well.

I updated the image replacement not to replace the <p>'s around the image so it would match again, however this makes it so there's extra space at the top and bottom of comments that are only an image and I can't find a fix for it. Check something like /r/travisscott/comments/135ev4e/travis_scott_getting_kicked_out_of_his_own/ if you want to see what that looks like now. It isn't awful, but I wonder if just failing to replace a weird formatting I've never seen before is better than causing that effect on all comments like that which I see pretty often.

ButteredCats avatar Apr 14 '24 21:04 ButteredCats

It looks like there might still be issues with this getting stuck in an infinite loop for some reason. I've been running this branch in order to test and a couple times over the past two weeks I've had my instance die after Redlib starts using a lot of CPU and RAM and stops responding. I'll look into it and comment back with what I find.

ButteredCats avatar May 16 '24 21:05 ButteredCats

Not sure what's up with the failed warnings check, I can't recreate those on my machine and they also don't seem to relate to anything I was touching?

Anyways, turns out the infinite loop had nothing to do with the image replacements themselves, but was instead caused by things like $0, $1, $2, etc to be interpreted as a regex capture group when doing the replace in REDDIT_PREVIEW_REGEX which usually led to a link being inserted over and over and forever replacing itself.

I opted to basically just make it ignore it by directly using a capture group, $2, which has everything after https://preview.redd.it. Since we're not just using the formatted URL anymore, it handles differentiating between normal and external previews by looking at the first capture group of REDDIT_PREVIEW_REGEX and using /preview/pre or /preview/external-pre it based on that.

ButteredCats avatar May 22 '24 21:05 ButteredCats

After updating rust I see those warnings on my machine, however they happen on the main branch which was previously fine so I assume this is just the rust update itself.

I've added a little prettying for embed only comments, reducing weird gaps they had before. I've also added support for embedding i.redd.it links after realizing it'd be really easy to add in by just changing the regex and making it use /img for them.

ButteredCats avatar May 24 '24 01:05 ButteredCats

Thanks!

sigaloid avatar May 29 '24 22:05 sigaloid