NoteOnMe博客平台搭建
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.

188 lines
5.7 KiB

3 years ago
  1. # Highlight.js
  2. [![Build Status](https://travis-ci.org/highlightjs/highlight.js.svg?branch=master)](https://travis-ci.org/highlightjs/highlight.js) [![Greenkeeper badge](https://badges.greenkeeper.io/highlightjs/highlight.js.svg)](https://greenkeeper.io/)
  3. Highlight.js is a syntax highlighter written in JavaScript. It works in
  4. the browser as well as on the server. It works with pretty much any
  5. markup, doesn’t depend on any framework, and has automatic language
  6. detection.
  7. ## Getting Started
  8. The bare minimum for using highlight.js on a web page is linking to the
  9. library along with one of the styles and calling
  10. [`initHighlightingOnLoad`][1]:
  11. ```html
  12. <link rel="stylesheet" href="/path/to/styles/default.css">
  13. <script src="/path/to/highlight.pack.js"></script>
  14. <script>hljs.initHighlightingOnLoad();</script>
  15. ```
  16. This will find and highlight code inside of `<pre><code>` tags; it tries
  17. to detect the language automatically. If automatic detection doesn’t
  18. work for you, you can specify the language in the `class` attribute:
  19. ```html
  20. <pre><code class="html">...</code></pre>
  21. ```
  22. The list of supported language classes is available in the [class
  23. reference][2]. Classes can also be prefixed with either `language-` or
  24. `lang-`.
  25. To make arbitrary text look like code, but without highlighting, use the
  26. `plaintext` class:
  27. ```html
  28. <pre><code class="plaintext">...</code></pre>
  29. ```
  30. To disable highlighting altogether use the `nohighlight` class:
  31. ```html
  32. <pre><code class="nohighlight">...</code></pre>
  33. ```
  34. ## Custom Initialization
  35. When you need a bit more control over the initialization of
  36. highlight.js, you can use the [`highlightBlock`][3] and [`configure`][4]
  37. functions. This allows you to control *what* to highlight and *when*.
  38. Here’s an equivalent way to calling [`initHighlightingOnLoad`][1] using
  39. vanilla JS:
  40. ```js
  41. document.addEventListener('DOMContentLoaded', (event) => {
  42. document.querySelectorAll('pre code').forEach((block) => {
  43. hljs.highlightBlock(block);
  44. });
  45. });
  46. ```
  47. You can use any tags instead of `<pre><code>` to mark up your code. If
  48. you don't use a container that preserves line breaks you will need to
  49. configure highlight.js to use the `<br>` tag:
  50. ```js
  51. hljs.configure({useBR: true});
  52. document.querySelectorAll('div.code').forEach((block) => {
  53. hljs.highlightBlock(block);
  54. });
  55. ```
  56. For other options refer to the documentation for [`configure`][4].
  57. ## Web Workers
  58. You can run highlighting inside a web worker to avoid freezing the browser
  59. window while dealing with very big chunks of code.
  60. In your main script:
  61. ```js
  62. addEventListener('load', () => {
  63. const code = document.querySelector('#code');
  64. const worker = new Worker('worker.js');
  65. worker.onmessage = (event) => { code.innerHTML = event.data; }
  66. worker.postMessage(code.textContent);
  67. });
  68. ```
  69. In worker.js:
  70. ```js
  71. onmessage = (event) => {
  72. importScripts('<path>/highlight.pack.js');
  73. const result = self.hljs.highlightAuto(event.data);
  74. postMessage(result.value);
  75. };
  76. ```
  77. ## Getting the Library
  78. You can get highlight.js as a hosted, or custom-build, browser script or
  79. as a server module. Right out of the box the browser script supports
  80. both AMD and CommonJS, so if you wish you can use RequireJS or
  81. Browserify without having to build from source. The server module also
  82. works perfectly fine with Browserify, but there is the option to use a
  83. build specific to browsers rather than something meant for a server.
  84. Head over to the [download page][5] for all the options.
  85. **Don't link to GitHub directly.** The library is not supposed to work straight
  86. from the source, it requires building. If none of the pre-packaged options
  87. work for you refer to the [building documentation][6].
  88. **The CDN-hosted package doesn't have all the languages.** Otherwise it'd be
  89. too big. If you don't see the language you need in the ["Common" section][5],
  90. it can be added manually:
  91. ```html
  92. <script
  93. charset="UTF-8"
  94. src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/go.min.js"></script>
  95. ```
  96. **On Almond.** You need to use the optimizer to give the module a name. For
  97. example:
  98. ```bash
  99. r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
  100. ```
  101. ### CommonJS
  102. You can import Highlight.js as a CommonJS-module:
  103. ```bash
  104. npm install highlight.js --save
  105. ```
  106. In your application:
  107. ```js
  108. import hljs from 'highlight.js';
  109. ```
  110. The default import imports all languages! Therefore it is likely to be more efficient to import only the library and the languages you need:
  111. ```js
  112. import hljs from 'highlight.js/lib/highlight';
  113. import javascript from 'highlight.js/lib/languages/javascript';
  114. hljs.registerLanguage('javascript', javascript);
  115. ```
  116. To set the syntax highlighting style, if your build tool processes CSS from your JavaScript entry point, you can import the stylesheet directly into your CommonJS-module:
  117. ```js
  118. import hljs from 'highlight.js/lib/highlight';
  119. import 'highlight.js/styles/github.css';
  120. ```
  121. ## License
  122. Highlight.js is released under the BSD License. See [LICENSE][7] file
  123. for details.
  124. ## Links
  125. The official site for the library is at <https://highlightjs.org/>.
  126. Further in-depth documentation for the API and other topics is at
  127. <http://highlightjs.readthedocs.io/>.
  128. Authors and contributors are listed in the [AUTHORS.en.txt][8] file.
  129. [1]: http://highlightjs.readthedocs.io/en/latest/api.html#inithighlightingonload
  130. [2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
  131. [3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightblock-block
  132. [4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure-options
  133. [5]: https://highlightjs.org/download/
  134. [6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
  135. [7]: https://github.com/highlightjs/highlight.js/blob/master/LICENSE
  136. [8]: https://github.com/highlightjs/highlight.js/blob/master/AUTHORS.en.txt