A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix for pasting images in markdown (#7536)
Shahmir Varqhaa0e8170b0cfe
diff --git a/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts b/frontend/src/core/codemirror/markdown/__tests__/commands.test.tsindex eae95c3715..4c7e2b37ef 100644--- a/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts+++ b/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts@@ -25,6 +25,12 @@ import {insertUL,} from "../commands";+vi.mock("@/components/ui/use-toast", () => ({+ toast: vi.fn(),+}));++import { toast } from "@/components/ui/use-toast";+function createEditor(content: string) {const state = EditorState.create({doc: content,@@ -198,7 +204,7 @@ describe("insertImage", () => {await insertImage(view, mockPngFile());expect(view.state.doc.toString()).toMatchInlineSnapshot(- `"Hello, world!"`,+ `"Hello, world!"`,);});@@ -245,7 +251,7 @@ describe("insertImage", () => {});expect(view.state.doc.toString()).toMatchInlineSnapshot(- `"Hello, world!"`,+ `"Hello, world!"`,);});@@ -290,7 +296,93 @@ describe("insertImage", () => {});expect(view.state.doc.toString()).toMatchInlineSnapshot(- `"Hello, world!"`,+ `"Hello, world!"`,+ );+ });++ test("converts absolute path to relative path for image URL", async () => {+ view = createEditor("Hello, world!");+ view.dispatch({+ selection: { anchor: 7, head: 7 },+ });++ // mock filenameAtom with absolute path+ vi.spyOn(store, "get").mockImplementation((atom) => {+ if (atom === filenameAtom) {+ return "/Users/user/Development/project/notebook.py";+ }+ if (atom === requestClientAtom) {+ return mockRequestClient;+ }+ });++ // Server returns absolute path+ mockRequestClient.sendCreateFileOrFolder.mockResolvedValueOnce({+ success: true,+ message: null,+ info: {+ path: "/Users/user/Development/project/public/hello.png",+ name: "hello.png",+ children: [],+ id: "",+ isDirectory: false,+ isMarimoFile: false,+ lastModified: null,+ },+ });++ await insertImage(view, mockPngFile());++ expect(mockRequestClient.sendCreateFileOrFolder).toHaveBeenCalledTimes(1);+ expect(mockRequestClient.sendCreateFileOrFolder).toHaveBeenCalledWith({+ path: "/Users/user/Development/project/public",+ type: "file",+ name: "hello.png",+ contents: "AQID",+ });++ // Should convert absolute path to relative path+ expect(view.state.doc.toString()).toMatchInlineSnapshot(+ `"Hello, world!"`,+ );+ });++ test("converts absolute path to relative path in nested directory", async () => {+ view = createEditor("Hello, world!");+ view.dispatch({+ selection: { anchor: 7, head: 7 },+ });++ // mock filenameAtom with absolute path in nested directory+ vi.spyOn(store, "get").mockImplementation((atom) => {+ if (atom === filenameAtom) {+ return "/Users/user/Development/project/wip/notebook.py";+ }+ if (atom === requestClientAtom) {+ return mockRequestClient;+ }+ });++ // Server returns absolute path+ mockRequestClient.sendCreateFileOrFolder.mockResolvedValueOnce({+ success: true,+ message: null,+ info: {+ path: "/Users/user/Development/project/wip/public/image.png",+ name: "image.png",+ children: [],+ id: "",+ isDirectory: false,+ isMarimoFile: false,+ lastModified: null,+ },+ });++ await insertImage(view, mockPngFile());++ // Should convert absolute path to relative path+ expect(view.state.doc.toString()).toMatchInlineSnapshot(+ `"Hello, world!"`,);});@@ -321,7 +413,7 @@ describe("insertImage", () => {expect(mockRequestClient.sendCreateFileOrFolder).toHaveBeenCalledTimes(1);expect(view.state.doc.toString()).toMatchInlineSnapshot(- `"Hello, world!"`,+ `"Hello, world!"`,);});@@ -340,7 +432,52 @@ describe("insertImage", () => {expect(mockRequestClient.sendCreateFileOrFolder).toHaveBeenCalledTimes(1);expect(view.state.doc.toString()).toMatchInlineSnapshot(- `"Hello, world!"`,+ `"Hello, world!"`,+ );+ });++ test("rejects large files when user cancels prompt", async () => {+ view = createEditor("Hello, world!");+ view.dispatch({+ selection: { anchor: 7, head: 7 },+ });++ // User cancels the prompt+ vi.spyOn(window, "prompt").mockImplementation(() => null);++ // Create a large file (> 100KB when base64 encoded)+ // 100KB base64 = ~75KB binary, so we create 80KB to be safe+ const largeData = new Uint8Array(80 * 1024);+ const largeFile = new File([largeData], "large.png", { type: "image/png" });++ await insertImage(view, largeFile);++ // Should not insert anything+ expect(view.state.doc.toString()).toBe("Hello, world!");++ // Should show a toast about content being too large+ expect(toast).toHaveBeenCalledWith(+ expect.objectContaining({+ title: "Content too large",+ }),+ );+ });++ test("inserts small base64 when user cancels prompt", async () => {+ view = createEditor("Hello, world!");+ view.dispatch({+ selection: { anchor: 7, head: 7 },+ });++ // User cancels the prompt+ vi.spyOn(window, "prompt").mockImplementation(() => null);++ // Small file (3 bytes) - well under 100KB limit+ await insertImage(view, mockPngFile());++ // Should insert the base64 since it's small+ expect(view.state.doc.toString()).toMatchInlineSnapshot(+ `"Hello, world!"`,);});});diff --git a/frontend/src/core/codemirror/markdown/commands.ts b/frontend/src/core/codemirror/markdown/commands.tsindex 641a97a805..9c61f52f5e 100644--- a/frontend/src/core/codemirror/markdown/commands.ts+++ b/frontend/src/core/codemirror/markdown/commands.ts@@ -13,6 +13,8 @@ import { filenameAtom } from "@/core/saving/file-state";import { store } from "@/core/state/jotai";import { type FilePath, Paths } from "@/utils/paths";+const MAX_BASE64_SIZE_KB = 100;+function hasSelection(view: EditorView) {return !view.state.selection.main.empty;}@@ -341,6 +343,16 @@ export async function insertImage(view: EditorView, file: File) {if (createFileRes.success) {savedFilePath = createFileRes.info?.path;++ // We want the relative path to the public folder+ if (+ savedFilePath &&+ notebookDir &&+ savedFilePath.startsWith(notebookDir)+ ) {+ savedFilePath = Paths.rest(savedFilePath, notebookDir);+ }+toast({title: "Image uploaded successfully",description: `We've uploaded your image at ${savedFilePath}`,@@ -350,6 +362,16 @@ export async function insertImage(view: EditorView, file: File) {title: "Failed to upload image. Using raw base64 string.",});}+ } else {+ // Large strings do not make sense to write/save to the document+ const base64SizeKB = Math.round(dataUrl.length / 1024);+ if (base64SizeKB > MAX_BASE64_SIZE_KB) {+ toast({+ title: "Content too large",+ description: `The content size is ${base64SizeKB} KB, which is too large to paste directly into the document.`,+ });+ return;+ }}}} catch {@@ -359,7 +381,10 @@ export async function insertImage(view: EditorView, file: File) {}const changes = view.state.changeByRange((range) => {- const text = view.state.sliceDoc(range.from, range.to);+ let text = view.state.sliceDoc(range.from, range.to);+ if (text.trim() === "") {+ text = "alt";+ }return {changes: [{