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.

Address issue 1004 ("\n in mo.md literally interperted in markdown mode") (#1038)

fuenfundachtzig38f81e66a9a2
diff --git a/frontend/package.json b/frontend/package.json
index 0d179df26c..75787ed666 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -81,7 +81,7 @@
"compassql": "^0.21.2",
"cssnano": "^6.0.4",
"date-fns": "^3.3.1",
- "dedent": "^1.5.1",
+ "string-dedent": "^3.0.1",
"dequal": "^2.0.3",
"eslint-plugin-header": "^3.1.1",
"htm": "^3.1.1",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 1dbf985531..8adfd5ec24 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -212,9 +212,6 @@ dependencies:
date-fns:
specifier: ^3.3.1
version: 3.3.1
- dedent:
- specifier: ^1.5.1
- version: 1.5.1
dequal:
specifier: ^2.0.3
version: 2.0.3
@@ -305,6 +302,9 @@ dependencies:
rpc-anywhere:
specifier: ^1.7.0
version: 1.7.0
+ string-dedent:
+ specifier: ^3.0.1
+ version: 3.0.1
swiper:
specifier: ^11.0.7
version: 11.0.7
@@ -9394,15 +9394,6 @@ packages:
character-entities: 2.0.2
dev: false
- resolution: {integrity: sha512-+LxW+KLWxu3HW3M2w2ympwtqPrqYRzU8fqi6Fhd18fBALe15blJPI/I4+UHveMVG6lJqB4JNd4UG0S5cnVHwIg==}
- peerDependencies:
- babel-plugin-macros: ^3.1.0
- peerDependenciesMeta:
- babel-plugin-macros:
- optional: true
- dev: false
-
resolution: {integrity: sha512-WaEtAOpRA1MQ0eohqZjpGD8zdI0Ovsm8mmFhaDN8dvDZzyoUMcYDnf5Y6iu7HTXxf8JDS23qWa4a+hKCDyOPzw==}
engines: {node: '>=6'}
@@ -15638,6 +15629,11 @@ packages:
resolution: {integrity: sha512-Nk/brWYpD85WlOgzw5h173aci0Teyv8YdIAEtV+N88nDB0dLlazZyJMIsN6eo1/AR61l+p6CJTG1JIyFaoNEEA==}
dev: false
+ resolution: {integrity: sha512-A2zCXSgpPrpFi1lDJlDwIPYakBWeDtQZ8ZBKssB8M/WbtNEKTzsl1yCDRmHx55jSB27xZDQ6NOtRYekESWx6fw==}
+ engines: {node: '>=0.12.0'}
+ dev: false
+
resolution: {integrity: sha512-KaJKY+hfpzNyet/emP81PJA9hTVSfxNLS9SFTWxdCnnW1/zOOwiV248+EfoX7IQFcBaOp4G5YE6xTJMF+pLg6A==}
dependencies:
diff --git a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
index 578a5521ad..eb48b6e979 100644
--- a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
@@ -105,7 +105,7 @@ describe("MarkdownLanguageAdapter", () => {
expect(innerCode).toBe(
`# Hello, Markdown!\nmo.md(\n '''\n # Hello, Markdown!\n Use marimo's "md" function to embed rich text into your marimo\n '''\n)`,
);
- expect(offset).toBe(18);
+ expect(offset).toBe(9);
});
it("simple markdown", () => {
@@ -119,14 +119,14 @@ describe("MarkdownLanguageAdapter", () => {
const pythonCode = 'mo.md(""" \n# Title\nContent\n """)';
const [innerCode, offset] = adapter.transformIn(pythonCode);
expect(innerCode).toBe("# Title\nContent");
- expect(offset).toBe(13);
+ expect(offset).toBe(9);
});
- it("should handle space around the f=strings", () => {
- const pythonCode = 'mo.md(\n\t"""\n# Title\nContent\n"""\n)';
+ it("should handle space around the f-strings", () => {
+ const pythonCode = 'mo.md(\n\t"""\n# Title\n{ Content }\n"""\n)';
const [innerCode, offset] = adapter.transformIn(pythonCode);
- expect(innerCode).toBe("# Title\nContent");
- expect(offset).toBe(12);
+ expect(innerCode).toBe("# Title\n{ Content }");
+ expect(offset).toBe(11);
});
it("should dedent indented strings", () => {
@@ -134,13 +134,21 @@ describe("MarkdownLanguageAdapter", () => {
'mo.md(\n\t"""\n\t- item 1\n\t-item 2\n\t-item3\n\t"""\n)';
const [innerCode, offset] = adapter.transformIn(pythonCode);
expect(innerCode).toBe("- item 1\n-item 2\n-item3");
- expect(offset).toBe(13);
+ expect(offset).toBe(11);
+ });
+
+ it("should preserve escaped characters", () => {
+ const pythonCode = `mo.md(r"$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$")`;
+ const [innerCode, offset] = adapter.transformIn(pythonCode);
+ expect(innerCode).toBe("$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$");
+ expect(offset).toBe(8);
});
});
describe("transformOut", () => {
it("should wrap Markdown code with triple double-quoted string format", () => {
const code = "# Markdown Title\n\nSome content here.";
+ adapter.lastQuotePrefix = "";
const [wrappedCode, offset] = adapter.transformOut(code);
expect(wrappedCode).toMatchInlineSnapshot(`
"mo.md(
@@ -156,6 +164,7 @@ describe("MarkdownLanguageAdapter", () => {
it("should escape triple quotes in the Markdown code", () => {
const code = 'Markdown with an escaped """quote"""!!';
+ adapter.lastQuotePrefix = "";
const [wrappedCode, offset] = adapter.transformOut(code);
expect(wrappedCode).toBe(
`mo.md("Markdown with an escaped \\"\\"\\"quote\\"\\"\\"!!")`,
@@ -193,6 +202,15 @@ describe("MarkdownLanguageAdapter", () => {
expect(wrappedCode).toBe('mo.md(rf"Normal markdown")');
expect(offset).toBe(9);
});
+
+ it("should preserve r strings", () => {
+ const code = "$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$";
+ adapter.lastQuotePrefix = "r";
+ const [wrappedCode, offset] = adapter.transformOut(code);
+ const pythonCode = `mo.md(r"$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$")`;
+ expect(wrappedCode).toBe(pythonCode);
+ expect(offset).toBe(8);
+ });
});
describe("isSupported", () => {
diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.ts
index 8bd970d431..77eb368f62 100644
--- a/frontend/src/core/codemirror/language/markdown.ts
+++ b/frontend/src/core/codemirror/language/markdown.ts
@@ -5,7 +5,7 @@ import { markdown } from "@codemirror/lang-markdown";
import { languages } from "@codemirror/language-data";
import { parseMixed } from "@lezer/common";
import { python, pythonLanguage } from "@codemirror/lang-python";
-import dedent from "dedent";
+import dedent from "string-dedent";
import { logNever } from "@/utils/assertNever";
import {
Completion,
@@ -53,7 +53,7 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {
for (const [start, regex] of regexes) {
const match = pythonCode.match(regex);
if (match) {
- const innerCode = match[1].trim();
+ const innerCode = match[1];
const [quotePrefix, quoteType] = splitQuotePrefix(start);
// store the quote prefix for later when we transform out
@@ -61,7 +61,8 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {
const unescapedCode = innerCode.replaceAll(`\\${quoteType}`, quoteType);
const offset = pythonCode.indexOf(innerCode);
- return [dedent(unescapedCode), offset];
+ // string-dedent expects the first and last line to be empty / contain only whitespace, so we pad with \n
+ return [dedent(`\n${unescapedCode}\n`).trim(), offset];
}
}