Skip to content

Commit 62b130a

Browse files
committed
normalize and fix youtube embeds - with support for start - closes lichess-org#2630
1 parent b3dff35 commit 62b130a

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

public/javascripts/embed-analyse.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ $(function() {
22

33
var studyRegex = /lichess\.org\/study\/(?:embed\/)?(\w{8})\/(\w{8})(#\d+)?\b/;
44
var gameRegex = /lichess\.org\/(?:embed\/)?(\w{8})(?:(?:\/(white|black))|\w{4}|)(#\d+)?\b/;
5-
var youtubeRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch)?(?:\?v=)?([^"&?\/ ]{11})(?:[^\s]*)/i;
65
var notGames = ['training', 'analysis', 'insights', 'practice'];
76

87
var parseLink = function(a) {
9-
var matches = a.href.match(youtubeRegex);
10-
if (matches) return {
8+
var yt = lichess.toYouTubeEmbedUrl(a.href);
9+
if (yt) return {
1110
type: 'youtube',
12-
src: 'https://www.youtube.com/embed/' + matches[1]
11+
src: yt
1312
};
1413
var matches = a.href.match(studyRegex);
1514
if (matches && matches[2] && a.text.match(studyRegex)) return {

public/javascripts/util.js

+17
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,23 @@ lichess.escapeHtml = function(html) {
380380
div.appendChild(document.createTextNode(html));
381381
return div.innerHTML;
382382
};
383+
lichess.toYouTubeEmbedUrl = function(url) {
384+
var m = url.match(/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch)?(?:\?v=)?([^"&?\/ ]{11})(?:\?|&|)(\S*)/i);
385+
if (!m) return;
386+
var start = 1;
387+
m[2].split('&').forEach(function(p) {
388+
var s = p.split('=');
389+
if (s[0] === 't' || s[0] === 'start') {
390+
if (s[1].match(/^\d+$/)) start = parseInt(s[1]);
391+
else {
392+
var n = s[1].match(/(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?/);
393+
start = (parseInt(n[1]) || 0) * 3600 + (parseInt(n[2]) || 0) * 60 + (parseInt(n[3]) || 0);
394+
}
395+
}
396+
});
397+
var params = 'modestbranding=1&rel=0&controls=2&iv_load_policy=3&start=' + start;
398+
return 'https://www.youtube.com/embed/' + m[1] + '?' + params;
399+
};
383400
$.spreadNumber = function(el, nbSteps, getDuration, previous) {
384401
var previous = previous,
385402
displayed;

public/stylesheets/study.css

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ div.underboard .notif.error {
123123
margin-bottom: 10px;
124124
}
125125
.study_comments .text {
126+
width: 100%;
126127
overflow: hidden;
127128
word-wrap: break-word;
128129
}

ui/analyse/src/study/studyComments.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ function authorText(author) {
1515
return author.name;
1616
}
1717

18-
var commentYoutubeRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch)?(?:\?v=)?([^"&?\/ ]{11})(?:[^\s]*)/gi;
18+
var commentYoutubeRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch)?(?:\?v=)?(?:[^"&?\/ ]{11})\S*/gi;
1919

2020
function embedYoutube(text) {
21-
return m.trust(lichess.escapeHtml(text).replace(commentYoutubeRegex, function(m, id) {
22-
return '<iframe width="100%" height="300" src="https://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe>';
21+
return m.trust(lichess.escapeHtml(text).replace(commentYoutubeRegex, function(found) {
22+
var url = lichess.toYouTubeEmbedUrl(found);
23+
if (!url) return found;
24+
return '<iframe width="100%" height="300" src="' + url + '" frameborder=0 allowfullscreen></iframe>';
2325
}));
2426
}
2527

@@ -54,7 +56,7 @@ module.exports = {
5456
m('span.node', nodeFullName(node))
5557
] : null,
5658
': ',
57-
m('span.text', embedYoutube(comment.text))
59+
m('div.text', embedYoutube(comment.text))
5860
]);
5961
}));
6062
}

0 commit comments

Comments
 (0)