为 gh-pages 添加语法高亮

发布于 2024-11-19 03:03:17 字数 246 浏览 4 评论 0原文

有没有一种简单的方法可以使用 github 的 Pygments 将语法突出显示添加到我的各种插件的 gh 页面?

我知道每个页面都通过 Jekyll 引擎运行并提供语法突出显示(ref)。但我不想安装博客。我只想将语法突出显示应用于我的 gh 页面中的代码块。

我想我总是可以在我的 gh-pages 中包含一个不同的插件......

Is there an easy way to add syntax highlighting to my various plugin's gh-pages using github's Pygments?

I know that every page runs through the Jekyll engine and provides syntax highlighting (ref). But I don't want to install a blog. I just want syntax highlighting applied to the code blocks in my gh-pages.

I guess I could always just include a different plugin with my gh-pages...

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

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

发布评论

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

评论(5

笙痞 2024-11-26 03:03:17

Pages 已经支持 pygments,无需安装任何内容。就用它吧!

---
layout: default
title: Something with codes
---

Happy fun highlighting. 
[More details](https://github.com/mojombo/jekyll/wiki/liquid-extensions)

{% highlight ruby %}
def foo
  puts 'foo'
end
{% endhighlight %}

Pages already does pygments, there's nothing to install. Just use it!

---
layout: default
title: Something with codes
---

Happy fun highlighting. 
[More details](https://github.com/mojombo/jekyll/wiki/liquid-extensions)

{% highlight ruby %}
def foo
  puts 'foo'
end
{% endhighlight %}
梦与时光遇 2024-11-26 03:03:17

GitHub Pages 现在仅支持 Rouge,纯 Ruby 语法高亮器,因此您只需更改 'kramdown' 语法高亮器即可在 _config 中使用 'rouge' .yml 文件。

markdown: kramdown
kramdown:
  input: GFM
  syntax_highlighter: rouge

"GitHub Pages now only supports Rouge, a pure-Ruby syntax highlighter" so you only need to change 'kramdown' syntax highligher to use 'rouge' in your _config.yml file.

markdown: kramdown
kramdown:
  input: GFM
  syntax_highlighter: rouge
追风人 2024-11-26 03:03:17

在尝试找出语法突出显示时,发现此线程是第一个命中项,并且我发现了一种更简单的方法,我想我会分享。只需将您想要突出显示的语言名称放在受隔离的代码块后面即可(参考 )。无需生成任何 css 或使用 yaml。

This is regular text

```ruby
# This is highlighted code
def foo
  puts 'foo'
end
```
```python
# Here is some in python
def foo():
  print 'foo'
```

Found this thread as the first hit while trying to figure out syntax highlighting, and I found an even easier way to do it that I thought I'd share. Just put the name of the language you want highlighted after the fenced code blocks (reference). No need to generate any css or use yaml.

This is regular text

```ruby
# This is highlighted code
def foo
  puts 'foo'
end
```
```python
# Here is some in python
def foo():
  print 'foo'
```
墨小沫ゞ 2024-11-26 03:03:17

正如 @David Douglas 所指出的,“GitHub Pages 现在仅支持 Rouge,一种纯 Ruby 语法荧光笔”,

您必须将其放入 _config.yml 中。这是来自 Barry 的 _config.yml Clark's Jekyll Now

# Jekyll 3 now only supports Kramdown for Markdown
kramdown:
    # Use GitHub flavored markdown, including triple backtick fenced code blocks
    input: GFM
    # Jekyll 3 and GitHub Pages now only support rouge for syntax highlighting
    syntax_highlighter: rouge
    syntax_highlighter_opts:
        # Use existing pygments syntax highlighting css
        css_class: 'highlight'

然后是代码突出显示部分...

Rouge 的语言别名列表如下:https://github.com/jneen/rouge/wiki/ List-of-supported- languages-and-lexers

它使用全小写的后者。

例如,对于 JavaScript:

``` javascript
function test() {
    console.log("test");
}
```

As pointed out by @David Douglas, "GitHub Pages now only supports Rouge, a pure-Ruby syntax highlighter"

You have to put this in you _config.yml. This is from the _config.yml of Barry Clark's Jekyll Now

# Jekyll 3 now only supports Kramdown for Markdown
kramdown:
    # Use GitHub flavored markdown, including triple backtick fenced code blocks
    input: GFM
    # Jekyll 3 and GitHub Pages now only support rouge for syntax highlighting
    syntax_highlighter: rouge
    syntax_highlighter_opts:
        # Use existing pygments syntax highlighting css
        css_class: 'highlight'

Then for the code highlighting part...

The list of langauge aliases for Rouge are listed here: https://github.com/jneen/rouge/wiki/List-of-supported-languages-and-lexers

It uses all-lowercase latters.

For example, for JavaScript:

``` javascript
function test() {
    console.log("test");
}
```
我的奇迹 2024-11-26 03:03:17

默认语法荧光笔是 rouge(相当于 _config.yml 文件中的 highlighter: rouge)。使用 rouge,如果您在 markdown 中编写这样的代码块:

~~~js
let z = 26;
~~~

您可以期望得到这样的 HTML 块:

<div class="language-js highlighter-rouge">
 <div class="highlight">
  <pre class="highlight"><code>
   <span class="kd">let</span> <span class="nx">z</span> <span class="o">=</span> <span class="mi">26</span><span class="p">;</span>
  </code></pre>
 </div>
</div>

那么您所需要的只是一个 CSS 文件(如果您使用的是 GitHub Pages 主题,您将自动获得 CSS )。我不确定 CSS 正式应该来自哪里,但

您可以根据自己的喜好随意定制 css。

The default syntax highlighter is rouge (equivalent to highlighter: rouge in your _config.yml file). With rouge, if you write a code block like this in markdown:

~~~js
let z = 26;
~~~

You can expect to get an HTML block like this:

<div class="language-js highlighter-rouge">
 <div class="highlight">
  <pre class="highlight"><code>
   <span class="kd">let</span> <span class="nx">z</span> <span class="o">=</span> <span class="mi">26</span><span class="p">;</span>
  </code></pre>
 </div>
</div>

Then all you need is a CSS file (if you're using a GitHub Pages Theme, you will get the CSS automatically). I'm not sure where the CSS is officially supposed to come from, but

Feel free to customize the css to your liking.

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