Skip to content

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.

feat: copy as markdown in the dataframe table (#7507)

Myles Scolnick1f17c1e6290d
diff --git a/frontend/src/components/data-table/download-actions.tsx b/frontend/src/components/data-table/download-actions.tsx
index 8145d50698..501b99fb76 100644
--- a/frontend/src/components/data-table/download-actions.tsx
+++ b/frontend/src/components/data-table/download-actions.tsx
@@ -5,6 +5,7 @@ import {
BrickWallIcon,
ChevronDownIcon,
ClipboardListIcon,
+ FileTextIcon,
TableIcon,
} from "lucide-react";
import React from "react";
@@ -13,7 +14,11 @@ import { logNever } from "@/utils/assertNever";
import { copyToClipboard } from "@/utils/copy";
import { downloadByURL } from "@/utils/download";
import { prettyError } from "@/utils/errors";
-import { jsonParseWithSpecialChar, jsonToTSV } from "@/utils/json/json-parser";
+import {
+ jsonParseWithSpecialChar,
+ jsonToMarkdown,
+ jsonToTSV,
+} from "@/utils/json/json-parser";
import { Button } from "../ui/button";
import {
DropdownMenu,
@@ -70,6 +75,12 @@ const clipboardOptions = [
description: "Comma-separated values",
icon: TableIcon,
},
+ {
+ label: "Markdown",
+ format: "markdown",
+ description: "Preserves hyperlinks and formatting",
+ icon: FileTextIcon,
+ },
] as const;
export const DownloadAs: React.FC<DownloadActionProps> = (props) => {
@@ -115,6 +126,12 @@ export const DownloadAs: React.FC<DownloadActionProps> = (props) => {
text = csv;
break;
}
+ case "markdown": {
+ const downloadUrl = await getDownloadUrl("json");
+ const json = await fetchJson(downloadUrl);
+ text = jsonToMarkdown(json);
+ break;
+ }
default:
logNever(format);
return;
diff --git a/frontend/src/utils/__tests__/json-parser.test.ts b/frontend/src/utils/__tests__/json-parser.test.ts
index 5b98162230..e0777bc23e 100644
--- a/frontend/src/utils/__tests__/json-parser.test.ts
+++ b/frontend/src/utils/__tests__/json-parser.test.ts
@@ -1,6 +1,10 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { expect, it } from "vitest";
-import { jsonParseWithSpecialChar, jsonToTSV } from "../json/json-parser";
+import {
+ jsonParseWithSpecialChar,
+ jsonToMarkdown,
+ jsonToTSV,
+} from "../json/json-parser";
it("can jsonParseWithSpecialChar happy path", () => {
expect(jsonParseWithSpecialChar('"hello"')).toEqual("hello");
@@ -131,3 +135,194 @@ it("handles NaN values in TSV", () => {
expect(jsonToTSV([{ a: Number.NaN, b: 1 }], locale)).toEqual("a\tb\nNaN\t1");
});
+
+it("can convert json to markdown - basic table", () => {
+ expect(jsonToMarkdown([])).toMatchInlineSnapshot(`""`);
+
+ expect(jsonToMarkdown([{ a: 1, b: 2 }])).toMatchInlineSnapshot(`
+ "| a | b |
+ |---|---|
+ | 1 | 2 |"
+ `);
+
+ expect(
+ jsonToMarkdown([
+ { a: 1, b: 2 },
+ { a: 3, b: 4 },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| a | b |
+ |---|---|
+ | 1 | 2 |
+ | 3 | 4 |"
+ `);
+
+ expect(
+ jsonToMarkdown([
+ { name: "Alice", age: 30 },
+ { name: "Bob", age: 25 },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| name | age |
+ |---|---|
+ | Alice | 30 |
+ | Bob | 25 |"
+ `);
+});
+
+it("can convert json to markdown - with URLs", () => {
+ expect(
+ jsonToMarkdown([
+ { name: "Google", url: "https://google.com" },
+ { name: "GitHub", url: "https://github.com" },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| name | url |
+ |---|---|
+ | Google | [https://google.com](https://google.com) |
+ | GitHub | [https://github.com](https://github.com) |"
+ `);
+
+ // Mixed content - URL and text in same cell
+ expect(
+ jsonToMarkdown([{ info: "Visit https://example.com for more" }]),
+ ).toMatchInlineSnapshot(`
+ "| info |
+ |---|
+ | Visit [https://example.com](https://example.com) for more |"
+ `);
+
+ // Multiple URLs in one cell
+ expect(
+ jsonToMarkdown([{ links: "https://google.com and https://github.com" }]),
+ ).toMatchInlineSnapshot(`
+ "| links |
+ |---|
+ | [https://google.com](https://google.com) and [https://github.com](https://github.com) |"
+ `);
+});
+
+it("can convert json to markdown - handles nulls and undefined", () => {
+ expect(
+ jsonToMarkdown([{ a: null, b: undefined, c: 1 }]),
+ ).toMatchInlineSnapshot(`
+ "| a | b | c |
+ |---|---|---|
+ | | | 1 |"
+ `);
+
+ expect(
+ jsonToMarkdown([
+ { a: 1, b: 2 },
+ { a: null, b: 3 },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| a | b |
+ |---|---|
+ | 1 | 2 |
+ | | 3 |"
+ `);
+});
+
+it("can convert json to markdown - handles special characters", () => {
+ // Pipes need to be escaped since they're markdown table delimiters
+ expect(
+ jsonToMarkdown([{ a: "value|with|pipes", b: "normal" }]),
+ ).toMatchInlineSnapshot(`
+ "| a | b |
+ |---|---|
+ | value\\|with\\|pipes | normal |"
+ `);
+
+ // Newlines should be replaced with spaces
+ expect(
+ jsonToMarkdown([{ a: "line1\nline2", b: "normal" }]),
+ ).toMatchInlineSnapshot(`
+ "| a | b |
+ |---|---|
+ | line1 line2 | normal |"
+ `);
+
+ // Backslashes should be escaped
+ expect(jsonToMarkdown([{ path: "C:\\Users\\Name" }])).toMatchInlineSnapshot(`
+ "| path |
+ |---|
+ | C:\\\\Users\\\\Name |"
+ `);
+});
+
+it("can convert json to markdown - handles different data types", () => {
+ expect(
+ jsonToMarkdown([{ str: "text", num: 42, bool: true, nil: null }]),
+ ).toMatchInlineSnapshot(`
+ "| str | num | bool | nil |
+ |---|---|---|---|
+ | text | 42 | true | |"
+ `);
+
+ // Numbers (including NaN and Infinity)
+ expect(
+ jsonToMarkdown([
+ {
+ a: Number.NaN,
+ b: Number.POSITIVE_INFINITY,
+ c: Number.NEGATIVE_INFINITY,
+ },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| a | b | c |
+ |---|---|---|
+ | NaN | Infinity | -Infinity |"
+ `);
+
+ // Arrays and objects should be stringified
+ expect(jsonToMarkdown([{ data: [1, 2, 3] }])).toMatchInlineSnapshot(`
+ "| data |
+ |---|
+ | [1,2,3] |"
+ `);
+
+ expect(
+ jsonToMarkdown([{ data: { nested: "value" } }]),
+ ).toMatchInlineSnapshot(`
+ "| data |
+ |---|
+ | {"nested":"value"} |"
+ `);
+});
+
+it("can convert json to markdown - handles existing markdown links", () => {
+ // When input already contains a markdown link, it should be preserved as-is
+ expect(
+ jsonToMarkdown([{ link: "[Google](https://google.com)" }]),
+ ).toMatchInlineSnapshot(`
+ "| link |
+ |---|
+ | [Google](https://google.com) |"
+ `);
+
+ // Multiple existing markdown links
+ expect(
+ jsonToMarkdown([
+ { text: "[Google](https://google.com) and [GitHub](https://github.com)" },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| text |
+ |---|
+ | [Google](https://google.com) and [GitHub](https://github.com) |"
+ `);
+
+ // Mix of existing markdown links and plain URLs
+ expect(
+ jsonToMarkdown([
+ {
+ mixed:
+ "[Google](https://google.com) and plain https://example.com and [GitHub](https://github.com)",
+ },
+ ]),
+ ).toMatchInlineSnapshot(`
+ "| mixed |
+ |---|
+ | [Google](https://google.com) and plain [https://example.com](https://example.com) and [GitHub](https://github.com) |"
+ `);
+});
diff --git a/frontend/src/utils/json/json-parser.ts b/frontend/src/utils/json/json-parser.ts
index 87667e15c5..7224a214fb 100644
--- a/frontend/src/utils/json/json-parser.ts
+++ b/frontend/src/utils/json/json-parser.ts
@@ -96,6 +96,95 @@ export function jsonToTSV(json: Record<string, unknown>[], locale: string) {
return `${keys.join("\t")}\n${values.join("\n")}`;
}
+/**
+ * Converts JSON data to a Markdown table format
+ * Detects URLs and converts them to markdown links [url](url)
+ */
+export function jsonToMarkdown(json: Record<string, unknown>[]): string {
+ if (json.length === 0) {
+ return "";
+ }
+
+ const keys = Object.keys(json[0]);
+
+ const header = `| ${keys.join(" | ")} |`;
+ const separator = `|${keys.map(() => "---").join("|")}|`;
+
+ const rows = json.map((row) => {
+ const cells = keys.map((key) => formatValueForMarkdown(row[key]));
+ return `| ${cells.join(" | ")} |`;
+ });
+
+ return [header, separator, ...rows].join("\n");
+}
+
+/**
+ * Formats a value for markdown export
+ * - Detects URLs and converts them to markdown links
+ * - Skips URLs that are already in markdown link format
+ * - Escapes special markdown characters
+ * - Handles nulls, arrays, and objects
+ */
+function formatValueForMarkdown(value: unknown): string {
+ // Handle nulls and undefined
+ if (value === null || value === undefined) {
+ return "";
+ }
+
+ // Handle arrays and objects by stringifying
+ if (Array.isArray(value)) {
+ return escapeMarkdownTableChars(JSON.stringify(value));
+ }
+
+ if (typeof value === "object") {
+ return escapeMarkdownTableChars(JSON.stringify(value));
+ }
+
+ // Convert to string
+ let stringValue = String(value);
+
+ // Regex to match existing markdown links: [text](url)
+ const markdownLinkRegex = /\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g;
+
+ // Temporarily replace existing markdown links with placeholders
+ const placeholders = new Map<string, string>();
+ let placeholderIndex = 0;
+
+ stringValue = stringValue.replace(markdownLinkRegex, (match) => {
+ const placeholder = `__MDLINK_${placeholderIndex++}__`;
+ placeholders.set(placeholder, match);
+ return placeholder;
+ });
+
+ // Convert plain URLs to markdown links
+ const urlRegex = /(https?:\/\/\S+)/g;
+ stringValue = stringValue.replace(urlRegex, "[$1]($1)");
+
+ // Restore markdown links from placeholders
+ for (const [placeholder, original] of placeholders) {
+ stringValue = stringValue.replace(placeholder, original);
+ }
+
+ // Escape special markdown table characters
+ return escapeMarkdownTableChars(stringValue);
+}
+
+/**
+ * Escapes special characters in markdown tables
+ */
+function escapeMarkdownTableChars(value: string): string {
+ return (
+ value
+ // Escape backslashes first (must be first to avoid double-escaping)
+ .replaceAll("\\", "\\\\")
+ // Escape pipes (markdown table delimiter)
+ .replaceAll("|", "\\|")
+ // Replace newlines with spaces
+ .replaceAll("\n", " ")
+ .replaceAll("\r", "")
+ );
+}
+
/** Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json */
function sanitizeBigInt(value: unknown): unknown {
if (