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.

set markdown to hide after conversion for consistency (#8117)

Shahmir Varqha1bd0a591c699
diff --git a/frontend/e2e-tests/toggle-cell-language.spec.ts b/frontend/e2e-tests/toggle-cell-language.spec.ts
index a9c5282264..b683aaa6c4 100644
--- a/frontend/e2e-tests/toggle-cell-language.spec.ts
+++ b/frontend/e2e-tests/toggle-cell-language.spec.ts
@@ -28,24 +28,17 @@ test("change the cell to a markdown cell and toggle hide code", async ({
await page.getByText("Convert to Markdown").click();
await expect(title).toBeVisible();
- // Verify markdown content
- const markdown = page.getByText("import marimo as mo");
- await expect(markdown).toBeVisible();
-
- // Hide code
- await openCellActions(page, title);
- await page.getByText("Hide code").click();
- await expect(title).toBeVisible();
-
- // Verify code editor is hidden
+ // Expect cell editor to be invisible at first (markdown initial hide code is true)
const cellEditor = page.getByTestId("cell-editor");
await expect(cellEditor).toBeHidden();
// Unhide code
await openCellActions(page, title);
await page.getByText("Show code").click();
- await expect(title).toBeVisible();
-
- // Verify code editor is visible
await expect(cellEditor).toBeVisible();
+
+ // Hide code
+ await openCellActions(page, title);
+ await page.getByText("Hide code").click();
+ await expect(cellEditor).toBeHidden();
});
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index c0a91531a4..b3991d9a13 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -43,6 +43,7 @@ import type { CellData } from "@/core/cells/types";
import { formatEditorViews } from "@/core/codemirror/format";
import { toggleToLanguage } from "@/core/codemirror/language/commands";
import { switchLanguage } from "@/core/codemirror/language/extension";
+import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
import {
aiEnabledAtom,
appWidthAtom,
@@ -85,6 +86,7 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
sendToBottom,
addColumnBreakpoint,
clearCellOutput,
+ markUntouched,
} = useCellActions();
const splitCell = useSplitCellCallback();
const runCell = useRunCell(cell?.cellId);
@@ -209,7 +211,7 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
icon: <MarkdownIcon />,
label: "Convert to Markdown",
hotkey: "cell.viewAsMarkdown",
- handle: () => {
+ handle: async () => {
const editorView = getEditorView();
if (!editorView) {
return;
@@ -219,6 +221,17 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
language: "markdown",
keepCodeAsIs: false,
});
+ // Code stays visible until the user blurs the cell
+ if (!config.hide_code && MARKDOWN_INITIAL_HIDE_CODE) {
+ await saveCellConfig({
+ configs: { [cellId]: { hide_code: MARKDOWN_INITIAL_HIDE_CODE } },
+ });
+ updateCellConfig({
+ cellId,
+ config: { hide_code: MARKDOWN_INITIAL_HIDE_CODE },
+ });
+ markUntouched({ cellId });
+ }
},
hidden: isSetupCell,
},
diff --git a/frontend/src/components/editor/cell/CreateCellButton.tsx b/frontend/src/components/editor/cell/CreateCellButton.tsx
index d98d69aa53..281c8c6034 100644
--- a/frontend/src/components/editor/cell/CreateCellButton.tsx
+++ b/frontend/src/components/editor/cell/CreateCellButton.tsx
@@ -12,6 +12,7 @@ import {
import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";
import { useCellActions } from "@/core/cells/cells";
import { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";
+import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
import {
getConnectionTooltip,
isAppInteractionDisabled,
@@ -63,7 +64,7 @@ export const CreateCellButton = ({
maybeAddMarimoImport({ autoInstantiate: true, createNewCell });
onClick?.({
code: LanguageAdapters.markdown.defaultCode,
- hideCode: true,
+ hideCode: MARKDOWN_INITIAL_HIDE_CODE,
});
};
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 3511937db7..6f1a2187a8 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -19,6 +19,7 @@ import {
reconfigureLanguageEffect,
switchLanguage,
} from "@/core/codemirror/language/extension";
+import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
import type { LanguageAdapterType } from "@/core/codemirror/language/types";
import {
connectedDocAtom,
@@ -149,6 +150,17 @@ const CellEditorInternal = ({
autoInstantiate,
createNewCell: cellActions.createNewCell,
});
+ // Code stays visible until the user blurs the cell
+ if (!cellConfig.hide_code && MARKDOWN_INITIAL_HIDE_CODE) {
+ void saveCellConfig({
+ configs: { [cellId]: { hide_code: MARKDOWN_INITIAL_HIDE_CODE } },
+ });
+ cellActions.updateCellConfig({
+ cellId,
+ config: { hide_code: MARKDOWN_INITIAL_HIDE_CODE },
+ });
+ cellActions.markUntouched({ cellId });
+ }
});
const aiEnabled = isAiEnabled(userConfig);
diff --git a/frontend/src/components/editor/renderers/cell-array.tsx b/frontend/src/components/editor/renderers/cell-array.tsx
index 3bb0d055b4..29d3aee9a5 100644
--- a/frontend/src/components/editor/renderers/cell-array.tsx
+++ b/frontend/src/components/editor/renderers/cell-array.tsx
@@ -23,6 +23,7 @@ import { Tooltip } from "@/components/ui/tooltip";
import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";
import { SETUP_CELL_ID } from "@/core/cells/ids";
import { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";
+import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
import { aiEnabledAtom } from "@/core/config/config";
import { canInteractWithAppAtom } from "@/core/network/connection";
import { useBoolean } from "@/hooks/useBoolean";
@@ -295,7 +296,7 @@ const AddCellButtons: React.FC<{
cellId: { type: "__end__", columnId },
before: false,
code: LanguageAdapters.markdown.defaultCode,
- hideCode: true,
+ hideCode: MARKDOWN_INITIAL_HIDE_CODE,
});
}}
>
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.ts
index da973cf5d7..c6e3d7fea3 100644
--- a/frontend/src/core/cells/__tests__/cells.test.ts
+++ b/frontend/src/core/cells/__tests__/cells.test.ts
@@ -2561,6 +2561,126 @@ describe("cell reducer", () => {
expect(state.untouchedNewCells.has(newCellId)).toBe(false);
expect(exportedForTesting.isCellCodeHidden(state, newCellId)).toBe(true);
});
+
+ it("can mark an existing cell as untouched", () => {
+ // Create a cell without hideCode (not in untouchedNewCells)
+ actions.createNewCell({
+ cellId: "__end__",
+ before: false,
+ hideCode: false,
+ });
+
+ const newCellId =
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
+
+ // Mark it as untouched
+ actions.markUntouched({ cellId: newCellId });
+
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
+ });
+
+ it("markUntouched is idempotent", () => {
+ // Create a cell without hideCode
+ actions.createNewCell({
+ cellId: "__end__",
+ before: false,
+ hideCode: false,
+ });
+
+ const newCellId =
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
+
+ // Mark as untouched multiple times
+ actions.markUntouched({ cellId: newCellId });
+ actions.markUntouched({ cellId: newCellId });
+ actions.markUntouched({ cellId: newCellId });
+
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
+ });
+
+ it("markUntouched does not affect already untouched cells", () => {
+ // Create a cell with hideCode (already in untouchedNewCells)
+ actions.createNewCell({
+ cellId: "__end__",
+ before: false,
+ hideCode: true,
+ });
+
+ const newCellId =
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
+
+ // Calling markUntouched should not change anything
+ actions.markUntouched({ cellId: newCellId });
+
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
+ });
+
+ it("markTouched and markUntouched can toggle cell state", () => {
+ // Create a cell without hideCode
+ actions.createNewCell({
+ cellId: "__end__",
+ before: false,
+ hideCode: false,
+ });
+
+ const newCellId =
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
+
+ // Initially not untouched
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
+
+ // Mark as untouched
+ actions.markUntouched({ cellId: newCellId });
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
+
+ // Mark as touched
+ actions.markTouched({ cellId: newCellId });
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
+
+ // Mark as untouched again
+ actions.markUntouched({ cellId: newCellId });
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
+ });
+
+ it("markUntouched works for markdown cell conversion scenario", () => {
+ // Simulates converting a Python cell to Markdown
+ // 1. Create a regular cell (no hideCode)
+ actions.createNewCell({
+ cellId: "__end__",
+ before: false,
+ hideCode: false,
+ });
+
+ const cellId =
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
+
+ // Cell starts without hide_code and not in untouchedNewCells
+ expect(state.cellData[cellId].config.hide_code).toBe(false);
+ expect(state.untouchedNewCells.has(cellId)).toBe(false);
+ expect(exportedForTesting.isCellCodeHidden(state, cellId)).toBe(false);
+
+ // 2. Convert to markdown: set hide_code and mark as untouched
+ actions.updateCellConfig({
+ cellId,
+ config: { hide_code: true },
+ });
+ actions.markUntouched({ cellId });
+
+ // Code should NOT be hidden because cell is untouched (user can edit)
+ expect(state.cellData[cellId].config.hide_code).toBe(true);
+ expect(state.untouchedNewCells.has(cellId)).toBe(true);
+ expect(exportedForTesting.isCellCodeHidden(state, cellId)).toBe(false);
+
+ // 3. User blurs the cell (markTouched)
+ actions.markTouched({ cellId });
+
+ // Now code should be hidden
+ expect(state.untouchedNewCells.has(cellId)).toBe(false);
+ expect(exportedForTesting.isCellCodeHidden(state, cellId)).toBe(true);
+ });
});
describe("releaseCellAtoms", () => {
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 96b0763540..7fa26e869c 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -1033,6 +1033,20 @@ const {
return state;
},
+ markUntouched: (state, action: { cellId: CellId }) => {
+ const { cellId } = action;
+
+ if (!state.untouchedNewCells.has(cellId)) {
+ const nextUntouchedNewCells = new Set(state.untouchedNewCells);
+ nextUntouchedNewCells.add(cellId);
+ return {
+ ...state,
+ untouchedNewCells: nextUntouchedNewCells,
+ };
+ }
+
+ return state;
+ },
scrollToTarget: (state) => {
// Scroll to the specified cell and clear the scroll key.
const scrollKey = state.scrollKey;
diff --git a/frontend/src/core/codemirror/language/languages/markdown.ts b/frontend/src/core/codemirror/language/languages/markdown.ts
index 03614aa949..da3b4660d6 100644
--- a/frontend/src/core/codemirror/language/languages/markdown.ts
+++ b/frontend/src/core/codemirror/language/languages/markdown.ts
@@ -28,6 +28,13 @@ import type { LanguageAdapter } from "../types";
export type MarkdownLanguageAdapterMetadata = MarkdownMetadata;
+/**
+ * Default hide_code setting for markdown cells.
+ * When true, the markdown code is hidden after the cell is blurred,
+ * showing only the rendered output.
+ */
+export const MARKDOWN_INITIAL_HIDE_CODE = true;
+
/**
* Language adapter for Markdown.
*/