| 1 | import { desktopHost } from "./desktopHost"; |
| 2 | |
| 3 | export const REMOTE_MARKDOWN_IMAGE_PATH = "/__reasonix_remote_markdown_image"; |
| 4 | |
| 5 | export interface MarkdownImageView { |
| 6 | url: string; |
| 7 | filename?: string; |
| 8 | mime?: string; |
| 9 | size?: number; |
| 10 | openHref?: string; |
| 11 | errorCode?: string; |
| 12 | } |
| 13 | |
| 14 | function runningInDesktopShell(): boolean { |
| 15 | return desktopHost().kind !== "none"; |
| 16 | } |
| 17 | |
| 18 | export function hasMarkdownImageResolver(): boolean { |
| 19 | return typeof desktopHost().app?.ResolveMarkdownImageForTab === "function"; |
| 20 | } |
| 21 | |
| 22 | // Route only absolute remote Markdown images back through the local desktop |
| 23 | // asset origin; relative, data, blob, and workspace-media URLs remain local |
| 24 | // and unchanged. |
| 25 | export function markdownImageSource(src: string | undefined, nativeShell = runningInDesktopShell()): string { |
| 26 | const value = src?.trim() ?? ""; |
| 27 | if (!nativeShell || value === "") return value; |
| 28 | |
| 29 | let remoteURL = value; |
| 30 | if (value.startsWith("//")) { |
| 31 | const protocol = typeof window !== "undefined" && /^https?:$/.test(window.location.protocol) |
| 32 | ? window.location.protocol |
| 33 | : "https:"; |
| 34 | remoteURL = protocol + value; |
| 35 | } else if (!/^https?:\/\//i.test(value)) { |
| 36 | return value; |
| 37 | } |
| 38 | |
| 39 | return `${REMOTE_MARKDOWN_IMAGE_PATH}?url=${encodeURIComponent(remoteURL)}`; |
| 40 | } |
| 41 |