我如何使用pygments使markdown显示行号?

时间:2014-03-18 15:40:00

标签: markdown highlight jekyll pygments

我知道液体高亮显示行号。 例如:

{% highlight java linenos %}
    public class Test {

    }
{% endhightlight $}

但我不知道如何让降价突出显示行号?

```java
    public class Test {

    }
```

像这样,它不会显示行号。是不是应该添加一个options参数?

1 个答案:

答案 0 :(得分:0)

添加行号和格式的简单方法是使用css / javascript。查看Prettify

Markdown生成器只会在您的内容周围创建html标记<div class="highlight"><pre><code></code></pre></div>

在页面的head部分,添加对美化脚本的引用,传递您需要的参数(或在您的站点中本地保存脚本)

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?lang=css&skin=sunburst"></script>

然后使用一些jQuery将“prettyprint”类添加到pre标签。这应该是你需要的全部工作

<script>
$( document ).ready(function() {
    $(this).find( ".highlight" ).each(function(){ 
        $(this).find("pre").addClass("prettyprint"); 
    })
});

</script>

选择您喜欢的theme,或使用默认值并添加“linenums”类以获取您的行号

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?lang=css&skin=sunburst"></script>
<script src="/javascripts/jquery-1.8.3.min.js"></script>
<script>
$(this).find("pre").not('.prettyprint').each(function(){
   $(this).addClass("prettyprint linenums");
   prettyPrint();
})
</script>

您的问题是关于使用pygments。如果这是你想要使用的,那么保留液体模板代码让pygments标记你的代码块并使用pygments样式表。我个人不喜欢在帖子中添加模板。有可能配置Jekyll使用markdown生成器生成更多代码友好的html,但我没有看到它。