You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

  1. # Highlight.js
  2. [![Build Status](https://travis-ci.org/isagalaev/highlight.js.svg?branch=master)](https://travis-ci.org/isagalaev/highlight.js)
  3. Highlight.js is a syntax highlighter written in JavaScript. It works in the
  4. browser as well as on the server. It works with pretty much any markup,
  5. doesn't depend on any framework and has automatic language detection.
  6. ## Getting Started
  7. The bare minimum for using highlight.js on a web page is linking to the library
  8. along with one of the styles and calling [`initHighlightingOnLoad`][1]:
  9. ```html
  10. <link rel="stylesheet" href="/path/to/styles/default.css">
  11. <script src="/path/to/highlight.pack.js"></script>
  12. <script>hljs.initHighlightingOnLoad();</script>
  13. ```
  14. This will find and highlight code inside of `<pre><code>` tags trying to detect
  15. the language automatically. If automatic detection doesn't work for you, you can
  16. specify the language in the class attribute:
  17. ```html
  18. <pre><code class="html">...</code></pre>
  19. ```
  20. The list of supported language classes is available in the [class reference][8].
  21. Classes can also be prefixed with either `language-` or `lang-`.
  22. To disable highlighting altogether use the `nohighlight` class:
  23. ```html
  24. <pre><code class="nohighlight">...</code></pre>
  25. ```
  26. ## Custom Initialization
  27. When you need a bit more control over the initialization of
  28. highlight.js, you can use the [`highlightBlock`][2] and [`configure`][3]
  29. functions. This allows you to control *what* to highlight and *when*.
  30. Here's an equivalent way to calling [`initHighlightingOnLoad`][1] using jQuery:
  31. ```javascript
  32. $(document).ready(function() {
  33. $('pre code').each(function(i, block) {
  34. hljs.highlightBlock(block);
  35. });
  36. });
  37. ```
  38. You can use any tags instead of `<pre><code>` to mark up your code. If you don't
  39. use a container that preserve line breaks you will need to configure
  40. highlight.js to use the `<br>` tag:
  41. ```javascript
  42. hljs.configure({useBR: true});
  43. $('div.code').each(function(i, block) {
  44. hljs.highlightBlock(block);
  45. });
  46. ```
  47. For other options refer to the documentation for [`configure`][3].
  48. ## Getting the Library
  49. You can get highlight.js as a hosted or custom-build browser script or as a
  50. server module. Head over to the [download page][4] for all the options.
  51. Note, that the library is not supposed to work straight from the source on
  52. GitHub, it requires building. If none of the pre-packaged options work for you
  53. refer to the [building documentation][5].
  54. ## License
  55. Highlight.js is released under the BSD License. See [LICENSE][10] file for
  56. details.
  57. ## Links
  58. The official site for the library is at <https://highlightjs.org/>.
  59. Further in-depth documentation for the API and other topics is at
  60. <http://highlightjs.readthedocs.org/>.
  61. Authors and contributors are listed in the [AUTHORS.en.txt][9] file.
  62. [1]: http://highlightjs.readthedocs.org/en/latest/api.html#inithighlightingonload
  63. [2]: http://highlightjs.readthedocs.org/en/latest/api.html#highlightblock-block
  64. [3]: http://highlightjs.readthedocs.org/en/latest/api.html#configure-options
  65. [4]: https://highlightjs.org/download/
  66. [5]: http://highlightjs.readthedocs.org/en/latest/building-testing.html
  67. [8]: http://highlightjs.readthedocs.org/en/latest/css-classes-reference.html
  68. [9]: https://github.com/isagalaev/highlight.js/blob/master/AUTHORS.en.txt
  69. [10]: https://github.com/isagalaev/highlight.js/blob/master/LICENSE