📜  highlight.js - Javascript (1)

📅  最后修改于: 2023-12-03 15:15:31.922000             🧑  作者: Mango

Highlight.js - Javascript

Highlight.js is a syntax highlighting library written in Javascript. It is easy to use and supports many programming languages and markup formats. With Highlight.js, you can add syntax highlighting to your web pages and make your code snippets look more professional.

Features
  • Easy to use: Highlight.js can be used with just a few lines of code. It is designed to be simple and lightweight.
  • Language support: Highlight.js supports over 180 programming languages and markup formats.
  • Auto-detection: Highlight.js can automatically detect the language of your code snippet.
  • Themes: Highlight.js has many built-in themes that you can use to customize the look of your code snippets.
  • Extensions: Highlight.js has many extensions that you can use to add additional functionality to your code snippets, such as line numbers and copy to clipboard buttons.
Usage

To use Highlight.js in your web page, you first need to include the library in your HTML file:

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>

Next, you need to specify which language to highlight. For example, to highlight a code snippet in Javascript, you would use the following code:

<pre><code class="javascript">function helloWorld() {
  console.log("Hello, world!");
}</code></pre>

Finally, you need to initialize Highlight.js:

<script>hljs.highlightAll();</script>
Themes

Highlight.js comes with many built-in themes that you can use to customize the look of your code snippets. To use a theme, you need to include it in your HTML file:

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/dracula.min.css">

Then, you can use the theme by adding a class attribute to your code block:

<pre><code class="javascript hljs dracula">function helloWorld() {
  console.log("Hello, world!");
}</code></pre>
Extensions

Highlight.js also comes with many extensions that you can use to add additional functionality to your code snippets. To use an extension, you need to include it in your HTML file:

<script src="//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.9.0/highlightjs-line-numbers.min.js"></script>

Then, you can use the extension by adding a class attribute to your code block:

<pre><code class="javascript hljs" data-line-numbers>function helloWorld() {
  console.log("Hello, world!");
}</code></pre>
Conclusion

Highlight.js is a powerful and flexible library for syntax highlighting. With its easy-to-use API, language support, themes, and extensions, you can make your code snippets look great and provide valuable functionality to your users. Consider using Highlight.js in your next web project!