Jose Serodio
Jose Serodio

Reputation: 1449

How to use Syntax Highlighting GitBook

I'm new to GitBook and I am trying to make the syntax highlight work as expected for some JavaScript lines of code.


some javascript lines without colour


I noticed that there is a plugin store in the editor. I enabled a plugin called highlight but it's not doing it's job.

I searched in the documentation for this, but I couldn't find anything related.

This book has the syntax highlighting enabled and working:
https://gitbookio.gitbooks.io/javascript/content/basics/comments.html

book.json

{
    "plugins": [
        "highlight"
    ],
    "pluginsConfig": {}
}

How can I do the same?

Upvotes: 0

Views: 2805

Answers (2)

swen
swen

Reputation: 341

If you are writing GitBook pages in Markdown, you can highlight your code like below:

```javascript
var foo = function(num) {
    return num + num;
}
```

Where 'javascript' can be replaced with other languages, such as 'python', 'java', 'c' etc.

Upvotes: 3

Jose Serodio
Jose Serodio

Reputation: 1449

I did my research and I found out how to do this.

I tried another plugin called Ace. This plugin worked, here is my book.json

{
    "plugins": ["ace"],
    "pluginsConfig": {
    }
}

And I needed to nest the block of code with a basic syntax for JavaScript.

Input.

## The Arrow function
Now this should be syntax highlighted
In ES5

{%ace edit=false, lang='javascript', check=false, theme="tomorrow" %}
var myFunction = function(num) {  
    return num + num;
};
{%endace%}

In ES6

{%ace edit=false, lang='javascript', check=false, theme="tomorrow" %}
let myFunction = (num) => num + num;
{%endace%}

Output.

code highlighted

This is working just fine!

Upvotes: 2

Related Questions