Skip to content

Commit

Permalink
LinkCardコンポーネント暫定
Browse files Browse the repository at this point in the history
  • Loading branch information
gentksb committed Jun 23, 2024
1 parent 046288e commit a61631e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 143 deletions.
2 changes: 1 addition & 1 deletion astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
integrations: [
AutoImport({
imports: [
"./src/components/mdx/LinkBox.astro",
"./src/components/mdx/LinkCard.astro",
{
"./src/components/mdx/positive.tsx": ["PositiveBox"],
"./src/components/mdx/negative.tsx": ["NegativeBox"]
Expand Down
76 changes: 0 additions & 76 deletions src/cache.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/components/mdx/LinkBox.astro

This file was deleted.

51 changes: 51 additions & 0 deletions src/components/mdx/LinkCard.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
import LinkCardLayout from "@layouts/LinkCardLayout.astro"
import { getOgpMetaData } from "@lib/getOgpMetaData"
import type { OgpData } from "@type/ogpData-type"
interface Props {
url: string
linkUrl?: string
}
const { url, linkUrl } = Astro.props
const { env } = Astro.locals.runtime
// check if the url is valid
try {
new URL(url)
if (linkUrl) {
new URL(linkUrl)
}
} catch (error) {
throw new Error("Invalid URL")
}
const getOgpData = async (url: string) => {
// cache key is the base64encoded url
const cacheKey = btoa(url)
const ogpCache: OgpData | null | undefined = await env.OGP_DATASTORE.get(
cacheKey,
{ type: "json" }
)
if (ogpCache) {
return ogpCache
} else {
const ogpMetaData = await getOgpMetaData(url)
await env.OGP_DATASTORE.put(cacheKey, JSON.stringify(ogpMetaData), {
expirationTtl: 60 * 60 * 24
})
return ogpMetaData
}
}
const ogpData = await getOgpData(url)
---

<LinkCardLayout
title={ogpData.ogpTitle ?? url}
siteName={ogpData.ogpSiteName ?? new URL(url).hostname}
description={ogpData.ogpDescription ?? ""}
imageSrc={ogpData.ogpImageUrl}
url={linkUrl ?? url}
/>
2 changes: 1 addition & 1 deletion src/layouts/LinkCardLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface Props {
title: string
description: string
siteName: string
imageSrc: string
imageSrc?: string
url: string
}
Expand Down
18 changes: 0 additions & 18 deletions src/lib/getOgp.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/lib/fetcher/fetchOgp.ts → src/lib/getOgpMetaData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { sanitizeUrl } from "@braintree/sanitize-url"
import { type OgpData } from "@type/ogpData-type"

export const fetchOgp = async (queryUrl: string) => {
export const getOgpMetaData = async (queryUrl: string) => {
const decodedUrl = decodeURIComponent(queryUrl)
const safeUrl = sanitizeUrl(decodedUrl)

const responseBody = await getOgpDatas(safeUrl)
const responseBody = await parseOgpTags(safeUrl)

return responseBody
}

const getOgpDatas = async (href: string): Promise<OgpData> => {
const parseOgpTags = async (href: string): Promise<OgpData> => {
const result: OgpData = {
ogpTitle: "",
ogpImageUrl: "",
Expand Down
4 changes: 2 additions & 2 deletions test/ogpfetcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, test } from "vitest"
import { fetchOgp } from "~/lib/fetcher/fetchOgp"
import { getOgpMetaData } from "@lib/getOgpMetaData"
import { normalLinkUrl, normalLinkDataExpectedResponse } from "./testData"

const encodedUrl = encodeURIComponent(normalLinkUrl)

test("URIエンコードされたURLからOGPデータを取得する", async () => {
const res = await fetchOgp(encodedUrl)
const res = await getOgpMetaData(encodedUrl)
expect(res).deep.equal(normalLinkDataExpectedResponse)
})

0 comments on commit a61631e

Please sign in to comment.