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.

no f-strings in md viewer (#1728)

Akshay Agrawal4eb20b06ab7f
diff --git a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
index c87fd66f61..44f3d2b3b8 100644
--- a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
@@ -55,12 +55,6 @@ describe("MarkdownLanguageAdapter", () => {
});
it("should unescape code blocks", () => {
- // f"""
- const pythonCode = String.raw`mo.md(f"""This is some \"""content\"""!""")`;
- const [innerCode, offset] = adapter.transformIn(pythonCode);
- expect(innerCode).toBe(`This is some """content"""!`);
- expect(offset).toBe(10);
-
// """
const pythonCode2 = String.raw`mo.md("""This is some \"""content\"""!""")`;
const [innerCode2, offset2] = adapter.transformIn(pythonCode2);
@@ -202,22 +196,6 @@ describe("MarkdownLanguageAdapter", () => {
expect(offset).toBe(9);
});
- it("should not downgrade a f-string", () => {
- const code = "Normal markdown";
- adapter.lastQuotePrefix = "f";
- const [wrappedCode, offset] = adapter.transformOut(code);
- expect(wrappedCode).toBe('mo.md(f"Normal markdown")');
- expect(offset).toBe(8);
- });
-
- it("should not downgrade a rf-string", () => {
- const code = "Normal markdown";
- adapter.lastQuotePrefix = "rf";
- const [wrappedCode, offset] = adapter.transformOut(code);
- expect(wrappedCode).toBe('mo.md(rf"Normal markdown")');
- expect(offset).toBe(9);
- });
-
it("should preserve r strings", () => {
const code = String.raw`$\nu = \mathllap{}\cdot\mathllap{\alpha}$`;
adapter.lastQuotePrefix = "r";
@@ -237,6 +215,11 @@ describe("MarkdownLanguageAdapter", () => {
expect(adapter.isSupported('mo.md("")')).toBe(true);
});
+ it("should return false for unsupported markdown string formats", () => {
+ expect(adapter.isSupported("mo.md(f'hello world')")).toBe(false);
+ expect(adapter.isSupported('mo.md(f"hello world")')).toBe(false);
+ });
+
it("should return false for unsupported string formats", () => {
const pythonCode = 'print("Hello, World!")';
expect(adapter.isSupported(pythonCode)).toBe(false);
diff --git a/frontend/src/core/codemirror/language/__tests__/utils.test.ts b/frontend/src/core/codemirror/language/__tests__/utils.test.ts
index e0cc947c51..5d3f730f59 100644
--- a/frontend/src/core/codemirror/language/__tests__/utils.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/utils.test.ts
@@ -132,7 +132,8 @@ describe("splitEditor", () => {
expect(result.afterCursorCode).toEqual('mo.md(" World!")');
});
- it("handles markdown with variables", () => {
+ // f-strings not currently supported
+ it.skip("handles markdown with variables", () => {
const mockEditor = createEditor('mo.md(f"""{a}\n{b}!""")');
// Set to markdown
switchLanguage(mockEditor, "markdown");
diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.ts
index 17bad5b0f1..60ff727bf2 100644
--- a/frontend/src/core/codemirror/language/markdown.ts
+++ b/frontend/src/core/codemirror/language/markdown.ts
@@ -16,11 +16,7 @@ import { enhancedMarkdownExtension } from "../markdown/extension";
import { CompletionConfig } from "@/core/config/config-schema";
import { HotkeyProvider } from "@/core/hotkeys/hotkeys";
import { indentOneTab } from "./utils/indentOneTab";
-import {
- QuotePrefixKind,
- QUOTE_PREFIX_KINDS,
- splitQuotePrefix,
-} from "./utils/quotes";
+import { QuotePrefixKind, splitQuotePrefix } from "./utils/quotes";
const quoteKinds = [
['"""', '"""'],
@@ -28,8 +24,14 @@ const quoteKinds = [
['"', '"'],
["'", "'"],
];
+
// explode into all combinations
-const pairs = QUOTE_PREFIX_KINDS.flatMap((prefix) =>
+//
+// A note on f-strings:
+//
+// f-strings are not yet supported due to bad interactions with
+// string escaping, LaTeX, and loss of Python syntax highlighting
+const pairs = ["", "r"].flatMap((prefix) =>
quoteKinds.map(([start, end]) => [prefix + start, end]),
);
@@ -89,7 +91,10 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {
const isOneLine = !code.includes("\n");
if (isOneLine) {
- const escapedCode = code.replaceAll('"', String.raw`\"`);
+ // This logic breaks f-strings:
+ //
+ // https://github.com/marimo-team/marimo/issues/1727
+ const escapedCode = code.replaceAll('"', '\\"');
const start = `mo.md(${prefix}"`;
const end = `")`;
return [start + escapedCode + end, start.length];