From 964ae16851e7bcef4db09df9c93212d6cf180161 Mon Sep 17 00:00:00 2001 From: MHZ Date: Sat, 22 Feb 2025 20:46:58 +0800 Subject: [PATCH] feat: support YouTube video thumbnail in link preview (#4427) --- plugin/httpgetter/html_meta.go | 13 +++++++++++++ web/src/components/MemoContent/Link.tsx | 3 +++ 2 files changed, 16 insertions(+) diff --git a/plugin/httpgetter/html_meta.go b/plugin/httpgetter/html_meta.go index 50211569..3ac71948 100644 --- a/plugin/httpgetter/html_meta.go +++ b/plugin/httpgetter/html_meta.go @@ -1,6 +1,7 @@ package httpgetter import ( + "fmt" "io" "net" "net/http" @@ -53,6 +54,7 @@ func GetHTMLMeta(urlStr string) (*HTMLMeta, error) { // TODO: limit the size of the response body htmlMeta := extractHTMLMeta(response.Body) + enrichSiteMeta(response.Request.URL, htmlMeta) return htmlMeta, nil } @@ -151,3 +153,14 @@ func validateURL(urlStr string) error { return nil } + +func enrichSiteMeta(url *url.URL, meta *HTMLMeta) { + if url.Hostname() == "www.youtube.com" { + if url.Path == "/watch" { + vid := url.Query().Get("v") + if vid != "" { + meta.Image = fmt.Sprintf("https://img.youtube.com/vi/%s/mqdefault.jpg", vid) + } + } + } +} diff --git a/web/src/components/MemoContent/Link.tsx b/web/src/components/MemoContent/Link.tsx index 97a9e427..8bfdeaa4 100644 --- a/web/src/components/MemoContent/Link.tsx +++ b/web/src/components/MemoContent/Link.tsx @@ -54,6 +54,9 @@ const Link: React.FC = ({ text, url }: Props) => { {linkMetadata.description && (

{linkMetadata.description}

)} + {linkMetadata.image && ( + {linkMetadata.title} + )} ) }