2

我正在使用标记将一些降价代码转换为具有一些代码块的 html。所以我想使用google-code-prettify来突出显示代码。

Marked 为代码提供了回调,如文档所述:

marked.setOptions({
  gfm: true,
  pedantic: false,
  sanitize: true,
  // callback for code highlighter
  highlight: function(code, lang) {
    if (lang === 'js') {
      return javascriptHighlighter(code);
    }
    return code;
  }
});

但我没有找到像javascritHighlighter(..)google-code-prettify 这样的方法。如何让他们一起工作?

4

1 回答 1

6

这是我自己做的。您正在寻找的功能是:

/**
 * @param sourceCodeHtml {string} The HTML to pretty print.
 * @param opt_langExtension {string} The language name to use.
 *     Typically, a filename extension like 'cpp' or 'java'.
 * @param opt_numberLines {number|boolean} True to number lines,
 *     or the 1-indexed number of the first line in sourceCodeHtml.
 */
function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines)

所以你会想要这样的东西:

prettyPrintOne(code, 'js', false)
于 2012-10-06T03:14:06.030 回答