Skip to content

Add max-height setting for code blocks - #310

Open
faisalahammad wants to merge 1 commit into
Automattic:masterfrom
faisalahammad:fix/282-max-height
Open

Add max-height setting for code blocks#310
faisalahammad wants to merge 1 commit into
Automattic:masterfrom
faisalahammad:fix/282-max-height

Conversation

@faisalahammad

Copy link
Copy Markdown

Fixes #282

Changes proposed in this Pull Request

Adds a global "Max Height (in pixels)" setting to the SyntaxHighlighter settings page. When set, code blocks get a max-height with a vertical scrollbar instead of stretching the page, which addresses the reporter's request for a settings-page way to cap the length of displayed code.

The setting is off by default (0), so existing sites are unchanged. It applies to both shortcode and Gutenberg block output, independent of which SyntaxHighlighter engine version is active (v2 or v3). No build step is required (PHP only).

What changed in syntaxhighlighter.php:

  • Default of 0 added to the settings defaults (disabled).
  • A text input on the settings page in the first (page-level) section.
  • Validation sanitizes to a non-negative integer (0 disables).
  • When set, the footer script injects a <style> element into <head>:
.syntaxhighlighter {
    max-height: 300px !important;
    overflow-y: auto !important;
}

The !important rules are needed because the SyntaxHighlighter core stylesheet sets overflow-y: hidden !important on .syntaxhighlighter; the injected style comes after in source order so it wins.

I picked pixels over a line count because the SH3 engine forces an 8px line height while SH2 uses 1.1em, so a line-based setting would render inconsistently between versions.

Testing instructions

  1. Install this branch on a WordPress site with a post that contains both a [sourcecode] shortcode block and a syntaxhighlighter/code Gutenberg block, each with 50+ lines of code.
  2. Go to Settings > SyntaxHighlighter and enter 300 in "Max Height (in pixels)", then Save Changes.
  3. View the post on the front end. Expected: both blocks cap their height at 300px and show a vertical scrollbar.
  4. Go back to settings and enter 0, then Save Changes. Expected: code blocks render at full height, same as before this change.
  5. Repeat with the Highlighter Version setting set to both Version 3.x and Version 2.x; the cap and scrollbar should work under both.

New/Updated Hooks

None. No new or updated actions or filters.

Deprecated Code

None. No code is being deprecated.

Screenshot / Video

No UI screenshots submitted for this change (settings page field only, no design change).

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 Automattic#282
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vertical scrollbar

1 participant