Moving to Gutenberg

The Gutenberg editor is now WordPress’s default and support for the previous Classic editor is through a plugin. That support is not guaranteed after 31 December 2021. I wanted to patch Aram Kocharyan’s Crayon Syntax Highlighter plugin to work with Gutenberg.

Other people have had the same objective. I relied heavily on Fedor Urvanov’s resuscitation of the Syntax Highlighter to understand what was required and largely followed his example.

Registering the block type

On the server side, a block type needs to be registered with the register_block_type() function.

The block type identifier, which comprises the name space and the block name, appears in the HTML comments that delimit the block in the code editor, so I chose a short name for the block:

Cascading Style Sheets (CSS) used by a web page need to be made available to it (enqueued). The 'editor_style' key of the array second argument of register_block_type() enqueues a CSS applied in the editor.

Registering the block

On the client side, the block needs to be registered with the wp.blocks.registerBlockType() function, which takes a string name (the block type) and an Object settings. The settings has properties title, category, icon (optional) and attributes. It also has methods edit and save.

The attributes property is an Object. Each of its properties specifies a source. For example, this specifies that content is a string stored in the inner text of the div HTML element:

The edit method represents what the Gutenberg editor will render when the block is used in the editor. It takes an Object parameter props. In the following, the variable el contains the function wp.element.createElement().

The function renders two elements: a block control containing a toolbar containing an icon button; and a div element containing props.attributes.content.

The onClick event of the icon button is set to a call of the showDialog() method of the CrayonTagEditor class:

The save method represents what the front end will render:

Registering the format

The Format API allows a custom button to be added to the formatting toolbar that applies a format to a text selection. The first step is to register the new format type:

The CrayonButton attached to the edit method renders a richtext toolbar button. The start property of the value property is the index of the first character in the selection and similarly for the end property.

If the selection is of the relevant format type, it is expanded to cover all adjacent indices with the relevant format type. The selection is sliced out of the rich text, converted to HTML, and then to a node, which is passed to a call of the showDialog() method of the CrayonTagEditor class.

JavaScript arrays are objects and have the method slice([begin[, end]]), which extracts from begin up to, but not including, end. wp.richText.slice() behaves similarly.

The format is given class name CRAYON_INLINE_CSS ('crayon-inline'). That class is used to style the format in the editor:

On the server side, the CSS file needs to be registered with wp_register_style() function:

Minifying

JavaScript code includes whitespace characters to make it more intelligible. Similarly, local variable names are usually chosen for intelligibility not brevity. The minification of JavaScript removes unnecessary whitespace and replaces longer local variable names with shorter ones. Similarly, the minification of a CSS file removes characters that are not needed for the CSS to function.

The author of the plugin used minifier application YUICompressor-2.4.7 and bash scripts to minify JavaScript and CSS source code.

util.js, jquery.popup.js and crayon.js were minified into crayon.min.js. Those files and crayon_qt.js, jquery.colorbox-min.js and crayon_tag_editor.js were minified into crayon.te.min.js.

colorbox.css, admin_style.css, crayon_style.css and global_style.css were minified into crayon.min.css.

The compressor was part of the Yahoo User Interface (YUI) library, which is no longer an active project. The most recent release of the application, version 2.4.8, was released in July 2013. Other compressors are available but, as a temporary measure, I used version 2.4.8 and replaced the bash scripts with PowerShell equivalents.

Clearing the browser cache

As I did not update the Crayon Syntax Highlighter version, I found I needed to clear the brower’s cache of images and files for the updated JavaScript files to be recognised.

Registering Crayon posts

The plugin only renders crayons in those posts that are registered as containing one or more crayons. In the original plugin for the Classic editor, that was done by adding functions to update_post, save_post and wp_insert_post_data hooks:

However, in the Gutenberg editor, is_admin() does not return a true value when a draft post is saved or previewed. The solution is to move adding the functions outside of the application of the is_admin() condition.

In addition, I can find no record that WordPress has, or has ever had, an update_post hook (including by reference to Adam R Brown’s database). The use of this hook was introduced by Mr Kocharyan on 1 January 2013. Code adding an action to that hook appears to be redundant.

Using the block

The use of the block is illustrated in the screenshot below. Clicking on the icon button in the tool bar in the block controls bar above the block opens the same dialogue box as used in the Classic editor version of the plugin. The content of the crayon is shown below the block controls bar; initially this is empty.

Example of a Crayon Syntax Highlighter block for the Gutenberg editor.