3

I've been trying to implement Code Prettify in my Jekyll blog. I followed some guides and was able to implement it, but it's not ideal. This is what I did:

1- I went to _includes and added the necessary files in head.html:

The sunburst theme:

<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?autoload=true&amp;skin=sunburst&amp;lang=css" defer="defer"></script>

The CSS file of the theme (it is in my css folder on GitHub):

<link rel="stylesheet" href="/css/prettify.css" rel= 'stylesheet' type='text/css'>

2- For example, I used the tag <pre class= "prettyprint"></pre> to test it. As you can see in this post (the code block is at the bottom of the page), it does work. If you notice, you see that <iostream> is missing because I used <pre></pre> and therefore it sees the <> characters, takes them as HTML and doesn't show <iostream>. This is how it looks:

enter image description here

I could use HTML entities to fix it, but this is not ideal. I've searched for 2 hours and couldn't find a reliable source to fix this.


So my question is:

Is it possible to make Code Prettify work in markdown, using `` instead of <pre></pre>? If not, is there another way to implement it in Jekyll? Or make it easier to use? If none of these are possible, is there a better alternative to Jekyll which supports Code Prettify?

If you need to see other files please do check my repository.

Ludwig
  • 432
  • 6
  • 17

2 Answers2

2

With Jekyll, you can use two highligting methods :

backtick-style fenced-code

In _config.yml, set :

kramdown:
  input: GFM

Highlight your code like this :

``` c
include <iostream>
using namespace std;

int main () {
cout << "and then there were none" << endl;
return 0;
}
```

With highlight liquid tag

{% highlight c %}
include <iostream>
using namespace std;

int main () {
cout << "and then there were none" << endl;
return 0;
}
{% endhighlight %}

Styling

Styles are from _scss/_syntax-highlighting.scss.

David Jacquel
  • 51,670
  • 6
  • 121
  • 147
  • Not sure why I should highlight my c++ code block using Java. I did try this and it worked, but I ended up switching to [Hexo](https://hexo.io/) before I saw your post. – Ludwig Feb 28 '16 at 18:32
  • I think it should be `cpp`. :D – Ludwig Feb 28 '16 at 22:12
-1

The plugin I'm currently using for code highlighting is "jekyll-rouge", which supports ''' style markdown well:

https://sacha.me/articles/jekyll-rouge/

Teddy Ma
  • 1,126
  • 6
  • 12