How to use <Format code> to display code in a <code> tag

I tried a number of different approaches and and finally figured out the secret sauce to using <Format code> with a <code> tag.

I want to embed a bit of code (Sinclair BASIC, so Prism’s not an option). The solution I finally found was this:

<Raw><pre class="wp-block-code"><code></Raw>
   <Format code><Field source_code /></Format>
<Raw></code></pre></Raw>

Without the <Raw> tag, the <code> would cause the L&L commands to be output as the contents of the <code> block.

The Gutenberg syntax highlighter will complain about the opening <code> in the first <Raw> block. You can ignore that.

1 Like

Nice! Thanks for the sharing a useful trick. I wondered if there’s a way to make it easier to achieve this, and found a probably undocumented feature.

The code tag supports an attribute called render that renders any dynamic tags in its content. So maybe like this:

<pre class="wp-block-code">
<code render><Format code><Field source_code /></Format></code>
</pre>

I put it on one line to avoid extra new line in the code block.

Possibly even shorter with <Field source_code format=code />, if that works.


Sinclair BASIC, that is classic. If I recall it ran on the ZX Spectrum, a thinker toy of retro beauty.