2012-09-20 92 views
4

在tumblr中,支持降价。 我尝试在我的tumblr中添加一个语法较高的打火机(highlight.js),但遇到了一些问题。用Markdown写html标签?

highlight.js需要在HTML代码标记中添加属性。

我尝试写这样的文章在我的tumblr:

<pre class="test"> 
    <code class="html"> 
     function A(){ return "hello world"; } 
    </code> 
</pre> 

在实际网页中的结果:

<pre> 
    <code> 
     function A(){ return "hello world"; } 
    </code> 
</pre> 

属性消失了...... 有没有可能在Markdown中添加HTML属性?

回答

1

我刚试过,这并不难。

http://softwaremaniacs.org/soft/highlight/en/description/在第一个代码片段中描述了您需要插入到您的tumblr页面的html以使其工作。 在预览模式下,代码片段不会加载突出显示。

为了能够使用荧光笔,您需要能够链接样式表和JavaScript。如果你没有托管,那么highlight.js的人可以免费提供托管解决方案。

添加这3条线路的<head>...</head>标签内

<link rel="stylesheet" href="http://yandex.st/highlightjs/7.2/styles/default.min.css"> 
<script src="http://yandex.st/highlightjs/7.2/highlight.min.js"></script> 
<script>hljs.initHighlightingOnLoad();</script> 

所有我加入到我的代码后在tumblr是

<pre><code class="language-python"> 
...your code here... 
</code></pre> 
3

如果使用google-code-prettify,你可以这样做(我目前做这个):

$(document).ready(function() { 
    $('pre').addClass('prettyprint'); 
    prettyPrint(); 
}); 

基本上,你加载所有的文件:

prettify.css 
sunburst.css // optional styles 
prettify.js 

添加代码段中,并调用prettyPrintonLoadonload="prettyPrint()"

上面的代码片段应该放在所有文件之前。它会查找所有pre元素,并将prettyprint类追加到该元素,以便脚本知道样式。


但是,如果你想使用当前的语法高亮,你可以做这样的事情(使用jQuery):

$(document).ready(function() { 
    $('pre').addClass('test'); 
    $('code').addClass('html'); 
    // code to intialize highlight.js 
}); 
+0

如果'的onload = “prettyPrint()”'已添加在''中,'ready()'的参数中不需要'prettyPrint()'。 –