mirror of
https://github.com/m1ngsama/FUJI.git
synced 2025-12-25 02:56:38 +00:00
Merge pull request #103 from wen-templari/feature/add-cover-image-utility
Update OG Image, Add getCoverImage utility function and update layouts
This commit is contained in:
commit
b54db897e6
4 changed files with 20 additions and 5 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 189 KiB After Width: | Height: | Size: 132 KiB |
|
|
@ -3,7 +3,7 @@ import BaseHead from "../components/header/BaseHead.astro"
|
||||||
import Header from "../components/header/Header.astro"
|
import Header from "../components/header/Header.astro"
|
||||||
import Footer from "../components/Footer.astro"
|
import Footer from "../components/Footer.astro"
|
||||||
|
|
||||||
import { formatDate } from "../utils"
|
import { formatDate, getCoverImage } from "../utils"
|
||||||
import { SITE_TITLE } from "../consts"
|
import { SITE_TITLE } from "../consts"
|
||||||
const { frontmatter } = Astro.props
|
const { frontmatter } = Astro.props
|
||||||
const type = frontmatter.tags?.[0] ?? "默认"
|
const type = frontmatter.tags?.[0] ?? "默认"
|
||||||
|
|
@ -20,7 +20,7 @@ const dateFormatted = formatDate(pubDate)
|
||||||
<BaseHead
|
<BaseHead
|
||||||
title={`${title} - ${SITE_TITLE}`}
|
title={`${title} - ${SITE_TITLE}`}
|
||||||
description={description}
|
description={description}
|
||||||
image={frontmatter.cover?.square}
|
image={getCoverImage(frontmatter.cover)}
|
||||||
/>
|
/>
|
||||||
</head>
|
</head>
|
||||||
<body
|
<body
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
---
|
---
|
||||||
import { CA_LOGO_URL } from "../consts"
|
import { formatDate, getCoverImage } from "../utils"
|
||||||
import { formatDate } from "../utils"
|
|
||||||
const { title, href, cover, tags, date } = Astro.props
|
const { title, href, cover, tags, date } = Astro.props
|
||||||
const dateFormatted = formatDate(date)
|
const dateFormatted = formatDate(date)
|
||||||
const type = tags?.[0] ?? "默认"
|
const type = tags?.[0] ?? "默认"
|
||||||
|
|
||||||
const image = cover?.url ?? cover ?? CA_LOGO_URL
|
const image = getCoverImage(cover)
|
||||||
|
|
||||||
const label = `${title} - ${type} - 发表时间 ${dateFormatted}`;
|
const label = `${title} - ${type} - 发表时间 ${dateFormatted}`;
|
||||||
---
|
---
|
||||||
|
|
|
||||||
16
src/utils.ts
16
src/utils.ts
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { CA_LOGO_URL } from "./consts"
|
||||||
|
|
||||||
export function formatDate(dateString: string) {
|
export function formatDate(dateString: string) {
|
||||||
const date = new Date(dateString)
|
const date = new Date(dateString)
|
||||||
const year = date.getFullYear()
|
const year = date.getFullYear()
|
||||||
|
|
@ -16,3 +18,17 @@ export function formatDateV2(date: string) {
|
||||||
// 返回格式化后的日期
|
// 返回格式化后的日期
|
||||||
return formattedDate
|
return formattedDate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getCoverImage = (cover: unknown) => {
|
||||||
|
switch (typeof cover) {
|
||||||
|
case "string":
|
||||||
|
return cover
|
||||||
|
case "object":
|
||||||
|
if (cover && "url" in cover) {
|
||||||
|
return (cover as { url: string }).url
|
||||||
|
}
|
||||||
|
return CA_LOGO_URL
|
||||||
|
default:
|
||||||
|
return CA_LOGO_URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue