2

I'm using marked to convert some markdown code to html, which has some code blocks. So I want to use google-code-prettify to highlight the code.

Marked has provided a callback for code, as documented:

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

But I don't find a method like javascritHighlighter(..) from google-code-prettify. How to let them work together?

Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

6

Just did this myself. The function you're looking for is:

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

So you'll want something like:

prettyPrintOne(code, 'js', false)
gclapperton
  • 61
  • 1
  • 2