Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,12 @@ https://twitter.com/wesbos/timelines/1189618481672667136

### YouTube

The YouTube transformer (currently) only supports videos in the following
formats:
The YouTube transformer (currently) supports videos and channels in the
following formats:

- Full url (`https://youtube.com/watch?v=dQw4w9WgXcQ`)
- Short url (`https://youtu.be/dQw4w9WgXcQ`)
- Channel url (`https://youtube.com/user/kentdoddsfamily`)

#### Usage

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/transformers/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ cases(
},
'user full url': {
url: 'https://youtube.com/user/kentdoddsfamily',
valid: false,
valid: true,
},
'user short url': {
url: 'https://youtube.com/kentdoddsfamily',
Expand Down Expand Up @@ -175,7 +175,7 @@ test('Plugin can transform YouTube links', async () => {

<https://youtube.com/playlist?list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u>

<https://youtube.com/user/kentdoddsfamily>
<iframe width=\\"100%\\" height=\\"315\\" src=\\"https://www.youtube.com/embed?listType=user_uploads&list=kentdoddsfamily\\" frameBorder=\\"0\\" allow=\\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\\" allowFullScreen></iframe>

<https://youtube.com/kentdoddsfamily>
"
Expand Down
11 changes: 9 additions & 2 deletions src/transformers/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const shouldTransform = (url) => {
return (
host === 'youtu.be' ||
(['youtube.com', 'www.youtube.com'].includes(host) &&
pathname.includes('/watch') &&
Boolean(searchParams.get('v')))
((pathname.includes('/watch') && Boolean(searchParams.get('v'))) ||
pathname.includes('/user/')))
);
};

Expand All @@ -24,6 +24,13 @@ export const getTimeValueInSeconds = (timeValue) => {
};
export const getYouTubeIFrameSrc = (urlString) => {
const url = new URL(urlString);
if (urlString.includes('/user/')) {
const channelID = urlString.split('/user/')[1];
const channelEmbed =
'https://www.youtube.com/embed?listType=user_uploads&list=';
return channelEmbed + channelID;
}

let id = url.searchParams.get('v');
if (url.host === 'youtu.be') {
id = url.pathname.slice(1);
Expand Down