Skip to content

Commit

Permalink
Support for hash on Vimeo URLs (#90)
Browse files Browse the repository at this point in the history
Co-authored-by: Gonzalo Massa <gonzalom@xyclon.com.ar>
  • Loading branch information
gonzalo-massa and gonzalom-xyclon committed Nov 16, 2021
1 parent ad135c1 commit 13bc500
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
30 changes: 24 additions & 6 deletions lib/provider/vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ Vimeo.prototype.parseUrl = function(url) {
return match ? match[1] : undefined;
};

Vimeo.prototype.parseParameters = function(params) {
return this.parseTime(params);
Vimeo.prototype.parseHash = function(url) {
var match = url.match(/\/\d+\/(\w+)$/i);
return match ? match[1] : undefined;
};

Vimeo.prototype.parseTime = function(params) {
Vimeo.prototype.parseParameters = function(params) {
if (params.t) {
params.start = getTime(params.t);
delete params.t;
}
if (params.h) {
params.hash = params.h;
delete params.h;
}
return params;
};

Expand All @@ -40,10 +45,14 @@ Vimeo.prototype.parse = function(url, params) {
params: this.parseParameters(params),
id: this.parseUrl(url),
};
var hash = this.parseHash(url, params);
if (hash) {
result.params.hash = hash;
}
return result.id ? result : undefined;
};

Vimeo.prototype.createUrl = function(baseUrl, vi, params) {
Vimeo.prototype.createUrl = function(baseUrl, vi, params, type) {
if (!vi.id || vi.mediaType !== this.mediaTypes.VIDEO) {
return undefined;
}
Expand All @@ -52,6 +61,15 @@ Vimeo.prototype.createUrl = function(baseUrl, vi, params) {
var startTime = params.start;
delete params.start;

if (params.hash) {
if (type === 'embed') {
params.h = params.hash;
} else if (type === 'long') {
url += '/' + params.hash;
}
delete params.hash;
}

url += combineParams(params);

if (startTime) {
Expand All @@ -61,11 +79,11 @@ Vimeo.prototype.createUrl = function(baseUrl, vi, params) {
};

Vimeo.prototype.createLongUrl = function(vi, params) {
return this.createUrl('https://vimeo.com/', vi, params);
return this.createUrl('https://vimeo.com/', vi, params, 'long');
};

Vimeo.prototype.createEmbedUrl = function(vi, params) {
return this.createUrl('//player.vimeo.com/video/', vi, params);
return this.createUrl('//player.vimeo.com/video/', vi, params, 'embed');
};

require('../base').bind(new Vimeo());
27 changes: 22 additions & 5 deletions lib/provider/vimeo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ function newParser() {

test('Vimeo: undefined', () => {
expect(newParser().parse('https://vimeo.com')).toBe(undefined);
expect(newParser().create({ videoInfo: { provider: 'vimeo' } })).toBe(undefined);
expect(newParser().create({ videoInfo: { provider: 'vimeo', mediaType: 'video' } })).toBe(undefined);
expect(newParser().create({ videoInfo: { provider: 'vimeo', mediaType: 'video' }, format: 'embed' })).toBe(undefined);
expect(newParser().create({ videoInfo: { provider: 'vimeo', mediaType: 'video' }, format: 'image' })).toBe(undefined);
expect(newParser().create({videoInfo: {provider: 'vimeo'}})).toBe(undefined);
expect(newParser().create({videoInfo: {provider: 'vimeo', mediaType: 'video'}})).toBe(undefined);
expect(newParser().create({videoInfo: {provider: 'vimeo', mediaType: 'video'}, format: 'embed'})).toBe(undefined);
expect(newParser().create({videoInfo: {provider: 'vimeo', mediaType: 'video'}, format: 'image'})).toBe(undefined);
});

test('Vimeo: urls', () => {
Expand Down Expand Up @@ -94,4 +94,21 @@ test('Vimeo: urls', () => {
'//player.vimeo.com/video/36881035#t=3m28s',
],
});
});
testUrls(newParser(), {
videoInfo: {
provider: 'vimeo',
id: '36881035',
mediaType: 'video',
params: {
hash: 'f9ad567296',
},
},
formats: {
long: 'https://vimeo.com/36881035/f9ad567296',
embed: '//player.vimeo.com/video/36881035?h=f9ad567296',
},
urls: ['https://vimeo.com/36881035/f9ad567296',
'//player.vimeo.com/video/36881035?h=f9ad567296',
],
});
});

0 comments on commit 13bc500

Please sign in to comment.