Skip to content

Conversation

@Ivan-Shaml
Copy link

@Ivan-Shaml Ivan-Shaml commented Dec 25, 2025

What I aim to do with this PR:

  1. Rework the front-end parsing of YouTube links, to make the code cleaner, easier to follow, and reusable.
  • Long regular expressions for non-straightforward tasks, bring more complexity and make the code hard to follow.
    • Before: with one long regex we tried to handle youtu.be and youtube.com URLs, which are structurally different.
    • Now: for each URL type, we handle it accordingly.
  1. Added Scala change, where the URL parsing of in the markdown is fixed.
  • Before: if protocol is omitted (www.youtube.com) the link goes to e.g. https://lichess.org/forum/general-chess-discussion/training-your-chess-intuition-and-board-awareness/www.youtube.com
  • Now: goes to https://www.youtube.com where http:// is added by default, if URL protocol is missing.

Closes #18780

@Ivan-Shaml Ivan-Shaml force-pushed the topic/yt-links-forum branch 5 times, most recently from 1a7e284 to a31bd2a Compare December 25, 2025 22:29
@Ivan-Shaml Ivan-Shaml marked this pull request as ready for review December 25, 2025 22:40
@Ivan-Shaml Ivan-Shaml changed the title Rework yt link parsing Rework youtube link parsing Dec 26, 2025
@fitztrev
Copy link
Member

Could you add a test for the url structure mentioned in #18780 to ensure it doesn't try to embed?

And then would you mind providing a copy+paste-able block of sample URLs that we can put in a blog or forum post to test the behavior locally?

@Ivan-Shaml
Copy link
Author

Links to test with (rick roll not intended)

@Ivan-Shaml Ivan-Shaml force-pushed the topic/yt-links-forum branch 2 times, most recently from bf31ede to 3c70812 Compare December 27, 2025 18:32
@Ivan-Shaml
Copy link
Author

LGTM

@SergioGlorias
Copy link
Member

we can check also:
https://www.youtube.com/live/dQw4w9WgXcQ (URL for livestreams)
https://youtube.com/shorts/dQw4w9WgXcQ (URL for shorts)

@SergioGlorias
Copy link
Member

@Ivan-Shaml
Copy link
Author

Ivan-Shaml commented Dec 29, 2025

also can add playlist:

Hmm, there is an edge-case, if user sends video URL like https://www.youtube.com/watch?v=Nf7oA1Q42Pw&list=PLe5ZNOR8Ttm1HHLAo64Hv4XGa_XU5-a7t&index=1

It will be parsed as regular watch, instead of playlist + watch. Should we handle that ?
P.S: Youtube embed API does not support this, we will treat it as regular video.


private def addProtocolIfNecessary(url: String): String =
if url.matches("(?i)^https?://.*") then url
else s"http://$url"
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably should be https here?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I did it like this initially, but thought what if the server is http only. If its https and correctly configured it will HTTP 301 to the secure url. We don't know what link they will paste in.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In 2025 http:// is fully dead, I haven't seen such a link for a long time.

 - Rename variable
@Ivan-Shaml Ivan-Shaml requested a review from ornicar December 30, 2025 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

URL detection in forums is broken in certain cases

4 participants