如何让标记和谷歌代码美化一起工作?

Fre*_*ind 2 javascript codehighlighter

我正在使用标记将一些降价代码转换为html,后者有一些代码块.所以我想用google-code-prettify突出显示代码.

标记为代码提供了回调,如下所示:

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

但是我没有找到类似javascritHighlighter(..)google-code-prettify的方法.如何让他们一起工作?

小智 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)
Run Code Online (Sandbox Code Playgroud)

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

prettyPrintOne(code, 'js', false)
Run Code Online (Sandbox Code Playgroud)