YouTube link embeds
This commit is contained in:
parent
b3551cbe9a
commit
75fbed4a4d
29
pkg/expand_media.go
Normal file
29
pkg/expand_media.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package barertc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Media regexps
|
||||||
|
var (
|
||||||
|
ytLinkRegexp = regexp.MustCompile(`(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})`)
|
||||||
|
ytIdRegexp = regexp.MustCompile(`[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]`) // YT ID validator
|
||||||
|
)
|
||||||
|
|
||||||
|
// YT embed template
|
||||||
|
const youtubeEmbedTemplate = `<iframe class="youtube-embed" width="560" height="315" src="https://www.youtube.com/embed/%s" title="YouTube video player" frameborder="0" allow="autoplay; encrypted-media; picture-in-picture; web-share" allowfullscreen></iframe>`
|
||||||
|
|
||||||
|
// ExpandMedia detects media URLs such as YouTube videos and stylizes the message up with embeds.
|
||||||
|
func (s *Server) ExpandMedia(message string) string {
|
||||||
|
// YouTube links.
|
||||||
|
if m := ytLinkRegexp.FindStringSubmatch(message); len(m) > 0 {
|
||||||
|
var ytid = m[1]
|
||||||
|
|
||||||
|
// Sanity check the ID parsed OK (e.g. multiple youtube links can throw it off)
|
||||||
|
if ytIdRegexp.Match([]byte(ytid)) {
|
||||||
|
message += fmt.Sprintf(youtubeEmbedTemplate, ytid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return message
|
||||||
|
}
|
|
@ -120,6 +120,9 @@ func (s *Server) OnMessage(sub *Subscriber, msg Message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect and expand media such as YouTube videos.
|
||||||
|
markdown = s.ExpandMedia(markdown)
|
||||||
|
|
||||||
// Message to be echoed to the channel.
|
// Message to be echoed to the channel.
|
||||||
var message = Message{
|
var message = Message{
|
||||||
Action: ActionMessage,
|
Action: ActionMessage,
|
||||||
|
|
|
@ -238,3 +238,8 @@ body {
|
||||||
font-size: small;
|
font-size: small;
|
||||||
padding: 2px 4px;
|
padding: 2px 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* YouTube embeds */
|
||||||
|
.youtube-embed {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user