Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanzdosilovic committed Aug 10, 2023
0 parents commit 455e864
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Code Execution
Code Execution plugin for [TypingMind](https://typingmind.com) powered by [Judge0](https://judge0.com).

## Get Started
1. Get your code execution API key [here](https://judge0.com/ce).
2. Click the *Settings* tab and enter your RapidAPI Key.

## Supported Languages
C, C++, Go, Python, Java, JavaScript, Ruby.

## Example Usage
```
Run the following Python code:
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10))
```

---

```
Run this C++ code:
#include <iostream>
int main() {
std::cout << "Hello from TypingMind!\n";
return 0;
}
```

## Contribute
Your contributions are welcome via [GitHub](https://github.com/judge0/typingmind).
66 changes: 66 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const LANGUAGE_IDS = { // https://ce.judge0.com/languages
"c": 50,
"cpp": 54,
"go": 95,
"java": 91,
"javascript": 93,
"python": 92,
"ruby": 72
};

const LANGUAGE_ALIASES = {
"c++": "cpp",
"golang": "go",
"js": "javascript"
}

function getLanguageId(language) {
let l = language.toLowerCase();
return LANGUAGE_IDS[LANGUAGE_ALIASES[l] || l] || 0;
}

function encode(str) {
return btoa(unescape(encodeURIComponent(str || "")));
}

function decode(bytes) {
var escaped = escape(atob(bytes || ""));
try {
return decodeURIComponent(escaped);
} catch {
return unescape(escaped);
}
}

async function code_execution(params, userSettings) {
const { source_code, language } = params;
const { rapidApiKey } = userSettings;

const languageId = getLanguageId(language);
if (languageId == 0) {
return `Unsupported language ${language}`;
}

const requestHeaders = new Headers();
requestHeaders.append("x-rapidapi-key", rapidApiKey);
requestHeaders.append("Content-Type", "application/json");

const requestData = {
"language_id": languageId,
"source_code": encode(source_code),
"redirect_stderr_to_stdout": true
};

let response = await fetch("https://judge0-ce.p.rapidapi.com/submissions?base64_encoded=true&wait=true", {
method: "POST",
headers: requestHeaders,
body: JSON.stringify(requestData)
});

if (!response.ok) {
return "Network error";
}

let responseData = await response.json();
return [decode(responseData["compile_output"]), decode(responseData["stdout"])].join("\n").trim();
}
7 changes: 7 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name": "rapidApiKey",
"label": "RapidAPI Key",
"type": "password"
}
]
21 changes: 21 additions & 0 deletions spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "code_execution",
"description": "Run code in various programming languages",
"parameters": {
"type": "object",
"properties": {
"source_code": {
"type": "string",
"description": "A source code snippet"
},
"language": {
"type": "string",
"description": "A name of the programming language"
}
},
"required": [
"source_code",
"language"
]
}
}

0 comments on commit 455e864

Please sign in to comment.