diff --git a/lib/plugins/tag/youtube.js b/lib/plugins/tag/youtube.js index b671a2e1cd..8a7ff76aae 100644 --- a/lib/plugins/tag/youtube.js +++ b/lib/plugins/tag/youtube.js @@ -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 diff --git a/test/scripts/tags/youtube.js b/test/scripts/tags/youtube.js index bb18302148..e7baf90750 100644 --- a/test/scripts/tags/youtube.js +++ b/test/scripts/tags/youtube.js @@ -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'); + }); });