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 (maniac@softwaremaniacs.org), highlight.js (original author). Copyright © 2013, Geert Bergman (geert@scrivo.nl), 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 2, 2021
This plugin is the best syntax highlighting plugin that I have used, thanks for all the hard work in developing it. One recommendation though... I just stumbled upon the ability to change the color scheme in the Customizer. This is an awesome feature, but not one that is highlighted in the description of the plugin or in the screenshots. Of course it is mentioned in the Advanced Usage wiki, but I have been using the plugin for months and definitely overlooked that 😉. A brief mention in the main description and a screenshot of the different theme options would definitely help unobservant users like myself. Thanks again!
October 24, 2020
I love that this is baked into the core code block without having to resort to a third-party integration that may or may not work. Well done on this one. Just a heads up though, which may require more testing. The latest Gutenberg release and this plugin do not appear to play well together. I had to disable the Gutenberg plugin, which invalidated the code blocks, and I had to recover each one. One feature request: Please add a rel attribute so I can display the language. Thanks again, Ronald Huereca
October 11, 2020
This is a great plugin. 1. The code looks just like it does in Visual Studio (theme: vs2015) 2. It improves performance in two ways: -generates html on the server-side, instead of rendering on the client-side. This is the main reason i looked into getting a different syntax highlighting plugin. I use a caching plugin, so it annoyed me that it was doing pointless client-side rendering. -the generated html is 3x smaller than the other plugin i was using. So the payload is smaller, and it's simply less work for the browser to paint. 3. It's simple. You just built-in Code block and change the language after you paste. 4. It has line wrapping. My current plugin doesn't have this, so it puts scroll bars on the code (looks bad) Great job with this plugin!
July 31, 2020
The original - Code Syntax Block by Marcus Kazmierczak worked really well, this fork by Weston just elevates the functionality to new levels. Server side formatting eliminates the JS dependency making it load faster and... is AMP compatible. Fantastic, 5 stars, give it a try!
Read all 14 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.