Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdownMedia.ts

113 lines3.9 KB

1import {
2 classifyMarkdownImageSource,
3 markdownImageSourceFragment,
4} from "@t3tools/client-runtime/markdown-images";
5import { splitFilePathPosition } from "@t3tools/shared/fileLinks";
6import {
7 mediaFileReference,
8 mediaUrlReference,
9 mediaReferenceFileName,
10} from "@t3tools/client-runtime/media-reference";
11import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
12import { normalizeNativeMarkdownUrl } from "./markdownLinks";
13import { resolveMobileMarkdownMediaSource } from "./markdownMediaSource";
14​
15import type { FilePreviewSource } from "../components/FilePreviewModal";
16import type { MediaVideoPreviewSource } from "./videoPreviewSource";
17import type { MediaActionsSource } from "./mediaActionsSource";
18​
19export function resolveMarkdownMediaPreview(
20 href: string,
21 input: {
22 readonly environmentId: EnvironmentId;
23 readonly threadId: ThreadId | undefined;
24 readonly workspaceRoot: string | null | undefined;
25 readonly captured?: boolean;
26 readonly imageEmbed?: boolean;
27 },
28):
29 | { readonly kind: "image"; readonly source: FilePreviewSource }
30 | { readonly kind: "pdf"; readonly source: FilePreviewSource }
31 | { readonly kind: "video"; readonly source: MediaVideoPreviewSource }
32 | null {
33 const classified = classifyMarkdownImageSource(href, input.workspaceRoot);
34 if (classified._tag !== "Blocked") {
35 const path =
36 classified._tag === "Direct" ? classified.uri : splitFilePathPosition(classified.path).path;
37 const reference =
38 classified._tag === "Direct"
39 ? mediaUrlReference(path)
40 : mediaFileReference(path, input.workspaceRoot);
41 const name = reference && mediaReferenceFileName(reference);
42 if (name && /\.pdf$/i.test(name)) {
43 const common = { kind: "pdf" as const, name, mimeType: "application/pdf" };
44 if (classified._tag === "Direct") {
45 const uri = normalizeNativeMarkdownUrl(path);
46 return {
47 kind: "pdf",
48 source: {
49 ...common,
50 uri,
51 actionsSource: {
52 ...(reference ? { reference } : {}),
53 uri,
54 name,
55 mimeType: common.mimeType,
56 },
57 },
58 };
59 }
60 if (input.captured) return null;
61 const resource = input.threadId
62 ? { _tag: "media-file" as const, threadId: input.threadId, path }
63 : input.workspaceRoot
64 ? { _tag: "draft-workspace-file" as const, cwd: input.workspaceRoot, path }
65 : null;
66 if (!resource) return null;
67 return {
68 kind: "pdf",
69 source: {
70 ...common,
71 environmentId: input.environmentId,
72 resource,
73 srcFragment: markdownImageSourceFragment(href),
74 actionsSource: {
75 ...(reference ? { reference } : {}),
76 environmentId: input.environmentId,
77 threadId: input.threadId,
78 resource,
79 name,
80 mimeType: common.mimeType,
81 },
82 },
83 };
84 }
85 }
86 const media = resolveMobileMarkdownMediaSource(href, input);
87 if (media === null || media.access === "unavailable") return null;
88 if (input.captured && media.access !== "direct") return null;
89 const { kind, name, mimeType, reference, srcFragment } = media;
90​
91 const target =
92 media.access === "direct"
93 ? { uri: normalizeNativeMarkdownUrl(media.uri) }
94 : {
95 environmentId: input.environmentId,
96 resource: media.resource,
97 ...(srcFragment ? { srcFragment } : {}),
98 };
99 const actionsSource: MediaActionsSource =
100 media.access === "direct"
101 ? { reference, uri: media.uri, name, mimeType }
102 : {
103 reference,
104 environmentId: input.environmentId,
105 threadId: input.threadId,
106 resource: media.resource,
107 name,
108 mimeType,
109 };
110 return kind === "video"
111 ? { kind, source: { type: "media", name, mimeType, ...target, actionsSource } }
112 : { kind, source: { kind, name, ...target, actionsSource } };