Skip to content

Commit

Permalink
feat(youtube_tag): add cookie option (#4155)
Browse files Browse the repository at this point in the history
* feat(youtube_tag): add cookie option

* refactor(youtube_tag): reduce variable
  • Loading branch information
curbengh authored Feb 25, 2020
1 parent 225abd5 commit e6af2b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/plugins/tag/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ const { htmlTag } = require('hexo-util');
* Youtube tag
*
* Syntax:
* {% youtube video_id, type %}
* {% youtube video_id, type, cookie %}
*/

function youtubeTag([id, type = 'video']) {
let src;

if (type === 'video') {
src = 'https://www.youtube.com/embed/' + id;
} else if (type === 'playlist') {
src = 'https://www.youtube.com/embed/videoseries?list=' + id;
function youtubeTag([id, type = 'video', cookie = true]) {
if (typeof type === 'boolean') {
cookie = type;
type = 'video';
}

const ytLink = cookie ? 'https://www.youtube.com' : 'https://www.youtube-nocookie.com';
const embed = type === 'video' ? '/embed/' : '/embed/videoseries?list=';

const iframeTag = htmlTag('iframe', {
src,
src: ytLink + embed + id,
frameborder: '0',
loading: 'lazy',
allowfullscreen: true
Expand Down
11 changes: 11 additions & 0 deletions test/scripts/tags/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ describe('youtube', () => {
$playlist('.video-container').html().should.be.ok;
$playlist('iframe').attr('src').should.eql('https://www.youtube.com/embed/videoseries?list=foo');
});

it('cookie', () => {
const $video1 = cheerio.load(youtube(['foo', 'video', false]));
$video1('.video-container').html().should.be.ok;
$video1('iframe').attr('src').should.eql('https://www.youtube-nocookie.com/embed/foo');

// cookie as second parameter
const $video2 = cheerio.load(youtube(['foo', false]));
$video2('.video-container').html().should.be.ok;
$video2('iframe').attr('src').should.eql('https://www.youtube-nocookie.com/embed/foo');
});
});

0 comments on commit e6af2b3

Please sign in to comment.