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: always use triple quotes to prevent escaping single quotes (#1857)
Myles Scolnick1f5fddeabc6e
diff --git a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts b/frontend/src/core/codemirror/language/__tests__/markdown.test.tsindex 319348a7ff..bd4b9d47aa 100644--- a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts+++ b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts@@ -13,8 +13,8 @@ describe("MarkdownLanguageAdapter", () => {const out = adapter.transformOut(innerCode);expect(out).toMatchInlineSnapshot(`[- "mo.md(r"")",- 8,+ "mo.md(r""" """)",+ 10,]`);});@@ -158,6 +158,50 @@ describe("MarkdownLanguageAdapter", () => {});describe("transformOut", () => {+ it("empty string", () => {+ const code = "";+ adapter.lastQuotePrefix = "";+ const [wrappedCode, offset] = adapter.transformOut(code);+ expect(wrappedCode).toBe(`mo.md(""" """)`);+ expect(offset).toBe(9);+ });++ it("single line", () => {+ const code = "Hello world";+ adapter.lastQuotePrefix = "";+ const [wrappedCode, offset] = adapter.transformOut(code);+ expect(wrappedCode).toBe(`mo.md("""Hello world""")`);+ expect(offset).toBe(9);+ });++ it("starts with quote", () => {+ const code = '"Hello" world';+ adapter.lastQuotePrefix = "";+ const [wrappedCode, offset] = adapter.transformOut(code);+ expect(wrappedCode).toMatchInlineSnapshot(`+ "mo.md(+ """+ "Hello" world+ """+ )"+ `);+ expect(offset).toBe(16);+ });++ it("ends with quote", () => {+ const code = 'Hello "world"';+ adapter.lastQuotePrefix = "";+ const [wrappedCode, offset] = adapter.transformOut(code);+ expect(wrappedCode).toMatchInlineSnapshot(`+ "mo.md(+ """+ Hello "world"+ """+ )"+ `);+ expect(offset).toBe(16);+ });+it("should wrap Markdown code with triple double-quoted string format", () => {const code = "# Markdown Title\n\nSome content here.";adapter.lastQuotePrefix = "";@@ -179,9 +223,9 @@ describe("MarkdownLanguageAdapter", () => {adapter.lastQuotePrefix = "";const [wrappedCode, offset] = adapter.transformOut(code);expect(wrappedCode).toBe(- `mo.md("Markdown with an escaped \\"\\"\\"quote\\"\\"\\"!!")`,+ `mo.md("""Markdown with an escaped \\"""quote\\"""!!""")`,);- expect(offset).toBe(7);+ expect(offset).toBe(9);});it.skip("should upgrade to an f-string if the code contains {}", () => {@@ -203,9 +247,9 @@ describe("MarkdownLanguageAdapter", () => {const code = String.raw`$\nu = \mathllap{}\cdot\mathllap{\alpha}$`;adapter.lastQuotePrefix = "r";const [wrappedCode, offset] = adapter.transformOut(code);- const pythonCode = `mo.md(r"$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$")`;+ const pythonCode = `mo.md(r"""$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$""")`;expect(wrappedCode).toBe(pythonCode);- expect(offset).toBe(8);+ expect(offset).toBe(10);});});diff --git a/frontend/src/core/codemirror/language/__tests__/utils.test.ts b/frontend/src/core/codemirror/language/__tests__/utils.test.tsindex 5d3f730f59..56234340b7 100644--- a/frontend/src/core/codemirror/language/__tests__/utils.test.ts+++ b/frontend/src/core/codemirror/language/__tests__/utils.test.ts@@ -128,8 +128,8 @@ describe("splitEditor", () => {selection: { anchor: "Hello,".length },});const result = splitEditor(mockEditor);- expect(result.beforeCursorCode).toEqual('mo.md("Hello,")');- expect(result.afterCursorCode).toEqual('mo.md(" World!")');+ expect(result.beforeCursorCode).toEqual('mo.md("""Hello,""")');+ expect(result.afterCursorCode).toEqual('mo.md(""" World!""")');});// f-strings not currently supported@@ -142,7 +142,7 @@ describe("splitEditor", () => {selection: { anchor: "{a}\n".length },});const result = splitEditor(mockEditor);- expect(result.beforeCursorCode).toEqual('mo.md(f"{a}")');- expect(result.afterCursorCode).toEqual('mo.md(f"{b}!")');+ expect(result.beforeCursorCode).toEqual('mo.md(f"""{a}""")');+ expect(result.afterCursorCode).toEqual('mo.md(f"""{b}!""")');});});diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.tsindex c5f11ef957..447647f57e 100644--- a/frontend/src/core/codemirror/language/markdown.ts+++ b/frontend/src/core/codemirror/language/markdown.ts@@ -86,20 +86,27 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {// Get the quote type from the last transformInconst prefix = this.lastQuotePrefix;+ // Empty string+ if (code === "") {+ // Need at least a space, otherwise the output will be 6 quotes+ code = " ";+ }++ // We always transform back with triple quotes, as to avoid needing to+ // escape single quotes.+ const escapedCode = code.replaceAll('"""', String.raw`\"""`);++ // If its one line and not bounded by quotes, write it as single lineconst isOneLine = !code.includes("\n");- if (isOneLine) {- // This logic breaks f-strings:- //- // https://github.com/marimo-team/marimo/issues/1727- const escapedCode = code.replaceAll('"', '\\"');- const start = `mo.md(${prefix}"`;- const end = `")`;+ const boundedByQuote = code.startsWith('"') || code.endsWith('"');+ if (isOneLine && !boundedByQuote) {+ const start = `mo.md(${prefix}"""`;+ const end = `""")`;return [start + escapedCode + end, start.length];}// Multiline codeconst start = `mo.md(\n ${prefix}"""\n`;- const escapedCode = code.replaceAll('"""', String.raw`\"""`);const end = `\n """\n)`;return [start + indentOneTab(escapedCode) + end, start.length + 1];}diff --git a/marimo/_smoke_tests/markdown_quotes.py b/marimo/_smoke_tests/markdown_quotes.pynew file mode 100644index 0000000000..e527961c65--- /dev/null+++ b/marimo/_smoke_tests/markdown_quotes.py@@ -0,0 +1,64 @@+import marimo++__generated_with = "0.7.9"+app = marimo.App(width="medium")+++def __(mo):+ mo.md("""Markdown""")+ return+++def __(mo):+ mo.md(r"""Markdown with an escaped \"""quote\"""!!""")+ return+++def __(mo):+ mo.md(+ """+ Markdown with a trailing "quote"+ """+ )+ return+++def __(mo):+ mo.md(+ """+ "Markdown" with a leading quote+ """+ )+ return+++def __(mo):+ mo.md("""Markdown with a trailing 'single quote'""")+ return+++def __(mo):+ mo.md("""'Markdown' with a leading single quote""")+ return+++def __(mo):+ mo.md("""Markdown with an triple-single '''quote'''!!""")+ return+++def __():+ import marimo as mo+ return mo,+++if __name__ == "__main__":+ app.run()