Open History to see the last commit
1export const MARKDOWN_IMAGE_MAX_WIDTH = 480;
2export const MARKDOWN_IMAGE_MAX_HEIGHT = 480;
3
4export interface MarkdownImageDisplaySize {
5 readonly width: number;
6 readonly height: number;
7}
8
9/** Keeps small images intrinsic while fitting larger images inside the chat viewport. */
10export function resolveMarkdownImageDisplaySize(input: {
11 readonly sourceWidth: number;
12 readonly sourceHeight: number;
13 readonly availableWidth: number;
14}): MarkdownImageDisplaySize | null {
15 if (
16 !Number.isFinite(input.sourceWidth) ||
17 !Number.isFinite(input.sourceHeight) ||
18 !Number.isFinite(input.availableWidth) ||
19 input.sourceWidth <= 0 ||
20 input.sourceHeight <= 0 ||
21 input.availableWidth <= 0
22 ) {
23 return null;
24 }
25
26 const scale = Math.min(
27 1,
28 input.availableWidth / input.sourceWidth,
29 MARKDOWN_IMAGE_MAX_WIDTH / input.sourceWidth,
30 MARKDOWN_IMAGE_MAX_HEIGHT / input.sourceHeight,
31 );
32
33 return {
34 width: input.sourceWidth * scale,
35 height: input.sourceHeight * scale,
36 };
37}