javascriptgoogle-code-prettify

Use code-prettify in javascript using local pretty.css?


code-prettify seems to want to grab pretty.css from a cdn, rather than use a local copy - is it possible to configure things so that it uses the local version without a network call?


Solution

  • The run_prettify.js loads from a CDN and uses query flags to figure out what to load:

    <head>
      <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
    </head>
    <body>
      ...
    </body>
    

    The prettify.js script gives you more control over loading but you have to remember to load the language handlers you need and call PR.prettyPrint() on load:

    <head>
      <script src="/path/to/prettify.js"></script>
      <link rel="stylesheet" href="/path/to/prettify.css" />
      <!-- You would also need to load the language handlers you need here. -->
    </head>
    <body onload="PR.prettyPrint()">
      ...
    </body>
    

    You can find prettify.{js,css} and the language handlers in https://github.com/google/code-prettify/tree/master/src