westonruter / syntax-highlighting-code-block

Extending the WordPress Code block with syntax highlighting rendered on the server, thus having faster frontend performance and being AMP-compatible.
https://wordpress.org/plugins/syntax-highlighting-code-block/
GNU General Public License v2.0
118 stars 11 forks source link

Shortcodes are evaluated instead of printed as text #668

Closed TobiasBg closed 1 year ago

TobiasBg commented 1 year ago

When adding a Shortcode to a core/code block, the Shortcode is printed as text instead of being evaluated (this is useful e.g. for documentation pages of a plugin's Shortcodes).

When then activating "Syntax-highlighting Code Block (with Server-side Rendering)" version 1.3.1 (tested on WP 6.1.1 and trunk), the Shortcode is however evaluated.

To reproduce this:

  1. Add a small plugin that adds a Shortcode:
    
    <?php
    /*
    Plugin Name: Shortcode Test
    Description: Shortcode Test
    Version: 1.0
    */

add_shortcode( 'test', 'shortcode_test' ); function shortcode_test( $atts, $content ) { return 'Just a test'; }


2. Add a "Code" block to a post or page with the Shortcode `[test]` as the content.
3. Compare the page output with "Syntax-highlighting Code Block (with Server-side Rendering)" deactivated and activated.
TobiasBg commented 1 year ago

My feeling is that the output that is generated by the plugin is then again run through do_shortcode() by WordPress, which does not seem to happen for the normal core/code block.

I then tried to use the escaped Shortcode [[test]], which however seems to have a core or Gutenberg bug: https://core.trac.wordpress.org/ticket/57351

As a temporary remedy, inserting a simple replacement

$content = str_replace( '[', '&#91;', $content );

before

if ( ! DEVELOPMENT_MODE ) {
    set_transient( $transient_key, compact( 'content', 'attributes' ), MONTH_IN_SECONDS );
}

in the plugin's render_block() function seems to work.

westonruter commented 1 year ago

Thanks for the report. I'm not sure why this is happening, as we're not applying the_content filters or manually doing shortcodes with the output. I'll try to reproduce the issue in the new year.

You've also verified the issue happens when your shortcode plugin and this plugin are only active with a generic core theme? Is there any other plugin/theme that is causing the issue?

TobiasBg commented 1 year ago

Yes, all this is just with WP 6.1.1, the test Shortcode, and a default block theme.

My feeling (see the details in my previous reply) is that the output is run through the default the_content filters (which includes do_shortcode()), but as the &#91; HTML entity has been replaced by a [, that then gets recognized as a Shortcode.

westonruter commented 1 year ago

Ah, a Block theme. Can you try with an older theme, like Twenty Twenty One?

TobiasBg commented 1 year ago

Sure, just tested Twenty Twenty One, Twenty Twenty, Twenty Seventeen, and Twenty Ten. Same problem everywhere, so it's not related to block themes. :-/

westonruter commented 1 year ago

Sorry for the delay. I can reproduce the issue.

This was also reported as an issue for Gutenberg in https://github.com/WordPress/gutenberg/issues/13927 and fixed in https://github.com/WordPress/gutenberg/pull/13996. The fix was for the block's save function to output &#91; instead of [. However, this is getting undone by us since we need the entities removed for passing into highlight.php:

https://github.com/westonruter/syntax-highlighting-code-block/blob/b42cac47bf2a7cf1d17cd5fffb16cb479e4a6d48/syntax-highlighting-code-block.php#L598

https://github.com/westonruter/syntax-highlighting-code-block/blob/b42cac47bf2a7cf1d17cd5fffb16cb479e4a6d48/syntax-highlighting-code-block.php#L609-L613

So as you suggest, re-encoding the special characters may be the solution. Nevertheless, it'll have to be careful to not accidentally encode any of the markup that highlight.php is generating. This may require doing two passes, first to replace all tags with placeholders, then do the entity encoding, and then replace the placeholders with the original tags. I'll give it a shot.

TobiasBg commented 1 year ago

Thanks for looking into this! I've been using that str_replace() from https://github.com/westonruter/syntax-highlighting-code-block/issues/668#issuecomment-1356868855 for now, and that seems to have helped a little bit (e.g. on this page) but not if that page content is loaded in a Loop (see this page and open the "The Shortcode [table id=N /]" section). But that might also be caused by Core#57351 or rather Core#55996 .

But something more robust than that str_replace() will probably be good.

westonruter commented 1 year ago

I've got a PR open to address the shortcode problem in https://github.com/westonruter/syntax-highlighting-code-block/pull/696

westonruter commented 1 year ago

This has been released on WordPress.org as part of v1.4.0.

TobiasBg commented 1 year ago

Great! Thanks, @westonruter!