Skip to content

Commit 31f3793

Browse files
committed
feat(inline embed): Added functionality for inline embedding of youtube and vimeo videos
1 parent b2d6a00 commit 31f3793

5 files changed

Lines changed: 190 additions & 95 deletions

File tree

dist/embed.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js

Lines changed: 94 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/modules/video/vimeo.es6

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ class Vimeo {
88
this.output = output;
99
this.options = options;
1010
this.embeds = embeds;
11-
this.regex = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)*/gi;
11+
this.regex = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)*/gi;
12+
this.service = 'vimeo'
1213
}
1314

1415
formatData(data) {
1516
return {
16-
title : data.title,
17-
thumbnail : data.thumbnail_medium,
18-
rawDescription : data.description.replace(/\n/g,'<br/>').replace(/&#10;/g, '<br/>'),
19-
views : data.stats_number_of_plays,
20-
likes : data.stats_number_of_likes,
21-
description : utils.truncate(data.description.replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' '), 150),
22-
url : data.url,
23-
id : data.id,
24-
host : 'vimeo'
17+
title: data.title,
18+
thumbnail: data.thumbnail_medium,
19+
rawDescription: data.description.replace(/\n/g, '<br/>').replace(/&#10;/g, '<br/>'),
20+
views: data.stats_number_of_plays,
21+
likes: data.stats_number_of_likes,
22+
description: utils.truncate(data.description.replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' '), 150),
23+
url: data.url,
24+
id: data.id,
25+
host: 'vimeo'
2526
}
2627
}
2728

@@ -39,22 +40,39 @@ class Vimeo {
3940

4041
async process() {
4142
try {
42-
let match;
43-
while ((match = utils.matches(this.regex, this.input)) !== null) {
44-
let embedUrl = `https://player.vimeo.com/video/${match[3]}`;
45-
let data,text;
46-
if(this.options.videoDetails){
47-
data = await this.data(match[3]);
48-
text = helper.detailsTemplate(this.formatData(data), embedUrl)
49-
} else {
50-
text = helper.template(embedUrl, this.options);
43+
if (!utils.ifInline(this.options, this.service)) {
44+
let regexInline = this.options.link ? new RegExp(`([^>]*${this.regex.source})<\/a>`,'gi') : new RegExp(`([^\\s]*${this.regex.source})`,'gi')
45+
let match;
46+
while((match = utils.matches(regexInline, this.output)) !== null){
47+
let id = this.options.link ? match[0].slice(0,-4).split('/').slice(-1).pop() : match[0].split('/').slice(-1).pop()
48+
let embedUrl = `https://player.vimeo.com/video/${id}`
49+
let data = await this.data(id)
50+
let text = helper.detailsTemplate(this.formatData(data), embedUrl)
51+
if(this.options.link){
52+
this.output = !this.options.inlineText ? this.output.replace(match[0],text + '</a>') : this.output.replace(match[0],match[0] + text)
53+
}else{
54+
this.output = !this.options.inlineText ? this.output.replace(match[0],text) : this.output.replace(match[0], match[0] + text)
55+
}
5156
}
57+
} else {
58+
let match;
59+
while ((match = utils.matches(this.regex, this.input)) !== null) {
60+
let embedUrl = `https://player.vimeo.com/video/${match[3]}`;
61+
let data, text;
62+
if (this.options.videoDetails) {
63+
data = await this.data(match[3]);
64+
text = helper.detailsTemplate(this.formatData(data), embedUrl)
65+
} else {
66+
text = helper.template(embedUrl, this.options);
67+
}
5268

53-
this.embeds.push({
54-
text: text,
55-
index: match.index
56-
})
69+
this.embeds.push({
70+
text: text,
71+
index: match.index
72+
})
73+
}
5774
}
75+
5876
return [this.output, this.embeds];
5977
} catch (error) {
6078
console.log(error);
@@ -63,4 +81,3 @@ class Vimeo {
6381
}
6482

6583
module.exports = Vimeo;
66-

0 commit comments

Comments
 (0)