Skip to content

Navigation Menu

Sign up

docusaurus-plugin-onlinecompiler: runnable code blocks in 46 languages #13008

ashwin31 started this conversation in Plugins, Themes, Presets and Libs
Discussion options

This plugin turns fenced code blocks into editors that readers can run in the page. Add runnable after the language, and the block gets a Run button and a box for the program's input. It works for Python, C++, Java, Go, Rust, SQL and the other languages OnlineCompiler.io runs, 46 in all. @docusaurus/theme-live-codeblock covers JSX; this is for everything else.

npm install docusaurus-plugin-onlinecompiler
// docusaurus.config.js
export default {
 plugins: [['docusaurus-plugin-onlinecompiler', { key: process.env.ONLINECOMPILER_WIDGET_KEY }]],
};
```python runnable
for n in range(1, 4):
 print(n, "squared is", n * n)
```

The code runs on OnlineCompiler.io, a hosted code execution service (I build it), in a sandbox with no network access, not in the reader's browser or on your server. You need a widget key from its dashboard: keys are free, the free plan includes 100,000 runs a month, and you can restrict a key to your docs domain. The code stays in the page's HTML as text, so search engines index it and readers still see it if the service can't be reached. The widget script loads only on pages that have a runnable block.

Like theme-live-codeblock, it replaces the classic theme's code block component, so a site uses one or the other. It needs Docusaurus 3 with the classic theme and is tested with 3.10 on both Rspack and webpack. MIT licensed.

Repository: https://github.com/onlinecompiler-io/docusaurus-plugin-onlinecompiler
npm: https://www.npmjs.com/package/docusaurus-plugin-onlinecompiler
Guide with examples: https://onlinecompiler.io/guides/add-runnable-code-examples-to-docusaurus

Questions are welcome here or as an issue on the repository.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /