From fdff760615b4b15073c110063adabe9954f4f460 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Sun, 2 Aug 2026 14:24:35 +0600 Subject: [PATCH] Add max-height setting for code blocks Adds a global "Max Height" setting to the SyntaxHighlighter settings page. When set (in pixels), code blocks get a max-height with a vertical scrollbar so long code no longer stretches the page. Setting is off by default (0). Applies to both shortcode and Gutenberg block output. No build step needed (PHP only). Fixes #282 --- syntaxhighlighter.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/syntaxhighlighter.php b/syntaxhighlighter.php index ee3b9d0..823cece 100644 --- a/syntaxhighlighter.php +++ b/syntaxhighlighter.php @@ -101,6 +101,7 @@ function_exists( 'parse_blocks' ) // WordPress 5.0+ 'collapse' => 0, 'firstline' => 1, 'gutter' => 1, + 'maxheight' => 0, // 0 disables the max-height cap 'htmlscript' => 0, 'light' => 0, 'padlinenumbers' => 'false', @@ -1138,6 +1139,14 @@ function maybe_output_scripts() { echo " SyntaxHighlighter.defaults['quick-code'] = false;\n"; } + if ( ! empty( $this->settings['maxheight'] ) ) { + $maxheight = (int) $this->settings['maxheight']; + echo " var maxheightcss = document.createElement( 'style' );\n"; + echo " maxheightcss.setAttribute( 'type', 'text/css' );\n"; + echo " maxheightcss.appendChild( document.createTextNode( '" . esc_attr( ".syntaxhighlighter { max-height: {$maxheight}px !important; overflow-y: auto !important; }" ) . "' ) );\n"; + echo " document.head.appendChild( maxheightcss );\n"; + } + ?> SyntaxHighlighter.all(); // Infinite scroll support @@ -1490,6 +1499,14 @@ function settings_page() { ?> + + + + +
+ + +

@@ -1811,6 +1828,7 @@ function validate_settings( $settings ) { $settings['classname'] = ( ! empty( $settings['classname'] ) ) ? preg_replace( '/[^ A-Za-z0-9_-]*/', '', $settings['classname'] ) : ''; $settings['firstline'] = (int) ( ( ! empty( $settings['firstline'] ) ) ? $settings['firstline'] : $this->defaultsettings['firstline'] ); $settings['tabsize'] = (int) ( ( ! empty( $settings['tabsize'] ) ) ? $settings['tabsize'] : $this->defaultsettings['tabsize'] ); + $settings['maxheight'] = (int) max( 0, ( $settings['maxheight'] ?? $this->defaultsettings['maxheight'] ) ); } return $settings;