仓库源文站点原文


title: Display Language Title of Syntax Highlighting time: 2014.08.31 17:36:00 layout: post tags:


What You Will Learn In This Post

In the post <a href="{{ site.url }}/2014/02/10/syntax-highlighting-for-jekyll/">Syntax Highlighting for Jekyll</a>, I introduced how to enabld syntax highlighting feature with Jekyll Websited hosted on GitHub. In this post, I'm going to talk more about how to display the language in which the code is highlighted.

You should first follow the steps in <a href="{{ site.url }}/2014/02/10/syntax-highlighting-for-jekyll/">Syntax Highlighting for Jekyll</a> to setup the basic feature.

What do we want?

When we have a block area of highlighted code in our blogs, the readers may possibly get confused about what language it is in. It would be much convenient if it has a title showing the language as in the following example.

{% highlight java %} return "Hello!"; {% endhighlight %}

As explained in my <a href="{{ site.url }}/2014/02/10/syntax-highlighting-for-jekyll/">former post</a>, when we want to include some code in our blog, we may probably write something like:

{% highlight text %} {{ '{' }}{{ '%' }} highlight java {{ '%' }}} return "Hello!"; {{ '{' }}{{ '%' }} endhighlight {{ '%' }}} {% endhighlight %}

We don't want to write something like the following example since Jekyll is designed for us to concentrate on the content itself when writing blogs, while writing extra HTML in mardown is something we should avoid whenever possible.

{% highlight text %}

<div class="highlight-title">java</div> {{ '{' }}{{ '%' }} highlight java {{ '%' }}} return "Hello!"; {{ '{' }}{{ '%' }} endhighlight {{ '%' }}} {% endhighlight %}

How to make it?

Observe first!

If we have a look at the generated code by Jekyll in <a href="https://github.com/davidfstr/rdiscount" target="_blank">rdiscount markdown</a>, we should find some pattern that we can take advantage of. Jekyll build results differ slightly on my local computer and on GitHub.

Locally built:

{% highlight html %}

<div class="highlight"> <pre> <code class="java"> <span class="k">return</span> <span class="s">"Hello!"</span><span class="o">;</span> </code> </pre> </div> {% endhighlight %}

Built on GitHub:

{% highlight html %}

<div class="highlight"> <pre> <code class="language-java" data-lang="java"> <span class="k">return</span> <span class="s">"Hello!"</span><span class="o">;</span> </code> </pre> </div> {% endhighlight %}

In both cases, we can take advantage of the value of class attribute and create a title like code in java or just language-java if you want.

CSS3 :before and :after

With CSS3 <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::before" target="_blank">`:before`</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::after" target="_blank">`:after`</a> selector, we can pre-append or append some content in CSS!

For example, if you want to place your email address in your blog without being distinguished by crawlers, here's what you can do.

{% highlight html %} <span class="my-email">me</span> {% endhighlight %}

{% highlight css %} .my-email:after { content: "@zhangwenli.com"; } {% endhighlight %}

Then, in the browser, your visitors will see me@zhangwenli.com!

You can also change the style of :before and :after like color, padding and etc. But since this post focuses on displaying the highlighted language, I'm not going to talk too much about :before and :after. You should try them out by yourselves. That can be interesting. :smirk:

Besides content: "@zhangwenli.com", we can also use attributes in content. For instance, content: attr(class) equals to content: "my-email" in the last example. And you can also use customerized attributes like:

{% highlight html %} <span class="my-email" domain="zhangwenli.com">me</span> {% endhighlight %}

{% highlight css %} .my-email:after { content: "@" attr(domain); } {% endhighlight %}

Now, let's get down to the real job...

We can use the following code to generate a title like language-java on GitHub.

{% highlight css %} pre > code:before { content: attr(class); display: block; } {% endhighlight %}

But if you'd prefer something like code in java, you can make use data-lang attribute generated by Jekyll on GitHub. Just be clear about your generated HTML code before you use it.

{% highlight css %} pre > code:before { content: "code in " attr(data-lang); display: block; } {% endhighlight %}

You may need to change some CSS style to make it looks prettier, but hopefully, that shouldn't be too difficult for you. :yum:

Check my related <a href="https://github.com/Ovilia/blog/commit/dce98cf6ed5d089998f86feadebbad8ba2e825d9" target="_blank">commit on GitHub</a> to see the style changes.