Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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",
Expand Down
20 changes: 8 additions & 12 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 25 additions & 7 deletions frontend/src/core/codemirror/language/__tests__/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -119,28 +119,36 @@ 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", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it("doesn't do this")

Looks like a quick copypaste from a different test, will update in my pr

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", () => {
const pythonCode =
'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(
Expand All @@ -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\\"\\"\\"!!")`,
Expand Down Expand Up @@ -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", () => {
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/core/codemirror/language/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -53,15 +53,16 @@ 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
this.lastQuotePrefix = quotePrefix;
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];
}
}

Expand Down
Loading