Syntax-highlighting Code Block (with Server-side Rendering)

Description

This plugin extends the Code block in WordPress core to add syntax highlighting which is rendered on the server. Pre-existing Code blocks on a site are automatically extended to include syntax highlighting. Doing server-side syntax highlighting eliminates the need to enqueue any JavaScript on the frontend (e.g. Highlight.js or Prism.js) and this ensures there is no flash of unhighlighted code (FOUC?). Reducing script on the frontend improves frontend performance and it also allows for the syntax highlighted code to appear properly in AMP pages as rendered by the official AMP plugin (see also ampproject/amp-wp#972) or when JavaScript is turned off in the browser.

In addition to not adding any JavaScript to the frontend, the stylesheets needed for styling the Code block will only be added to the page if there is a Code block present. The stylesheets are added inline when the Code block is rendered, ensuring that they do not block rendering of any content higher in the page. If stylesheets fail to load for any reason, the block simply renders without styling, with one key exception: highlighted lines are wrapped in mark elements so they’ll get highlighted regardless, including in RSS Feeds and posts syndicated in email (as long as the mark element is supported in the client).

This extended Code block uses language auto-detection to add syntax highlighting, but you can override the language in the block’s settings sidebar. (There is currently no syntax highlighting of the Code block in the editor.) The plugin supports all programming languages that highlight.php supports (being a port of highlight.js). The Code block also is extended to support specifying the aforementioned highlighted lines. There is also a checkbox for whether to show line numbers on the frontend (with the numbers being non-selectable). Lastly, given inconsistencies across themes as to whether lines in a Code block should be wrapped, this plugin adds styling to force them to no-wrap by default, with a checkbox to opt-in to wrapping when desired.

For advanced usage, please see the plugin wiki.

This plugin is developed on GitHub. See list of current issues with the plugin. Please feel free to file any additional issues or requests that you may come across. Pull requests are welcome.

Credits

This is a fork of Code Syntax Block by Marcus Kazmierczak (mkaz), which is also available on WordPress.org. Copyright (c) 2018 Marcus Kazmierczak. Licensed under GPL 2.0 or later.

highlight.php is released under the BSD 3-Clause License. Copyright © 2006-2013, Ivan Sagalaev ([email protected]), highlight.js (original author). Copyright © 2013, Geert Bergman ([email protected]), highlight.php

Screenshots

  • Code blocks can be added as normal, optionally overriding the auto-detected language. Also specify any lines to be highlighted, whether to show line numbers, and if the lines should wrap.
  • The Code block renders with syntax highlighting on the frontend without any JavaScript enqueued. Stylesheets are added only when block is on the page.

Reviews

January 21, 2022
Just found this gem of a plugin when looking for code syntax highlighting plugins. No hassle at all to get working, just install and activate and it formats and styles existing code blocks in posts. Could not be happier with this plugin. Wlldone!
November 19, 2021
I've switched to "Syntax-highlighting Code Block" from a similar plugin, mainly because the old one didn't recognize the new Java Text Blocks. This one does, and GTMetrix also reports a heavily reduced DOM size for almost all my pages, for example: from 2,363 elements down to 1,654 elements from 1,842 elements down to 1,442 elements That's an amazing and surprising improvement! I do have one feature request, though: It would be great if one could select a subset of the available languages for the dropdown, so you don't always have to scroll through all 185 languages.
June 7, 2021
Hi, I just wanted to stop by to say thanks for such a great plugin. It does the job perfectly for me. The way you have used CSS allows me to customise the different colours etc for different components and I have even got a dark mode version working (with prefers darkmode CSS). I really hope you are able to keep this project maintained. Cheers Colin
April 12, 2021
Just saw this plugin in the source code of another developers blog. I use(d) another block to highlight my code but instantly bought in the idea of server render the code instead of ship JS to my users. I try to step up my game of resource efficient webdevelopment. So reduce the JS I ship to users - server rendering over client side rendering etc. So this plugin rocks. Also I am glad that changing over will be doable. The plugin I currently use let me switch to your plugin without trouble, so this will be a task for every now and then in the next days. Thanks for this plugin!
March 13, 2021
Why this plugin is awesome. 1) The plugin renders on the server side which is ideally more work for your server. But with proper caching implementation, this approach increases page speed as the client doesn't have to load javascript on the client side. 2) The plugin adds on top of the existing gutenberg code block. Most of the syntax highlighters out there try to provide their own gutenberg block which makes it difficult to switch between plugins. For instance, I was using code syntax block plugin and I had to go to each post to convert them back to regular code blocks. 3) No client side scripts thus optimal for AMP enabled pages. As your AMP pages get cached on Google AMP cache anyway, your server has to generate pages rarely. 4) More theme options compared to other code block highlighters out there.
Read all 20 reviews

Contributors & Developers

“Syntax-highlighting Code Block (with Server-side Rendering)” is open source software. The following people have contributed to this plugin.

Contributors

“Syntax-highlighting Code Block (with Server-side Rendering)” has been translated into 7 locales. Thank you to the translators for their contributions.

Translate “Syntax-highlighting Code Block (with Server-side Rendering)” into your language.

Interested in development?

Browse the code, check out the SVN repository, or subscribe to the development log by RSS.

Changelog

For the plugin’s changelog, please see the Releases page on GitHub.