Skip to content

Commit

Permalink
youtu.be形式のリンクにも対応
Browse files Browse the repository at this point in the history
  • Loading branch information
takusea committed Oct 22, 2024
1 parent f0136d0 commit 1e7e252
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/features/video-metadata/VideoAddForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function VideoAddForm(props: Props) {

const handleClick = () => {
let metadata: VideoMetadata;
if (url.includes("youtube")) {
if (url.includes("youtu")) {
metadata = parseUrlToYouTubeMetadata(url);
} else if (url.includes("twitch")) {
metadata = parseUrlToTwitchMetadata(url);
Expand Down
14 changes: 11 additions & 3 deletions src/features/video-metadata/parseUrl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { parseTimeToSeconds } from "../../util/timeFormatter";
import type { VideoMetadata } from "./type";

function normalizeYouTubeUrl(url: string) {
if (url.includes("youtu.be")) {
return url.replace("?", "&").replace("youtu.be/", "youtube.com/watch?v=");
}
if (url.includes("live")) {
return url.replace("?", "&").replace("live/", "watch?v=");
}
return url;
}

export function parseUrlToYouTubeMetadata(url: string): VideoMetadata {
const query = url.includes("live")
? url.replace("?", "&").replace("live/", "watch?v=").split("?")[1]
: url.split("?")[1];
const query = normalizeYouTubeUrl(url).split("?")[1];
const params = new URLSearchParams(query);

const id = params.get("v");
Expand Down

0 comments on commit 1e7e252

Please sign in to comment.