feat: add ability to scrape youtube video

This commit is contained in:
2023-08-02 15:56:33 +02:00
parent cebbb8af2b
commit ba853342bd
10 changed files with 193 additions and 36 deletions

View File

@ -1,30 +1,10 @@
export const isYoutubeLink = (link: string) => {
try {
const url = new URL(link);
return ["youtu.be", "youtube.com"].includes(url.hostname);
} catch (err) {
console.log(err);
return false;
}
};
function extractYoutubeId(link: string) {
const url = new URL(link);
if (url.searchParams.has("v")) {
const id = url.searchParams.get("v");
if (id?.length && id.length > 4) {
return id;
}
}
return url.pathname.replace(/^\//, "");
}
import { extractYoutubeId } from "@lib/string.ts";
export const YoutubePlayer = ({ link }: { link: string }) => {
const id = extractYoutubeId(link);
return (
<iframe
class="mb-6"
width="100%"
height="400px"
src={`https://www.youtube-nocookie.com/embed/${id}`}