🐛 Fix loading attachment via solink in post

This commit is contained in:
LittleSheep 2025-01-05 22:29:48 +08:00
parent bdd6d66e67
commit 176e636f48
5 changed files with 32 additions and 3 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -31,6 +31,7 @@
"rehype-sanitize": "^6.0.0", "rehype-sanitize": "^6.0.0",
"rehype-stringify": "^10.0.1", "rehype-stringify": "^10.0.1",
"remark-breaks": "^4.0.0", "remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0",
"remark-parse": "^11.0.0", "remark-parse": "^11.0.0",
"remark-rehype": "^11.1.1", "remark-rehype": "^11.1.1",
"unified": "^11.0.5", "unified": "^11.0.5",

View File

@ -25,6 +25,7 @@ import rehypeStringify from 'rehype-stringify'
import remarkBreaks from 'remark-breaks' import remarkBreaks from 'remark-breaks'
import remarkParse from 'remark-parse' import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype' import remarkRehype from 'remark-rehype'
import remarkGfm from 'remark-gfm'
import CloseIcon from '@mui/icons-material/Close' import CloseIcon from '@mui/icons-material/Close'
@ -37,7 +38,16 @@ export const getServerSideProps = (async (context) => {
if (post.type != 'article') { if (post.type != 'article') {
processor = processor.use(remarkBreaks) processor = processor.use(remarkBreaks)
} }
const out = await processor.use(remarkRehype).use(rehypeSanitize).use(rehypeStringify).process(post.body.content) post.body.content = post.body.content.replace(
/!\[.*?\]\(solink:\/\/attachments\/([\w-]+)\)/g,
'![alt](https://api.sn.solsynth.dev/cgi/uc/attachments/$1)',
)
const out = await processor
.use(remarkRehype)
.use(remarkGfm)
.use(rehypeSanitize)
.use(rehypeStringify)
.process(post.body.content)
post.body.rawContent = post.body.content post.body.rawContent = post.body.content
post.body.content = String(out) post.body.content = String(out)
} }
@ -102,6 +112,13 @@ export default function Post({ post, attachments }: InferGetServerSidePropsType<
return null return null
}, [post]) }, [post])
const displayableAttachments = useMemo(() => {
if (post.type == 'article') {
return attachments.filter((a) => !a.mimetype.startsWith('image'))
}
return attachments
}, [post])
const [openAppHint, setOpenAppHint] = useState<boolean>() const [openAppHint, setOpenAppHint] = useState<boolean>()
useEffect(() => { useEffect(() => {
@ -203,7 +220,7 @@ export default function Post({ post, attachments }: InferGetServerSidePropsType<
{post.body.content && <div dangerouslySetInnerHTML={{ __html: post.body.content }} />} {post.body.content && <div dangerouslySetInnerHTML={{ __html: post.body.content }} />}
</Box> </Box>
{attachments && ( {displayableAttachments && (
<Grid <Grid
container container
spacing={2} spacing={2}
@ -215,7 +232,7 @@ export default function Post({ post, attachments }: InferGetServerSidePropsType<
lg: Math.min(4, attachments.length), lg: Math.min(4, attachments.length),
}} }}
> >
{attachments.map((a) => ( {displayableAttachments.map((a) => (
<Grid size={1} key={a.id}> <Grid size={1} key={a.id}>
<AttachmentItem item={a} /> <AttachmentItem item={a} />
</Grid> </Grid>

View File

@ -37,6 +37,10 @@ export const getServerSideProps = (async (context) => {
if (post.type != 'article') { if (post.type != 'article') {
processor = processor.use(remarkBreaks) processor = processor.use(remarkBreaks)
} }
post.body.content = post.body.content.replace(
/!\[.*?\]\(solink:\/\/attachments\/([\w-]+)\)/g,
'![alt](https://api.sn.solsynth.dev/cgi/uc/attachments/$1)',
)
const out = await processor const out = await processor
.use(remarkRehype) .use(remarkRehype)
.use(rehypeSanitize) .use(rehypeSanitize)
@ -47,6 +51,9 @@ export const getServerSideProps = (async (context) => {
} }
if (post.body.attachments) { if (post.body.attachments) {
post.attachments = await listAttachment(post.body.attachments) post.attachments = await listAttachment(post.body.attachments)
if (post.type == 'article') {
post.attachments = post.attachments.filter((a) => !a.mimetype.startsWith('image'))
}
} }
posts[idx] = post posts[idx] = post
} }

View File

@ -36,3 +36,7 @@
width: 100%; width: 100%;
} }
} }
.prose img {
border-radius: 8px;
}