├── .github └── 1689867191729131520.jpeg ├── README.md ├── plugin.js ├── settings.json └── spec.json /.github/1689867191729131520.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/judge0/typingmind/d1c07d7e0c536c35c52670008001426ccad69d13/.github/1689867191729131520.jpeg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code Execution 2 | Code Execution plugin for [TypingMind](https://typingmind.com) powered by [Judge0](https://judge0.com). 3 | 4 | [![https://t.co/gPO0ubB7kh](.github/1689867191729131520.jpeg)](https://t.co/gPO0ubB7kh) 5 | 6 | ## Get Started 7 | 1. Get your code execution API key [here](https://judge0.com/ce). 8 | 2. Click the *Settings* tab and enter your RapidAPI Key. 9 | 10 | ## Supported Languages 11 | C, C++, Go, Python, Java, JavaScript, Ruby. 12 | 13 | ## Example Usage 14 | ``` 15 | Run the following Python code: 16 | def fibonacci(n): 17 | if n <= 1: 18 | return n 19 | else: 20 | return fibonacci(n-1) + fibonacci(n-2) 21 | print(fibonacci(10)) 22 | ``` 23 | 24 | --- 25 | 26 | ``` 27 | Run this C++ code: 28 | #include 29 | 30 | int main() { 31 | std::cout << "Hello from TypingMind!\n"; 32 | return 0; 33 | } 34 | ``` 35 | 36 | ## Contribute 37 | Your contributions are welcome via [GitHub](https://github.com/judge0/typingmind). 38 | -------------------------------------------------------------------------------- /plugin.js: -------------------------------------------------------------------------------- 1 | const LANGUAGE_IDS = { // https://ce.judge0.com/languages 2 | "c": 50, 3 | "cpp": 54, 4 | "go": 95, 5 | "java": 91, 6 | "javascript": 93, 7 | "python": 92, 8 | "ruby": 72 9 | }; 10 | 11 | const LANGUAGE_ALIASES = { 12 | "c++": "cpp", 13 | "golang": "go", 14 | "js": "javascript" 15 | } 16 | 17 | function getLanguageId(language) { 18 | let l = language.toLowerCase(); 19 | return LANGUAGE_IDS[LANGUAGE_ALIASES[l] || l] || 0; 20 | } 21 | 22 | function encode(str) { 23 | return btoa(unescape(encodeURIComponent(str || ""))); 24 | } 25 | 26 | function decode(bytes) { 27 | var escaped = escape(atob(bytes || "")); 28 | try { 29 | return decodeURIComponent(escaped); 30 | } catch { 31 | return unescape(escaped); 32 | } 33 | } 34 | 35 | async function code_execution(params, userSettings) { 36 | const { source_code, language } = params; 37 | const { rapidApiKey } = userSettings; 38 | 39 | const languageId = getLanguageId(language); 40 | if (languageId == 0) { 41 | return `Unsupported language ${language}`; 42 | } 43 | 44 | const requestHeaders = new Headers(); 45 | requestHeaders.append("x-rapidapi-key", rapidApiKey); 46 | requestHeaders.append("Content-Type", "application/json"); 47 | 48 | const requestData = { 49 | "language_id": languageId, 50 | "source_code": encode(source_code), 51 | "redirect_stderr_to_stdout": true 52 | }; 53 | 54 | let response = await fetch("https://judge0-ce.p.rapidapi.com/submissions?base64_encoded=true&wait=true", { 55 | method: "POST", 56 | headers: requestHeaders, 57 | body: JSON.stringify(requestData) 58 | }); 59 | 60 | if (!response.ok) { 61 | return "Network error"; 62 | } 63 | 64 | let responseData = await response.json(); 65 | return [decode(responseData["compile_output"]), decode(responseData["stdout"])].join("\n").trim(); 66 | } 67 | -------------------------------------------------------------------------------- /settings.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "rapidApiKey", 4 | "label": "RapidAPI Key", 5 | "type": "password" 6 | } 7 | ] 8 | -------------------------------------------------------------------------------- /spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "code_execution", 3 | "description": "Run code in various programming languages", 4 | "parameters": { 5 | "type": "object", 6 | "properties": { 7 | "source_code": { 8 | "type": "string", 9 | "description": "A source code snippet" 10 | }, 11 | "language": { 12 | "type": "string", 13 | "enum": ["c", "cpp", "go", "java", "javascript", "python", "ruby"], 14 | "description": "A name of the programming language" 15 | } 16 | }, 17 | "required": [ 18 | "source_code", 19 | "language" 20 | ] 21 | } 22 | } 23 | --------------------------------------------------------------------------------