From 3a693dcb44e61c3330826d70df3b669febfe6276 Mon Sep 17 00:00:00 2001 From: Alexander Mann <8006302+fuenfundachtzig@users.noreply.github.com> Date: Sat, 6 Apr 2024 17:39:01 +0200 Subject: [PATCH] switched to string-dedent --- frontend/package.json | 2 +- frontend/pnpm-lock.yaml | 15 ++++++++------- .../language/__tests__/markdown.test.ts | 10 +++++----- frontend/src/core/codemirror/language/markdown.ts | 7 ++++--- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index 9beec87cd6c..753a4efd04e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -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", diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml index 15de42db731..9913fcfa000 100644 --- a/frontend/pnpm-lock.yaml +++ b/frontend/pnpm-lock.yaml @@ -209,9 +209,6 @@ dependencies: date-fns: specifier: ^3.3.1 version: 3.3.1 - dedent-js: - specifier: ^1.0.1 - version: 1.0.1 dequal: specifier: ^2.0.3 version: 2.0.3 @@ -302,6 +299,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 @@ -9338,10 +9338,6 @@ packages: character-entities: 2.0.2 dev: false - /dedent-js@1.0.1: - resolution: {integrity: sha512-OUepMozQULMLUmhxS95Vudo0jb0UchLimi3+pQ2plj61Fcy8axbP9hbiD4Sz6DPqn6XG3kfmziVfQ1rSys5AJQ==} - dev: false - /deep-eql@4.1.3: resolution: {integrity: sha512-WaEtAOpRA1MQ0eohqZjpGD8zdI0Ovsm8mmFhaDN8dvDZzyoUMcYDnf5Y6iu7HTXxf8JDS23qWa4a+hKCDyOPzw==} engines: {node: '>=6'} @@ -15572,6 +15568,11 @@ packages: resolution: {integrity: sha512-Nk/brWYpD85WlOgzw5h173aci0Teyv8YdIAEtV+N88nDB0dLlazZyJMIsN6eo1/AR61l+p6CJTG1JIyFaoNEEA==} dev: false + /string-dedent@3.0.1: + resolution: {integrity: sha512-A2zCXSgpPrpFi1lDJlDwIPYakBWeDtQZ8ZBKssB8M/WbtNEKTzsl1yCDRmHx55jSB27xZDQ6NOtRYekESWx6fw==} + engines: {node: '>=0.12.0'} + dev: false + /string-split-by@1.0.0: 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 578a5521add..53b7bf5c0aa 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", () => { + 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", () => { @@ -134,7 +134,7 @@ 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); }); }); diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.ts index 39999c739f3..77eb368f621 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-js"; +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]; } }