Jekyll 中的语法高亮 Markdown 代码块(不使用 Liquid 标签)

发布于 2024-12-23 02:36:18 字数 323 浏览 7 评论 0原文

Jekyll 中的语法高亮似乎仅限于使用 Liquid 标签和 pygments,如下所示

{% highlight bash %}
cd ~
{% endhighlight %}

:浏览每篇文章并修复代码块。另外,我想将我的帖子保留为纯降价格式,以防我需要再次切换博客平台。

我将 Jekyll 解析器切换到了 redcarpet,希望能够使用这个 markdown 语法:

```bash
cd ~
```

但它似乎不起作用。它只是将其包装在一个普通的代码块中。有什么想法吗?

It seems like syntax highlighting in Jekyll is limited to using liquid tags and pygments like so:

{% highlight bash %}
cd ~
{% endhighlight %}

But I've imported my existing blog from wordpress and it was written in markdown (using markdown code blocks) and I don't want to have to go through each post and fix the code blocks. Also, I want to keep my posts in pure markdown format in case I ever need to switch blogging platforms again.

I switched my Jekyll parser to redcarpet with the hope that I could use this markdown syntax:

```bash
cd ~
```

But it doesn't seem to work. It just wraps it in a normal code block. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

迷鸟归林 2024-12-30 02:36:18

围栏块
已介绍
与 Redcarpet 2. Jekyll 一起
现在支持
Redcarpet 2.

顺便说一句,我正在使用 Redcarpet
胭脂
直到
Kramdown 支持
可用。

另外有些人更喜欢
Nanoc
杰基尔。

Fenced blocks
were introduced
with Redcarpet 2. Jekyll
now supports
Redcarpet 2.

As an aside I am using Redcarpet with
Rouge
until
Kramdown support
is available.

In addition some people prefer
Nanoc
to Jekyll.

梦里的微风 2024-12-30 02:36:18

替代解决方案

Markdown 允许 HTML,所以如果您不介意添加一点 JS,您可以这样做:

## A section

Here is some Ruby code.

<pre>
  <code class="ruby">
    puts "hello"
  </code>
</pre>

然后您可以使用 Highlight.js(文档此处)基于此添加突出显示班级。

这不是一个理想的解决方案,但它应该适用于任何 Markdown 解析器。

Alternate solution

Markdown allows HTML, so if you don't mind adding a bit of JS, you could do this:

## A section

Here is some Ruby code.

<pre>
  <code class="ruby">
    puts "hello"
  </code>
</pre>

Then you could use Highlight.js (documentation here) to add highlighting based on that class.

It's not an ideal solution, but it should work with any Markdown parser.

沉睡月亮 2024-12-30 02:36:18

我最终切换到 kramdown 来解析 附带的 markdown coderay 用于语法高亮。这样做的好处是它是一个可以在 Heroku 上运行的纯 ruby​​ 解决方案。

I ended up switching to kramdown to parse markdown which comes with coderay for syntax highlighting. This has the benefit of being a pure ruby solution which works on heroku.

像极了他 2024-12-30 02:36:18

第 1 步。安装Redcarpet

gem install redcarpet

第 2 步。像这样更新 _config.yaml 中的构建设置。

# Build settings
#markdown: kramdown
markdown: redcarpet

Step 1. Install Redcarpet.

gem install redcarpet

Step 2. Update the build settings in your _config.yaml like this.

# Build settings
#markdown: kramdown
markdown: redcarpet
十级心震 2024-12-30 02:36:18

在最新的jekyll中支持代码块,但是如果你使用旧版本,你需要破解。

下面怎么样?
尝试将以下文件添加为您的 _plugin/triple-backtick.rb

module Jekyll
  class MarkdownConverter
    alias :old_convert :convert
    def convert(content)
      content.gsub!(/(?:^|\n)```(\w*)\n(.*\n)```\n/m) do |text|
        cls = $1.empty? ? "prettyprint" : "prettyprint lang-#{$1}"
        "<pre class=\"#{cls}\"><code>#{$2}</code></pre>"
      end
      old_convert(content)
    end
  end
end

In latest jekyll support code blocks, but if you use older version, you need to hack.

How about below?
Try to add below's file as your _plugin/triple-backtick.rb

module Jekyll
  class MarkdownConverter
    alias :old_convert :convert
    def convert(content)
      content.gsub!(/(?:^|\n)```(\w*)\n(.*\n)```\n/m) do |text|
        cls = $1.empty? ? "prettyprint" : "prettyprint lang-#{$1}"
        "<pre class=\"#{cls}\"><code>#{$2}</code></pre>"
      end
      old_convert(content)
    end
  end
end
洒一地阳光 2024-12-30 02:36:18

默认情况下,Redcarpet 已集成到 Jekyll 中,并且代码突出显示将按预期运行。

对于较旧的 Jekyll 博客:

  1. 安装 redcarpet gem:

    gem install redcarpet

  2. 更新_config.yaml

    降价:红地毯
    

有关参考和更多信息,请参阅:

已关闭的 Github 问题< /a>

更新了 Jekyll 代码库

Redcarpet is integrated integrated into Jekyll by default and code highlighting will function as expected.

For older Jekyll blogs:

  1. Install redcarpet gem:

    gem install redcarpet

  2. Update _config.yaml

    markdown: redcarpet
    

For reference and further info see:

Closed Github Issue

Updated Jekyll Codebase

知你几分 2024-12-30 02:36:18

所以我也遇到了这个问题,在尝试了很多地方后终于意识到 Jekyll 中的官方 redcarpet2 支持非常简单。
将其写入您的 _config.yml

# Conversion
markdown: redcarpet
highlighter: pygments
redcarpet:
  extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "strikethrough", "superscript"]

确保您有 pygments css 文件并且它已包含在内。这一步很重要。

您可以阅读我的博客文章 http:// blog.championswimmer.in/2015/10/jekyllsyntax-highlighting-in-github-favored-markdown-codeblocks/ 了解详细信息。

So I ran into this problem as well and after banging my head around a lot of places finally realised with official redcarpet2 support in Jekyll this is pretty simple.
Write this in your _config.yml

# Conversion
markdown: redcarpet
highlighter: pygments
redcarpet:
  extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "strikethrough", "superscript"]

Make sure that you have pygments css file and it is included. THIS STEP IS IMPORTANT.

You can read my blog post http://blog.championswimmer.in/2015/10/jekyllsyntax-highlighting-in-github-favoured-markdown-codeblocks/ for details.

最美不过初阳 2024-12-30 02:36:18

您还可以使用三重波形符语法:

~~~ruby
class Base
  def two
    1 + 1
  end
end
~~~

Kramdown (Jekyll) 支持该语法。

You can also use the triple-tilde syntax:

~~~ruby
class Base
  def two
    1 + 1
  end
end
~~~

which is supported by Kramdown (Jekyll).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文