Ghost代码markdown块中的行号

6

我想知道如何在呈现的markdown代码块中显示行号,特别是在Ghost博客平台上如何做到这一点。如果您能够使代码根据语言颜色编码(与GitHub和其他网站类似),那就更好了。谢谢!

2个回答

13

这篇文章(2013年10月11日)提到:

我刚意识到Ghost已经支持GitHub-Markdown扩展。

因此,您只需在以下行下添加如Google Code Prettify之类的内容即可将其包含在文章中:
/content/themes/casperdefault.hbs.

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js">    
</script>

并在Ghost中使用以下GitHub markdown:

```prettyprint lang-ruby require 'redcarpet' markdown = Redcarpet.new("Hello World!") puts markdown.to_html ```

以上的 Markdown 将生成以下 HTML 代码,然后由 Google Code Prettify 进行美化:

<pre>
  <code class="prettyprint lang-ruby">
    require 'redcarpet' 
    markdown = Redcarpet.new("Hello World!") 
    puts markdown.to_html
  </code>
</pre> 

从那里,您可以在 "google-code-prettify" 阅读更多内容,其中解释了如何添加行号:

您可以使用linenums类打开行号显示。
如果您的代码不是从第1行开始的,您可以在该类的结尾添加冒号和行号,例如 linenums

但是,我还没有测试过是否实际上会在生成的<code>元素的属性中出现该类。

```prettyprint lang-ruby linenumber xxxx

1

我尝试了一切,但都不起作用,最后我在底部添加了这些代码( suggested by google-code),然后一切都开始工作了。

<script>
    $(document).ready(function () {            
        $("pre").each(function () {
            if (($(this).html().split(/\n/).length - 1) > 3) {
                $(this).addClass('prettyprint linenums:1');
            }
        });
    });
 </script>

也许它能帮助任何人!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接