{"id":1152,"date":"2023-07-03T21:25:20","date_gmt":"2023-07-03T14:25:20","guid":{"rendered":"https:\/\/dev.artru.eu.org\/?p=1152"},"modified":"2023-10-27T14:37:55","modified_gmt":"2023-10-27T07:37:55","slug":"highlightjs-automatic-syntax-highlighting","status":"publish","type":"post","link":"https:\/\/artru.net\/en\/highlightjs-automatic-syntax-highlighting\/","title":{"rendered":"HighlightJS automatically highlights pre, code . syntax"},"content":{"rendered":"<p class=\"wp-block-paragraph\">I have used many syntax highlighting plugins in wordpress. However, these plugins are not satisfied. Especially the feature <strong>automatic<\/strong> highlight syntax instead of having to edit each article to create &quot;Block highlight&quot; or having to add attr attributes like class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After a while, I found HighlightJS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">About HighlightJS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is a syntax highlighting tool written in JavaScript that supports more than 180 languages. It does not depend on any other framework and has automatic language detection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example html file before and after using Highlight.JS.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1262\" height=\"377\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/07\/Example-html-with-HighlightJS.png\" alt=\"Example HTML with HighlightJS\" class=\"wp-image-1176\"\/><figcaption class=\"wp-element-caption\">Example HTML with Highlight JS<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Basic usage<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Automatic code marking<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To use it, you need to embed 2 js and css files in your website for HighlightJS to work. Code <code data-no-translation=\"\" data-no-auto-translation=\"\">hljs.highlightAll()<\/code> By default, it will automatically find and highlight the code inside the tag <code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;pre&gt; &lt;code&gt;<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/gh\/highlightjs\/cdn-release@latest\/build\/styles\/default.min.css\"&gt;\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/gh\/highlightjs\/cdn-release@latest\/build\/highlight.min.js\"&gt;&lt;\/script&gt;\n&lt;script&gt;hljs.highlightAll();&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Manual code marking<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to specify a specific language you want, you can use the attribute <code data-no-translation=\"\" data-no-auto-translation=\"\">class<\/code>=\u201dlanguage-xxx\u201d for tag <code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;pre&gt;, &lt;code&gt;<\/code> there.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;code class=\"language-php\"&gt;...&lt;\/code&gt;\n&lt;pre class=\"language-css\"&gt;&lt;code&gt;...&lt;\/code&gt;&lt;\/pre&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Ignore code block markup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To completely skip marking a block of code, use <code data-no-translation=\"\" data-no-auto-translation=\"\">class=\"nohighlight\"<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;code class=\"nohighlight\"&gt;...&lt;\/code&gt;\n&lt;pre class=\"nohighlight\"&gt;&lt;code&gt;...&lt;\/code&gt;&lt;\/pre&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced usage<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">According to the developer, by default it only detects the language and color, and does not have options like line numbers or copy buttons.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, we can still do it thanks to the DEVs on github developing more features.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Numbering Line Numbers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To add numbering you need to embed additional files <a href=\"https:\/\/github.com\/wcoder\/highlightjs-line-numbers.js\/\" target=\"_blank\" data-schema-attribute=\"about\" rel=\"noreferrer noopener\">highlightjs-line-numbers.js<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/highlightjs-line-numbers.js@latest\/dist\/highlightjs-line-numbers.min.js\"&gt;&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then call the function <code data-no-translation=\"\" data-no-auto-translation=\"\">hljs.initLineNumbersOnLoad()<\/code> right behind <code data-no-translation=\"\" data-no-auto-translation=\"\">hljs.highlightAll()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script&gt;\n  hljs.highlightAll();\n  hljs.initLineNumbersOnLoad();\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Code copy button<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I just found this extension at <a href=\"https:\/\/github.com\/arronhunt\/highlightjs-copy\" target=\"_blank\" data-schema-attribute=\"about\" rel=\"noreferrer noopener\">highlightjs-copy<\/a>. You can refer to more to customize according to your own will.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Like Line Numbers, you need to embed JS and CSS files to customize the copy button.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script src=\"https:\/\/unpkg.com\/highlightjs-copy\/dist\/highlightjs-copy.min.js\"&gt;&lt;\/script&gt;\n&lt;link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/highlightjs-copy\/dist\/highlightjs-copy.min.css\" \/&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike line numbers, you should set the function <code data-no-translation=\"\" data-no-auto-translation=\"\">hljs.addPlugin(new CopyButtonPlugin())<\/code> before <code data-no-translation=\"\" data-no-auto-translation=\"\">hljs.highlightAll()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script&gt;\n  hljs.addPlugin(new CopyButtonPlugin());\n  hljs.initLineNumbersOnLoad();\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Custom js and css files<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can load js, css files and save them internally in your website. It also has a language option for the user to create a separate JS file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"694\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/07\/Custom-package-HighlightJS.png\" alt=\"Custom package HighlightJS\" class=\"wp-image-1214\"\/><figcaption class=\"wp-element-caption\">Custom package Highlight JS<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">So is the CSS. You can choose the syntax themes according to your preferences at <a href=\"https:\/\/github.com\/highlightjs\/highlight.js\/tree\/main\/src\/styles\" target=\"_blank\" rel=\"noreferrer noopener\" data-schema-attribute=\"about\">Styles Github<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Reference source:<\/strong> <a href=\"https:\/\/highlightjs.org\/\" target=\"_blank\" data-schema-attribute=\"about\" rel=\"noreferrer noopener\">highlightjs.org<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>M\u00ecnh \u0111\u00e3 t\u1eebng s\u1eed d\u1ee5ng qua nhi\u1ec1u plugin l\u00e0m n\u1ed5i b\u1eadt c\u00fa ph\u00e1p trong wordpress. Tuy nhi\u00ean c\u00e1c plugin n\u00e0y ch\u01b0a l\u00e0m h\u00e0i l\u00f2ng. \u0110\u1eb7c bi\u1ec7t l\u00e0 t\u00ednh n\u0103ng t\u1ef1 \u0111\u1ed9ng l\u00e0m n\u1ed7i b\u1eadt c\u00fa ph\u00e1p thay v\u00ec ph\u1ea3i ch\u1ec9nh s\u1eeda t\u1eebng b\u00e0i vi\u1ebft \u0111\u1ec3 t\u1ea1o &#8220;Block hightlight&#8221; ho\u1eb7c ph\u1ea3i th\u00eam c\u00e1c thu\u1ed9c t\u00ednh attr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1224,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/1152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/comments?post=1152"}],"version-history":[{"count":0,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/1152\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media\/1224"}],"wp:attachment":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media?parent=1152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/categories?post=1152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/tags?post=1152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}