UNPKG

slidev-addon-sm

Version:

slidev-addon slidev sm

80 lines (72 loc) 1.75 kB
import { defineCodeRunnersSetup, CodeRunnerContext, CodeRunnerOutput, } from "@slidev/types"; import sanitizeHtml from "sanitize-html"; import axios from "axios"; async function compileCpp() { try { const response = await axios.post( "https://godbolt.org/api/compiler/g121/compile", { source: ` #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; } `.trim(), options: { userArguments: "-O2 -std=c++17", compilerOptions: { executorRequest: true, }, filters: { execute: true, binary: false, intel: true, demangle: true, labels: true, libraryCode: false, directives: true, commentOnly: true, trim: false, }, tools: [], libraries: [], }, lang: "c++", allowStoreCodeDebug: true, } ); console.log("编译结果:", response.data); } catch (error) { console.error("编译失败:", error.response?.data || error.message); } } async function executeCppCodeRemotely( code: string, ctx: CodeRunnerContext ): Promise<CodeRunnerOutput> { compileCpp(); return { html: "<h1> test </d1>", }; } export default defineCodeRunnersSetup(() => { return { async cpp(code, ctx) { // 以某种方式执行代码并获取输出 const result = await executeCppCodeRemotely(code, ctx); return { text: result, }; }, html(code, ctx) { return { html: sanitizeHtml(code), }; }, }; });