bkad / prat

group chat with markdown served over websockets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Video embeds

cespare opened this issue · comments

Youtube, vimeo, etc.

I started doing this but then realized it's a pain in the ass because of how callbacks work with sundown.

Here's a regex for youtube.com links to get started:

r"(https?://)?(www\.)?youtube\.com/watch\?\w*v=(?P<id>[0-9A-Za-z+)\w*"

Can't youtube links also have the host youtu.be?

Sure. Extending the regex is the easy part. Hooking into the link processing part of the markdown is the f'ing annoying part.

As a reference, in order to add the target="_blank" attribute for autolinks, I had to modify the C code directly. bkad/misaka@ccb20ee

The reason for it is because the library only allows you to override the callback directly. You can't, for example, take the output of the autolink output that Sundown generates, modify it Python, and pass it back into the buffer. If you want to keep most of the native logic but modify it slightly, you have two choices: 1.) Duplicate all the native code in the Python override method. or 2.) Modify the Sundown code directly. I went with the latter option, just because it wasn't too complicated for my particular modification.

There might be a third option. We can add in pre and post processors to the renderer in python, so it's possible to do a regex match for something like youtube links, replace them with a token (e.g. youtube:) so that the autolinker doesn't try and apply formatting to the url, then output the embedded iframe in the post processing pass. See https://github.com/bkad/oochat/tree/video_embedding for a prototype of this.

Unfortunately this approach will walk all over youtube links in ``` defined code blocks.

Too much of a hack.

Having found several ways to break my code in the last 5 minutes. I agree with @bkad. This is going to be a pain to do right.