如何让标记和谷歌代码漂亮地工作在一起

How to let marked and google-code-prettify work together?

本文关键字:漂亮 代码 工作 在一起 谷歌      更新时间:2023-09-26

我正在使用marked将一些markdown代码转换为html,其中包含一些代码块。所以我想使用谷歌代码pretify来突出显示代码。

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;
  }
});

但我在谷歌代码prettify中找不到像javascritHighlighter(..)这样的方法。如何让他们一起工作?

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

/**
 * @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)