Skip to content

Commit

Permalink
fix: adds support for shortened youtube share links
Browse files Browse the repository at this point in the history
Adds support for shortened YouTube share links with hostnames
like youtu.be.
  • Loading branch information
amygroshek committed Mar 17, 2020
1 parent 5d8de5f commit f78f13f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/__story__/youtube.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ storiesOf('youtube', module)
<Embed url={'https://www.youtube.com/watch?v=soICQ3B2kEk'} />
</Box>
);
})
.add('Shortened URL', () => {
return (
<Box>
<Embed url={'https://youtu.be/soICQ3B2kEk'} />
</Box>
);
});
16 changes: 11 additions & 5 deletions src/routeToBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ const routeTwitter: ReactEmbedRouter = (blocks, {pathname}) => {
return [blocks.tweet, steps[steps.length - 1]];
};

const routeYouTube: ReactEmbedRouter = (blocks, {search}) => {
const matches = search.match(/v=([^\&]+)(&|$)/);
if (!matches) return undefined;
return [blocks.youtube, matches[1]];
const routeYouTube: ReactEmbedRouter = (blocks, parsed) => {
const searchMatch = parsed.search.match(/v=([^\&]+)(&|$)/);
const urlMatch = parsed.pathname.replace('/', '');
if (searchMatch) {
return [blocks.youtube, searchMatch[1]];
} else if (urlMatch) {
return [blocks.youtube, urlMatch];
} else {
return undefined;
}
};

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

const routeToBlock: ReactEmbedRouter = (blocks: Blocks, parsed: ParsedUrl) => {
const {hostname, url} = parsed;

switch (hostname) {
case 'twitter.com':
return routeTwitter(blocks, parsed);
case 'www.youtube.com':
case 'youtu.be':
case 'youtube.com':
return routeYouTube(blocks, parsed);
case 'soundcloud.com':
Expand Down

0 comments on commit f78f13f

Please sign in to comment.