{"id":983,"date":"2021-05-07T08:09:01","date_gmt":"2021-05-07T08:09:01","guid":{"rendered":"http:\/\/www.ishygddt.xyz\/~blog\/?p=983"},"modified":"2022-03-08T18:51:27","modified_gmt":"2022-03-08T18:51:27","slug":"syntax-highlighting-for-inline-code-in-wordpress","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/05\/syntax-highlighting-for-inline-code-in-wordpress","title":{"rendered":"Applying highlight.js to inline code on WordPress"},"content":{"rendered":"<p>If you're using the plugin <a href=\"https:\/\/wordpress.org\/plugins\/prismatic\/\">Prismatic<\/a> to apply <a href=\"https:\/\/highlightjs.org\/\">highlight.js<\/a> to your code excerpts, but you want it to also affect <em>inline<\/em> <code class=\"language-html\" data-line=\"\">&lt;code&gt;<\/code> tags (i.e. not <em>only<\/em> those wrapped in <code class=\"language-html\" data-line=\"\">&lt;pre&gt;<\/code> blocks), simply go Settings &gt; Prismatic &gt; Highlight.js and paste this into the \u201cInit Script\u201d field:<\/p>\n<pre><code class=\"language-javascript\" data-line=\"\">hljs.configure({ cssSelector: &#039;code&#039; });\nhljs.highlightAll();<\/code><\/pre>\n<p>\u2014 then be sure to remember to add the <code class=\"language-css\" data-line=\"\">language-*<\/code> classes to your inline <code class=\"language-html\" data-line=\"\">&lt;code&gt;<\/code> tags!<\/p>\n<p>You may also want to add this site-specific CSS:<\/p>\n<pre><code class=\"language-css\" data-line=\"\">\/* why does hljs put such phat padding in code blocks by default lol *\/\ncode.hljs {\n\tpadding: 1pt 2pt;\n}\npre code.hljs {\n\tpadding: revert;\n}<\/code><\/pre>\n<hr \/>\n<p>When I first wrote this, Prismatic was shipping with Highlight.js version 10.5.0 (and the latest version was 10.7.2). Highlight.js version 11 has <em>since<\/em> shipped with some API changes, including refactoring the <code class=\"language-javascript\" data-line=\"\">highlightAll<\/code> function that you're supposed to use so that it can be used with a custom selector, yielding the easy solution above.<\/p>\n<p>If you are, for some weird reason, still running an <strong>older version<\/strong> (&lt;11) of Highlight.js, or an older version (&lt;3) of Prismatic, I've kept around the legacy init script that originally warranted this blogpost\u2014it is forwards-compatible, so you can use it with no fear; it will continue working <em>through<\/em> the Prismatic 3.0 update:<\/p>\n<pre><code class=\"language-javascript\" data-line=\"\">\/\/ This init script was written by James E.\n\/\/ on May 7th, 2021. Its purpose is to enable\n\/\/ highlight.js to apply to &quot;inline&quot; code tags\n\/\/ (i.e. those that are the child of a p or\n\/\/ even a span element), as well as &quot;block&quot; code\n\/\/ i.e. those wrapped in pre tags on WordPress).\n\n\/\/ As of Prismatic version 3.0 (hljs version 11),\n\/\/ released on January 13th, 2022, this convoluted\n\/\/ monster of a script is no longer needed.\n\/\/ While it is forwards-compatible and shouldn&#039;t\n\/\/ break when you update the plugin, you should\n\/\/ probably replace it with the newer syntax as\n\/\/ soon as you can:\n\/\/ http:\/\/www.ishygddt.xyz\/~blog\/2021\/05\/syntax-highlighting-for-inline-code-in-wordpress\n\/\/ If, however, you do experience breakage caused\n\/\/ by this script interacting with a Prismatic update,\n\/\/ please leave a comment on the above post and I&#039;ll\n\/\/ try to fix it, albeit too late to matter for you.\n\nwindow.addEventListener(&#039;DOMContentLoaded&#039;, event =&gt; {\n  if((&#039;highlightElement&#039; in hljs) || !(&#039;highlightBlock&#039; in hljs)) console.warn(&quot;This init script was designed for an older version of Highlight.js.\\nThe administrator of this website should install the newer version at the earliest opportunity:\\nhttps:\/\/github.com\/highlightjs\/highlight.js\/issues\/945#issuecomment-835573185&quot;);\n  let sheet = event.target.getElementById(&#039;prismatic-highlight-css&#039;).sheet;\n  sheet.addRule(&#039;.hljs:not(pre&gt;*)&#039;, &#039;padding: revert&#039;);\n  Array.from(sheet.rules)\n  .filter(rule =&gt; rule.selectorText == &#039;.hljs&#039;\n               &amp;&amp; rule.style.display == &#039;block&#039;)\n  .forEach(rule =&gt; {\n    rule.style.removeProperty(&#039;display&#039;);\n  });\n  event.target.querySelectorAll(&#039;code&#039;)\n  .forEach(hljs.highlightElement || hljs.highlightBlock);\n}, false);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you're using the plugin Prismatic to apply highlight.js to your code excerpts, but you want it to also affect inline &lt;code&gt; tags (i.e. not only those wrapped in &lt;pre&gt; blocks), simply go Settings &gt; Prismatic &gt; Highlight.js and paste this into the \u201cInit Script\u201d field: hljs.configure({ cssSelector: &#039;code&#039; }); hljs.highlightAll(); \u2014 then be sure &hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[22,61,62],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","category-drafts","tag-meta","tag-syntax-highlighting","tag-wordpress"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":37,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"predecessor-version":[{"id":1735,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/983\/revisions\/1735"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}