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

markdownImages.ts

85 lines2.9 KB

1import {
2 isNetworkPath,
3 isWindowsAbsolutePath,
4 stripSlashPrefixedWindowsDrive,
5 workspaceRelativeFilePath,
6} from "@t3tools/shared/path";
7​
8import {
9 normalizeMarkdownLinkDestination,
10 safeDecodeURIComponent,
11 splitMarkdownLinkSearchAndHash,
12} from "@t3tools/shared/markdownLinks";
13import { parseFileUrlHref } from "@t3tools/shared/fileLinks";
14​
15const DIRECT_IMAGE_SOURCE_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i;
16const URI_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/;
17​
18export type MarkdownImageSource =
19 | { readonly _tag: "Direct"; readonly uri: string }
20 | { readonly _tag: "WorkspaceFile"; readonly path: string }
21 | { readonly _tag: "Blocked" };
22​
23export function markdownImageSourceFragment(source: string): string {
24 return splitMarkdownLinkSearchAndHash(normalizeMarkdownLinkDestination(source)).hash;
26​
27function joinWorkspacePath(workspaceRoot: string, relativePath: string): string {
28 const separator = isWindowsAbsolutePath(workspaceRoot) ? "\\" : "/";
29 const root = workspaceRoot.replace(/[\\/]+$/, "");
30 const path = relativePath.replace(/[\\/]/g, separator).replace(/^[\\/]+/, "");
31 return `${root}${separator}${path}`;
33​
34/** Rendering is not a click, so a network path loads only from the project's own share. */
35function hostFileSource(
36 path: string,
37 workspaceRoot: string | null | undefined,
38): MarkdownImageSource {
39 return isNetworkPath(path) && workspaceRelativeFilePath(path, workspaceRoot) === null
40 ? { _tag: "Blocked" }
41 : { _tag: "WorkspaceFile", path };
43​
44/**
45 * Filesystem paths belong to the environment host and must never reach a
46 * browser or native image component without first becoming a signed asset URL.
47 */
48export function classifyMarkdownImageSource(
49 value: string | null | undefined,
50 workspaceRoot?: string | null,
51): MarkdownImageSource {
52 if (value === null || value === undefined) return { _tag: "Blocked" };
53​
54 const source = normalizeMarkdownLinkDestination(value);
55 if (source.length === 0 || source.startsWith("#") || source.startsWith("?")) {
56 return { _tag: "Blocked" };
57 }
58 if (DIRECT_IMAGE_SOURCE_PATTERN.test(source)) {
59 return { _tag: "Direct", uri: source };
60 }
61​
62 if (/^file:/i.test(source)) {
63 const target = parseFileUrlHref(source);
64 return target === null
65 ? { _tag: "Blocked" }
66 : hostFileSource(
67 stripSlashPrefixedWindowsDrive(safeDecodeURIComponent(target.path)),
68 workspaceRoot,
69 );
70 }
71​
72 const path = stripSlashPrefixedWindowsDrive(
73 safeDecodeURIComponent(splitMarkdownLinkSearchAndHash(source).path),
74 );
75 if (path.length === 0) return { _tag: "Blocked" };
76 if (path.startsWith("/") || isWindowsAbsolutePath(path)) {
77 return hostFileSource(path, workspaceRoot);
78 }
79 if (URI_SCHEME_PATTERN.test(path) || path.startsWith("~/") || path.startsWith("~\\")) {
80 return { _tag: "Blocked" };
81 }
82 if (!workspaceRoot) return { _tag: "Blocked" };
83​
84 return { _tag: "WorkspaceFile", path: joinWorkspacePath(workspaceRoot, path) };