Syntax highlighting not working in MD files for Jekyll?

会有一股神秘感。 提交于 2019-12-24 05:55:46

问题


The syntax highlighter does not seem to be working in Jekyll using the kramdown parser and rouge gem. Quotes also do not show up highlighted and formatted.

Markdown file:

```javascript
function order(words){
  var array = words.split(' ');
  var result = array.slice();

  for (word in array) {
    for (var i = 0; i < array[word].length; i++) {
       if (!isNaN(array[word][i])) {
         result[array[word][i] - 1] = array[word]
       }
     }
   }return result.join(' ');  
}
```

My config file listed below.

config.yml:

# Build settings
markdown: kramdown
highlighter: rouge
theme: minima
gems:
  - jekyll-feed

include: ['_pages']
exclude:
  - Gemfile
  - Gemfile.lock
  - vendor
#sass
sass:
  style: compressed

回答1:


Your Markdown file syntax is fine, you don't need to change it (check my comment bellow)

To get the code highlighting working, you need to check 2 things:

  1. the highlighter and the markdown processors are correctly configured in _config.yml
  2. the generated html files have access to a CSS highlighting-syntax style rules

1. Highlighter and the markdown processors configuration

As of jekyll 3.0, Kramdown as the Markdown engine, and Rouge as the syntax highlighter. are the default jekyll setting, and the only setting supported by github pages.

So you can remove their related setting, or set it explicitly at _config.yml as follows:

# Conversion
markdown:    kramdown
highlighter: rouge

# Markdown Processors
kramdown:
  input: GFM
  auto_ids: true
  syntax_highlighter: rouge

2. Code highlighting style:

the generated html file should have access to a CSS code-highlighting rules, that depends on the theme that you're working with.

One way to do so, is to have a code-highlighting style rules defined in the main css file, then include that file in the html head of the default layout.

define the CSS code-highliting rules

Make sure that the main CSS file, (located at /assets/css, and usually named main.scss or style.scss) has some code highlight CSS rules defined, either explicitly defined there, or by importing a file (scss, sass, or less) that contains the CSS rules.

for a quick check, I've putted some scss code-highlighting themes in this repo

  • clone sass-code-highlight repo
  • put sass-code-highlight folder inside the sass directory (by default: _sass)
  • inport the code-highlight to the main css file

in assets/css/main.scss add the following:

    @import "sass-code-highlight/monokai"; // 'monokai' as example

include the Main CSS in the HTML HEAD

you need to have the sinppet bellow in the default layout (_layouts/default.html)

<head>
  <!-- head stuff-->

  <!-- CSS -->
  <link rel="stylesheet" type="text/css" href="{{site.baseurl}}/assets/css/main.css"> <!-- IMPORTANT -->
</head>

either directly, or by including a head.htmlfile - located at _includes directory - into it, as follows:

<!DOCTYPE html>
<html lang="en">
  {% include head.html %} <!--  <- include the head -->
  <body>
  {{ content }}
  </body>
</html>

Note: make sure that the css path is valid.




回答2:


As you are using kramdown then you need to use ~~~ to make it highlight the code block samples:

In this case:

~~~ javascript
function order(words){
   var array = words.split(' ');
   var result = array.slice(); 
   for (word in array) { 
      for (var i = 0; i < array[word].length; i++) { 
         if (!isNaN(array[word][i])) {
             result[array[word][i] - 1] = array[word] } 
                } 
    }return result.join(' '); 
 }
~~~

Anoche approach using IALs:

~~~
//Code
~~~ 
{: .language-javascript}



回答3:


You can also wrap the code in {%highlight javascript%}... {%endhighlight%} blocks.




回答4:


Fenced code block works only with Kramdown::kramdown (use ~~~) and Kramdown::GFM (use ~~~ or backticks) parsers (see kramdown doc).

If you want to use them, you can set kramdown parser in _config.yml :

kramdown:
  input: GFM


来源:https://stackoverflow.com/questions/41773267/syntax-highlighting-not-working-in-md-files-for-jekyll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!