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.

1.0 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import type { QuoteType } from "../types.js";

/**
 * Escape quotes in a string for safe embedding in Python code.
 *
 * @param code - The code to escape
 * @param quoteType - The quote type to escape
 * @returns The escaped code
 */
export function escapeQuotes(code: string, quoteType: QuoteType): string {
  if (quoteType === '"""') {
    return code.replaceAll('"""', String.raw`\"""`);
  }
  if (quoteType === "'''") {
    return code.replaceAll("'''", String.raw`\'\'\'`);
  }
  if (quoteType === '"') {
    return code.replaceAll('"', String.raw`\"`);
  }
  if (quoteType === "'") {
    return code.replaceAll("'", String.raw`\'`);
  }
  return code;
}

/**
 * Unescape quotes in a string extracted from Python code.
 *
 * @param code - The code with escaped quotes
 * @param quoteType - The quote type that was escaped
 * @returns The unescaped code
 */
export function unescapeQuotes(code: string, quoteType: QuoteType): string {
  return code.replaceAll(`\\${quoteType}`, quoteType);
}