Skip to content

Commit be8d720

Browse files
authored
Enhance metadata handling in CodeBlockWithMetadata component and update example codeblock configuration
1 parent eb1c8cc commit be8d720

File tree

6 files changed

+1226
-2242
lines changed

6 files changed

+1226
-2242
lines changed

docs/libs/beman.optional/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ for (const auto& i : opt) {
5959
Full code can be found in [./examples/range_loop.cpp](https://github.com/bemanproject/optional/blob/main/examples/range_loop.cpp). Build and run instructions in
6060
[./examples/README.md](https://github.com/bemanproject/optional/blob/main/examples/README.md).
6161

62-
```cpp { "compiler": "clang_trunk", "libs": ["beman_optional@trunk"], "options": "-std=c++26" }
62+
```cpp { "compilers": ["clang_trunk -std=c++26", "gsnapshot -std=c++26"], "libs": ["beman_optional@trunk"], "filters": {"execute":true} }
6363
// examples/range_loop.cpp -*-C++-*-
6464
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6565

docusaurus.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ const config: Config = {
4343
onBrokenLinks: 'throw',
4444
onBrokenMarkdownLinks: 'warn',
4545

46-
// plugins: [require.resolve("./src/plugins/codeblock-metadata")],
47-
4846
// Even if you don't use internationalization, you can use this field to set
4947
// useful metadata like html lang. For example, if your site is Chinese, you
5048
// may want to replace "en" with "zh-Hans".

package-lock.json

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
"dependencies": {
1818
"@docusaurus/core": "3.7.0",
1919
"@docusaurus/preset-classic": "3.7.0",
20-
"@docusaurus/theme-live-codeblock": "^3.8.1",
2120
"@mdx-js/react": "^3.0.0",
2221
"@remark-embedder/core": "^3.0.3",
2322
"clsx": "^2.0.0",
2423
"prism-react-renderer": "^2.3.0",
2524
"react": "^19.0.0",
26-
"react-dom": "^19.0.0",
27-
"remark-code-blocks": "^2.0.1",
28-
"unist-util-visit": "^5.0.0"
25+
"react-dom": "^19.0.0"
2926
},
3027
"devDependencies": {
3128
"@docusaurus/module-type-aliases": "3.7.0",

src/components/CodeBlockWithMetadata.jsx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,59 @@
11
import React from "react";
22
import CodeBlock from "@theme/CodeBlock";
33

4+
let uniqueID = (function () {
5+
let id = 0;
6+
return function () { return id++; };
7+
})();
8+
49
export default function CodeBlockWithMetadata({ children, meta, language }) {
510
const sourceCode = children.trim();
611

712
let metadata = {};
813
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+
}
1018
} catch (err) {
1119
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+
);
1225
}
1326

1427
const lang = (language == "cpp" ? "c++" : language);
1528

16-
console.log("CodeBlock language:", lang);
17-
1829
const transformedLibs = (metadata.libs || []).map((lib) => {
1930
const [id, version] = lib.split("@");
2031
return { id, version };
2132
});
2233

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] || {},
2747
};
28-
const executor = { compiler: compilerConfig };
48+
2949
const session = {
30-
id: 1,
31-
lang,
50+
id: uniqueID(),
51+
language: lang,
3252
source: sourceCode,
33-
compilers: [],
34-
executors: [executor],
53+
compilers: transformedCompilers,
54+
executors: [],
3555
};
56+
3657
const clientState = { sessions: [session] };
3758

3859
const clientStateB64 = btoa(JSON.stringify(clientState)).replace(/\//g, "%2F");

0 commit comments

Comments
 (0)