Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make JavaScript code examples runnable? #1453

Open
rpearce opened this issue Jan 25, 2021 · 9 comments
Open

How to make JavaScript code examples runnable? #1453

rpearce opened this issue Jan 25, 2021 · 9 comments
Labels
A-Code-Blocks Area: Code blocks A-Playground Area: Playground

Comments

@rpearce
Copy link

rpearce commented Jan 25, 2021

Hi! Thanks for this great project.

Is there a way to run editable and runnable JS examples like the Rust ones? It seems those go to a remote location, which could be doable with a nodejs backend somewhere, but I'm not sure how to do that in mdBook.

Is there a configuration option somewhere for what backend to send requests to? Is it something to do with Ace? Do I need a different editor entirely?

My use case: I'm writing a small book on ramda.js and want to have runnable examples of that but without having to pull in runkit or some other such thing.

I'm sure other people would want backends for Go, Python, Ruby, etc., too!

Thanks for your time. 💜

@rpearce rpearce changed the title Make JavaScript code examples runnable? How to make JavaScript code examples runnable? Jan 26, 2021
@remexre
Copy link

remexre commented Feb 8, 2021

I imagine you'd do some tinkering with book.js in the theme, specifically in the vicinity of:

fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {

@rpearce
Copy link
Author

rpearce commented Feb 9, 2021

@remexre Thanks for the advice. I'll see if I can find a backend, how much I need to alter the code, and I will follow up here with results when I get them.

@KritR
Copy link

KritR commented Feb 26, 2021

I'm trying to get this to work with C. So far I've modified the book.js but I can't get the play buttons to show up yet when language isn't set to rust. Will update if there's any progress.

@KritR
Copy link

KritR commented Feb 26, 2021

Ok juts got this working here.
https://github.com/KritR/my-intro-cs/blob/master/theme/book.js
One annoying thing that's happening right now is that #'s are being stripped from the beginning of my includes.
But besides that. Here's what's changed.

That should be most of it.

If you wanted to tweak my version of book.js.
It would be easy enough to replace all instances of language-c with language-javascript.
And then replace the runC() method with something that ran the javascript code, and outputted the text to the result box.

@rpearce
Copy link
Author

rpearce commented Feb 26, 2021

I appreciate you. I haven't gotten to this point yet (still doing other building stuff), but I will pretty soon.

@ehuss ehuss added A-Code-Blocks Area: Code blocks A-Playground Area: Playground labels Jul 6, 2021
@md-rehman
Copy link

md-rehman commented Feb 20, 2023

Making JS example runnable is simple, you can check the demo here.

To do this simply add this inside the theme folder.

I have tried to add comments and make the code more readable.

There is one issue though, i.e., syntax highlighting is not working. If someone knows how to do it, please share.

@JSH32
Copy link

JSH32 commented Apr 4, 2023

Did anyone figure out syntax highlighting?

@dalance
Copy link
Contributor

dalance commented Jan 6, 2024

@md-rehman @JSH32

I've opened #2286.
It is a complete guide including syntax highlighting for custom playground.

@ronyhe
Copy link

ronyhe commented Feb 13, 2024

@md-rehman @JSH32

I've opened #2286. It is a complete guide including syntax highlighting for custom playground.

Thank you for this! Helps me immensely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Code-Blocks Area: Code blocks A-Playground Area: Playground
Projects
None yet
Development

No branches or pull requests

8 participants