Support more type of attachments

This commit is contained in:
LittleSheep 2024-12-17 22:22:04 +08:00
parent fc4c884ade
commit 44572badcc
2 changed files with 49 additions and 18 deletions

View File

@ -0,0 +1,29 @@
---
import { getAttachmentUrl } from '../scripts/attachment'
interface Props {
data: any
}
const { data: attachment } = Astro.props
---
{
attachment.mimetype.startsWith('image') ? (
<a href={getAttachmentUrl(attachment.rid)} target="_blank">
<img
src={getAttachmentUrl(attachment.rid)}
alt={attachment.alt}
class="rounded-lg"
/>
</a>
) : attachment.mimetype.startsWith('video') ? (
<video src={getAttachmentUrl(attachment.rid)} controls class="rounded-lg" />
) : attachment.mimetype.startsWith('audio') ? (
<audio src={getAttachmentUrl(attachment.rid)} controls class="rounded-lg" />
) : (
<a href={getAttachmentUrl(attachment.rid)} target="_blank">
Unable to preview, but you can open it in your broswer.
</a>
)
}

View File

@ -6,6 +6,7 @@ import { Icon } from 'astro-icon/components'
import { marked } from 'marked'
import Layout from '../../layouts/Layout.astro'
import AttachmentRenderer from '../../components/AttachmentRenderer.astro'
import { getAttachmentUrl, fetchAttachmentMeta } from '../../scripts/attachment'
const { slug } = Astro.params
@ -85,24 +86,25 @@ const attachments = await fetchAttachmentMeta(data.body.attachments)
)
}
<article class="prose max-w-none max-md:prose-lg" set:html={content} />
<article>
<div class="prose max-w-none max-md:prose-lg" set:html={content} />
{
attachments && (
<div class="attachment-list mt-5 gap-4 grid grid-cols-1 md:grid-cols-2">
<div
class="attachment-list mt-5 gap-4 grid grid-cols-1"
class:list={
attachments.length >= 2 ? 'md:grid-cols-2' : 'md:grid-cols-1'
}
>
{attachments.map((attachment) => (
<div class="attachment">
<a href={getAttachmentUrl(attachment.rid)} target="_blank">
<img
src={getAttachmentUrl(attachment.rid)}
alt={attachment.alt}
class="rounded-lg"
/>
</a>
<AttachmentRenderer data={attachment} />
</div>
))}
</div>
)
}
</article>
</div>
</Layout>