fix: python format issue

This commit is contained in:
Timothy Jaeryang Baek 2025-07-29 17:08:47 +04:00
parent 526a98171e
commit 071a2ac835

View file

@ -127,9 +127,14 @@
let timeout; let timeout;
const worker = getPyodideWorker(); const worker = getPyodideWorker();
const startTag = `--||CODE-START-${id}||--`;
const endTag = `--||CODE-END-${id}||--`;
const script = ` const script = `
import black import black
print("${startTag}")
print(black.format_str("""${code.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/"/g, '\\"')}""", mode=black.Mode())) print(black.format_str("""${code.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/"/g, '\\"')}""", mode=black.Mode()))
print("${endTag}")
`; `;
const packages = ['black']; const packages = ['black'];
@ -144,7 +149,20 @@ print(black.format_str("""${code.replace(/\\/g, '\\\\').replace(/`/g, '\\`').rep
if (stderr) { if (stderr) {
reject(stderr); reject(stderr);
} else { } else {
const formatted = stdout && typeof stdout === 'string' ? stdout.trim() : ''; function extractBetweenDelimiters(stdout, start, end) {
console.log('stdout', stdout);
const startIdx = stdout.indexOf(start);
const endIdx = stdout.indexOf(end, startIdx + start.length);
if (startIdx === -1 || endIdx === -1) return null;
return stdout.slice(startIdx + start.length, endIdx).trim();
}
const formatted = extractBetweenDelimiters(
stdout && typeof stdout === 'string' ? stdout : '',
startTag,
endTag
);
resolve({ code: formatted }); resolve({ code: formatted });
} }
} }