Skip to content

Commit

Permalink
switched to string-dedent
Browse files Browse the repository at this point in the history
  • Loading branch information
fuenfundachtzig committed Apr 6, 2024
1 parent 84de4c0 commit 3a693dc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
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-js": "^1.0.1",
"string-dedent": "^3.0.1",
"dequal": "^2.0.3",
"eslint-plugin-header": "^3.1.1",
"htm": "^3.1.1",
Expand Down
15 changes: 8 additions & 7 deletions frontend/pnpm-lock.yaml

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

10 changes: 5 additions & 5 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,22 +119,22 @@ 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", () => {
it("should handle space around the f-strings", () => {
const pythonCode = 'mo.md(\n\t"""\n# Title\nContent\n"""\n)';
const [innerCode, offset] = adapter.transformIn(pythonCode);
expect(innerCode).toBe("# Title\nContent");
expect(offset).toBe(12);
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);
});
});

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-js";
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

0 comments on commit 3a693dc

Please sign in to comment.