mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

How to make JavaScript code examples runnable?

Open rpearce opened this issue 4 years ago • 9 comments

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 avatar Jan 25 '21 20:01 rpearce

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

https://github.com/rust-lang/mdBook/blob/4c951d530d7cf225fcca18d6e9146b28bf8dcf10/src/theme/book.js#L127

remexre avatar Feb 08 '21 15:02 remexre

@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.

rpearce avatar Feb 09 '21 15:02 rpearce

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 avatar Feb 26 '21 00:02 KritR

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.

KritR avatar Feb 26 '21 18:02 KritR

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

rpearce avatar Feb 26 '21 18:02 rpearce

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.

md-rehman avatar Feb 20 '23 03:02 md-rehman

Did anyone figure out syntax highlighting?

JSH32 avatar Apr 04 '23 15:04 JSH32

@md-rehman @JSH32

I've opened https://github.com/rust-lang/mdBook/pull/2286. It is a complete guide including syntax highlighting for custom playground.

dalance avatar Jan 06 '24 11:01 dalance

@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!

ronyhe avatar Feb 13 '24 12:02 ronyhe