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 { marked } from 'marked'
import Layout from '../../layouts/Layout.astro' import Layout from '../../layouts/Layout.astro'
import AttachmentRenderer from '../../components/AttachmentRenderer.astro'
import { getAttachmentUrl, fetchAttachmentMeta } from '../../scripts/attachment' import { getAttachmentUrl, fetchAttachmentMeta } from '../../scripts/attachment'
const { slug } = Astro.params 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 && ( attachments && (
<div class="attachment-list mt-5 gap-4 grid grid-cols-1 md:grid-cols-2"> <div
{attachments.map((attachment) => ( class="attachment-list mt-5 gap-4 grid grid-cols-1"
<div class="attachment"> class:list={
<a href={getAttachmentUrl(attachment.rid)} target="_blank"> attachments.length >= 2 ? 'md:grid-cols-2' : 'md:grid-cols-1'
<img }
src={getAttachmentUrl(attachment.rid)} >
alt={attachment.alt} {attachments.map((attachment) => (
class="rounded-lg" <div class="attachment">
/> <AttachmentRenderer data={attachment} />
</a> </div>
</div> ))}
))} </div>
</div> )
) }
} </article>
</div> </div>
</Layout> </Layout>