Skip to content

Commit f78f13f

Browse files
committed
fix: adds support for shortened youtube share links
Adds support for shortened YouTube share links with hostnames like youtu.be.
1 parent 5d8de5f commit f78f13f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/__story__/youtube.story.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ storiesOf('youtube', module)
1313
<Embed url={'https://www.youtube.com/watch?v=soICQ3B2kEk'} />
1414
</Box>
1515
);
16+
})
17+
.add('Shortened URL', () => {
18+
return (
19+
<Box>
20+
<Embed url={'https://youtu.be/soICQ3B2kEk'} />
21+
</Box>
22+
);
1623
});

src/routeToBlock.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ const routeTwitter: ReactEmbedRouter = (blocks, {pathname}) => {
77
return [blocks.tweet, steps[steps.length - 1]];
88
};
99

10-
const routeYouTube: ReactEmbedRouter = (blocks, {search}) => {
11-
const matches = search.match(/v=([^\&]+)(&|$)/);
12-
if (!matches) return undefined;
13-
return [blocks.youtube, matches[1]];
10+
const routeYouTube: ReactEmbedRouter = (blocks, parsed) => {
11+
const searchMatch = parsed.search.match(/v=([^\&]+)(&|$)/);
12+
const urlMatch = parsed.pathname.replace('/', '');
13+
if (searchMatch) {
14+
return [blocks.youtube, searchMatch[1]];
15+
} else if (urlMatch) {
16+
return [blocks.youtube, urlMatch];
17+
} else {
18+
return undefined;
19+
}
1420
};
1521

1622
const routeJsFiddle: ReactEmbedRouter = (blocks, {pathname}) => {
@@ -56,11 +62,11 @@ const routeGfycat: ReactEmbedRouter = (blocks, {pathname}) => {
5662

5763
const routeToBlock: ReactEmbedRouter = (blocks: Blocks, parsed: ParsedUrl) => {
5864
const {hostname, url} = parsed;
59-
6065
switch (hostname) {
6166
case 'twitter.com':
6267
return routeTwitter(blocks, parsed);
6368
case 'www.youtube.com':
69+
case 'youtu.be':
6470
case 'youtube.com':
6571
return routeYouTube(blocks, parsed);
6672
case 'soundcloud.com':

0 commit comments

Comments
 (0)