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.

fix: fix detection of markdown language support (#2268)

Myles Scolnickaafc9bbfd7d2
diff --git a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
index bd4b9d47aa..01c0a683c0 100644
--- a/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/markdown.test.ts
@@ -277,5 +277,39 @@ describe("MarkdownLanguageAdapter", () => {
const pythonCode = [once, once].join("\n");
expect(adapter.isSupported(pythonCode)).toBe(false);
});
+
+ it("should return false for multiple markdown blocks in a single string", () => {
+ const pythonCode = `mo.md("this is markdown"), mo.md("this is not markdown")`;
+ expect(adapter.isSupported(pythonCode)).toBe(false);
+ });
+
+ it("should return false for multiple mo statements blocks in a single string", () => {
+ const CASES = [
+ `mo.md("this is markdown"), mo.plain_text("this is not markdown")`,
+ `mo.plain_text("this is not markdown"), mo.md("this is markdown")`,
+ `mo.md("this is markdown"), mo.md("this is markdown")`,
+ `mo.plain_text("this is not markdown"), mo.plain_text("this is not markdown")`,
+ ];
+ for (const pythonCode of CASES) {
+ expect(adapter.isSupported(pythonCode)).toBe(false);
+ }
+ });
+
+ it("should return true when the mo calls are just strings inside the markdown", () => {
+ const CASES = [
+ `mo.md("this is markdown, mo.plain_text('this is not markdown')")`,
+ `mo.md("this is markdown, mo.md('this is markdown')")`,
+ `mo.md("this is markdown, mo.md('this is markdown'), mo.plain_text('this is not markdown')")`,
+ `mo.md("""this is markdown, mo.plain_text('this is not markdown')""")`,
+ `mo.md("""this is markdown, mo.md('this is markdown')""")`,
+ `mo.md("""this is markdown, mo.md('this is markdown'), mo.plain_text('this is not markdown')""")`,
+ `mo.md(r"""this is markdown, mo.plain_text('this is not markdown')""")`,
+ `mo.md(r"""this is markdown, mo.md('this is markdown')""")`,
+ `mo.md(r"""this is markdown, mo.md('this is markdown'), mo.plain_text('this is not markdown')""")`,
+ ];
+ for (const pythonCode of CASES) {
+ expect(adapter.isSupported(pythonCode)).toBe(true);
+ }
+ });
});
});
diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.ts
index e9fae8e790..4b269b190d 100644
--- a/frontend/src/core/codemirror/language/markdown.ts
+++ b/frontend/src/core/codemirror/language/markdown.ts
@@ -125,16 +125,30 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {
return true;
}
- const markdownLines = pythonCode
- .trim()
- .split("\n")
- .map((line) => line.startsWith("mo.md("))
- .filter(Boolean);
- if (markdownLines.length > 1) {
- // more than line starting with mo.md(; as a heuristic,
- // don't show "view as markdown"
- return false;
+ // Handle mo.md("foo"), mo.plain_text("bar") in the same line
+ // If it starts with mo.md, but we have more than one function call, return false
+ if (pythonCode.trim().startsWith("mo.md(")) {
+ const tree = pythonLanguage.parser.parse(pythonCode);
+ let functionCallCount = 0;
+
+ // Parse the code using Lezer to check for multiple function calls
+ tree.iterate({
+ enter: (node) => {
+ if (node.name === "CallExpression") {
+ functionCallCount++;
+ if (functionCallCount > 1) {
+ return false; // Stop iterating if we've found more than one function call
+ }
+ }
+ },
+ });
+
+ // If the function call count is greater than 1, we don't want to show "view as markdown"
+ if (functionCallCount > 1) {
+ return false;
+ }
}
+
return regexes.some(([, regex]) => regex.test(pythonCode));
}