mirror of
https://github.com/block/buzz.git
synced 2026-11-14 05:12:27 +00:00
feat(media): support arbitrary file types with download cards (#810)
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
42
desktop/src/shared/ui/markdownFileCard.ts
Normal file
42
desktop/src/shared/ui/markdownFileCard.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
|
||||
|
||||
/** Minimal shape of an imeta entry as consumed by the markdown renderer. */
|
||||
export type FileCardImetaEntry = {
|
||||
m?: string;
|
||||
size?: number;
|
||||
filename?: string;
|
||||
};
|
||||
|
||||
export type ResolvedFileCard = {
|
||||
href: string;
|
||||
filename: string;
|
||||
size?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Decide whether a markdown link should render as a generic-file download
|
||||
* card. A link qualifies when its href matches an imeta entry whose MIME is
|
||||
* neither image nor video (media goes through the `img` renderer instead).
|
||||
*
|
||||
* Pure — extracted from `markdown.tsx` so the FileCard decision (the riskiest
|
||||
* part of the generic-file rendering path) is unit-testable without mounting
|
||||
* React. Returns the resolved card props, or `null` to fall through to normal
|
||||
* link handling.
|
||||
*/
|
||||
export function resolveFileCard(
|
||||
entry: FileCardImetaEntry | undefined,
|
||||
href: string | undefined,
|
||||
childText: string,
|
||||
): ResolvedFileCard | null {
|
||||
if (
|
||||
!href ||
|
||||
!entry?.m ||
|
||||
entry.m.startsWith("image/") ||
|
||||
entry.m.startsWith("video/")
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const filename =
|
||||
entry.filename || childText.trim() || href.split("/").pop() || "file";
|
||||
return { href: rewriteRelayUrl(href), filename, size: entry.size };
|
||||
}
|
||||
Reference in New Issue
Block a user