go-gitea / gitea

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
https://gitea.com
MIT License
44.42k stars 5.43k forks source link

[FR] Code preview in opengraph image when linking to file #25495

Open nyxkrage opened 1 year ago

nyxkrage commented 1 year ago

Feature Description

Currently, when a link to a file on Gitea is shared, the OpenGraph image is simply the user's or organization's profile picture. I personally don't think this is a very good or informative use of the space.

Instead, I propose that we can, like Sourcegraph, show a preview of the file that is being linked to. This would provide an instant overview of the code without having to click a link.

I have on my personal fork of Gitea implemented this in a very basic state, using jiro4989/textimg, and alecthomas/chroma, which uses chroma to syntax highlight with ANSI escape codes, and then using the parser from textimg to generate a list of tokens that should be highlighted, and then it creates the actual image with gg. However, my implementation isn't very ideal since the URL being used is ROOT_URL/org/repo/src/og/<branch>/<path>, it currently only uses Catppuccin Mocha for the highlighting, and there is no form of caching.

There are some important differences that there should be from my primitive implementation that should probably be discussed before any work starts on this.

My implementation can be seen here: OpenGraph Changes on my Gitea fork

Screenshots

Current Behaviour opengraph embed on discord when linking to a file on gitea

Proposed Behavior opengraph embed on discord when linking to a file on sourcegraph opengraph embed on discord when linking to a file on my fork of gitea

jolheiser commented 1 year ago

The changes seems fairly self-contained, which is nice to see.

I think an option to (dis/en)able it would be good, as I'd imagine for smaller instances you'd end up with either more load with caching off, or more storage usage if each image is cached.

In terms of color scheme, perhaps configurable and use the instance default if unset.

The URL route I don't have a strong opinion on, the proposed one sounds okay to me.

lunny commented 1 year ago

The loading should be on frontend and the markup parser could have a special class name for them.

nyxkrage commented 1 year ago

I think an option to (dis/en)able it would be good

Yeah this does seem like a necessity, I'd imagine the extra load would be quite undesirable for most instances. Keeping it turned off by default, just like LFS, seems like the expected behavior to me.

The loading should be on frontend...

I'm not quite sure whats meant by this. If you mean that the image should be generated on the frontend, then I don't believe that is possible with the way OpenGraph works, as it needs a proper URL to an image file.