|
1 | 1 | import React from "react"; |
2 | 2 | import CodeBlock from "@theme/CodeBlock"; |
3 | 3 |
|
| 4 | +let uniqueID = (function () { |
| 5 | + let id = 0; |
| 6 | + return function () { return id++; }; |
| 7 | +})(); |
| 8 | + |
4 | 9 | export default function CodeBlockWithMetadata({ children, meta, language }) { |
5 | 10 | const sourceCode = children.trim(); |
6 | 11 |
|
7 | 12 | let metadata = {}; |
8 | 13 | try { |
9 | | - metadata = JSON.parse(meta || "{}"); |
| 14 | + metadata = JSON.parse(meta); |
| 15 | + if (!metadata.compilers) { |
| 16 | + throw new Error("Metadata JSON must include a 'compilers' field."); |
| 17 | + } |
10 | 18 | } catch (err) { |
11 | 19 | console.error("Invalid metadata JSON in code block:", err); |
| 20 | + return ( |
| 21 | + <div style={{ marginBottom: "1em" }}> |
| 22 | + <CodeBlock language={language}>{sourceCode}</CodeBlock> |
| 23 | + </div> |
| 24 | + ); |
12 | 25 | } |
13 | 26 |
|
14 | 27 | const lang = (language == "cpp" ? "c++" : language); |
15 | 28 |
|
16 | | - console.log("CodeBlock language:", lang); |
17 | | - |
18 | 29 | const transformedLibs = (metadata.libs || []).map((lib) => { |
19 | 30 | const [id, version] = lib.split("@"); |
20 | 31 | return { id, version }; |
21 | 32 | }); |
22 | 33 |
|
23 | | - const compilerConfig = { |
24 | | - id: metadata.compiler, |
25 | | - libs: transformedLibs, |
26 | | - options: metadata.options || "", |
| 34 | + const transformedCompilers = (metadata.compilers || []).map((compiler) => { |
| 35 | + const firstSpace = compiler.indexOf(" "); |
| 36 | + const id = firstSpace === -1 ? compiler : compiler.slice(0, firstSpace); |
| 37 | + const options = firstSpace === -1 ? "" : compiler.slice(firstSpace + 1); |
| 38 | + const libs = transformedLibs; |
| 39 | + const filters = metadata.filters || {}; |
| 40 | + return { id, options, libs, filters }; |
| 41 | + }); |
| 42 | + |
| 43 | + const executor = { |
| 44 | + compilerVisible: true, |
| 45 | + compilerOutputVisible: true, |
| 46 | + compiler: transformedCompilers[0] || {}, |
27 | 47 | }; |
28 | | - const executor = { compiler: compilerConfig }; |
| 48 | + |
29 | 49 | const session = { |
30 | | - id: 1, |
31 | | - lang, |
| 50 | + id: uniqueID(), |
| 51 | + language: lang, |
32 | 52 | source: sourceCode, |
33 | | - compilers: [], |
34 | | - executors: [executor], |
| 53 | + compilers: transformedCompilers, |
| 54 | + executors: [], |
35 | 55 | }; |
| 56 | + |
36 | 57 | const clientState = { sessions: [session] }; |
37 | 58 |
|
38 | 59 | const clientStateB64 = btoa(JSON.stringify(clientState)).replace(/\//g, "%2F"); |
|
0 commit comments