From 7df5ef872c67f478e62f064db9c9ef7463cd65f5 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sun, 18 May 2025 02:36:31 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=B8=E3=82=A7?= =?UTF-8?q?=E3=82=AF=E3=83=88=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=ABfavicon?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bun.lock | 1 + package.json | 1 + src/pages/projects/[...id].astro | 31 ++++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bun.lock b/bun.lock index 8c52b4f5..e79fafc6 100644 --- a/bun.lock +++ b/bun.lock @@ -14,6 +14,7 @@ "astro": "^5.7.12", "astro-icon": "^1.1.5", "bits-ui": "^1.4.8", + "cheerio": "^1.0.0", "daisyui": "^5.0.35", "date-fns": "^4.1.0", "markdown-to-txt": "^2.0.1", diff --git a/package.json b/package.json index 99ae6842..6f0b21e3 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "astro": "^5.7.12", "astro-icon": "^1.1.5", "bits-ui": "^1.4.8", + "cheerio": "^1.0.0", "daisyui": "^5.0.35", "date-fns": "^4.1.0", "markdown-to-txt": "^2.0.1", diff --git a/src/pages/projects/[...id].astro b/src/pages/projects/[...id].astro index 03c16240..d9fcf587 100644 --- a/src/pages/projects/[...id].astro +++ b/src/pages/projects/[...id].astro @@ -8,6 +8,7 @@ import { getProjects } from "+/query"; import { Focus } from "+/schema.ts"; import type { GetStaticPaths } from "astro"; import { Icon } from "astro-icon/components"; +import { load as cheerioLoad } from "cheerio"; export const getStaticPaths = (async () => { const projects = await getProjects("all"); @@ -18,6 +19,30 @@ export const getStaticPaths = (async () => { }) satisfies GetStaticPaths; const { project } = Astro.props; const { Content } = await render(project); + +let iconSrc: string | undefined = undefined; +if (project.data.website && project.data.status !== "dead") { + const websiteRes = await fetch(project.data.website); + if (websiteRes.ok) { + const websiteHtml = await websiteRes.text(); + const website = cheerioLoad(websiteHtml); + const iconHref = website("link[rel='icon'], link[rel='shortcut icon']")[0] + ?.attribs.href; + if (iconHref) { + iconSrc = new URL(iconHref, project.data.website).toString(); + } else { + const faviconRes = await fetch( + `${new URL(project.data.website).origin}/favicon.ico`, + ); + if ( + faviconRes.ok && + !faviconRes.headers.get("content-type")?.startsWith("text/") + ) { + iconSrc = faviconRes.url; + } + } + } +} --- - + {iconSrc ? ( + favicon + ) : ( + + )} {project.data.title} へ ) From 665b15d675d31c35c7ddad18fd1cd3a05b0c3760 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sun, 18 May 2025 02:49:39 +0900 Subject: [PATCH 2/2] =?UTF-8?q?base64=E3=81=A7=E5=9F=8B=E3=82=81=E8=BE=BC?= =?UTF-8?q?=E3=82=93=E3=81=A0=E3=81=BB=E3=81=86=E3=81=8C=E9=80=9F=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/projects/[...id].astro | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/projects/[...id].astro b/src/pages/projects/[...id].astro index d9fcf587..5f4857dd 100644 --- a/src/pages/projects/[...id].astro +++ b/src/pages/projects/[...id].astro @@ -29,7 +29,8 @@ if (project.data.website && project.data.status !== "dead") { const iconHref = website("link[rel='icon'], link[rel='shortcut icon']")[0] ?.attribs.href; if (iconHref) { - iconSrc = new URL(iconHref, project.data.website).toString(); + const faviconRes = await fetch(new URL(iconHref, project.data.website)); + iconSrc = `data:${faviconRes.headers.get("content-type")};base64,${Buffer.from(await faviconRes.arrayBuffer()).toString("base64")}`; } else { const faviconRes = await fetch( `${new URL(project.data.website).origin}/favicon.ico`, @@ -38,7 +39,7 @@ if (project.data.website && project.data.status !== "dead") { faviconRes.ok && !faviconRes.headers.get("content-type")?.startsWith("text/") ) { - iconSrc = faviconRes.url; + iconSrc = `data:${faviconRes.headers.get("content-type")};base64,${Buffer.from(await faviconRes.arrayBuffer()).toString("base64")}`; } } }