Skip to content

Commit 4ed9bec

Browse files
committed
Add Plain Text language option mapped to Prism's language-none
Adds 'text' => 'Plain Text' to the language picker. On render, 'text' is mapped to 'none' via str_replace so Prism applies theme styling without running a syntax highlighter on the content.
1 parent 8af50d1 commit 4ed9bec

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

includes/frontend/class-blocks.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
class Blocks {
2525

26+
2627
/**
2728
* Constructor.
2829
*
@@ -47,7 +48,7 @@ public function enqueue_editor_assets(): void {
4748
return;
4849
}
4950

50-
$asset = require $asset_file;
51+
$asset = include $asset_file;
5152

5253
wp_enqueue_script(
5354
'wzcbh-editor',
@@ -115,7 +116,7 @@ public function enqueue_editor_canvas_styles(): void {
115116
return;
116117
}
117118

118-
$asset = require $asset_file;
119+
$asset = include $asset_file;
119120

120121
$editor_css = WZCBH_PLUGIN_DIR . 'includes/blocks/build/index.css';
121122
if ( file_exists( $editor_css ) ) {
@@ -138,7 +139,7 @@ public function enqueue_editor_canvas_styles(): void {
138139
// Later declarations win, mirroring the CSS cascade.
139140
$theme_path = Settings::get_color_scheme_css( true );
140141
if ( file_exists( $theme_path ) && wp_style_is( 'wzcbh-editor-canvas-style', 'enqueued' ) ) {
141-
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
142+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
142143
$theme_css = file_get_contents( $theme_path );
143144
$bg_value = '';
144145
$color_value = '';
@@ -158,7 +159,7 @@ public function enqueue_editor_canvas_styles(): void {
158159
}
159160

160161
foreach ( preg_split( '/[\n;]/', $declarations ) as $decl ) {
161-
$decl = trim( $decl );
162+
$decl = trim( $decl );
162163
if ( preg_match( '/^background(?:-color)?:\s*.+$/i', $decl ) ) {
163164
$bg_value = $decl . ';';
164165
} elseif ( preg_match( '/^color:\s*.+$/i', $decl ) ) {
@@ -171,7 +172,7 @@ public function enqueue_editor_canvas_styles(): void {
171172
$props = array_filter( array( $bg_value, $color_value ) );
172173
if ( $props ) {
173174
$selectors = '.block-editor-block-list__layout pre[class*="language-"],' .
174-
'.block-editor-block-list__layout code[class*="language-"]';
175+
'.block-editor-block-list__layout code[class*="language-"]';
175176
wp_add_inline_style(
176177
'wzcbh-editor-canvas-style',
177178
$selectors . '{' . implode( ' ', $props ) . '}'
@@ -224,7 +225,7 @@ public function register_rest_routes(): void {
224225
*
225226
* @since 1.1.0
226227
*
227-
* @param \WP_REST_Request $request The REST request.
228+
* @param \WP_REST_Request $request The REST request.
228229
* @return \WP_REST_Response
229230
*/
230231
public function save_default_settings( \WP_REST_Request $request ): \WP_REST_Response {
@@ -269,14 +270,24 @@ public function save_default_settings( \WP_REST_Request $request ): \WP_REST_Res
269270
*
270271
* @since 1.0.0
271272
*
272-
* @param string $block_content The rendered block HTML.
273-
* @param array<string, mixed> $block The block data array.
273+
* @param string $block_content The rendered block HTML.
274+
* @param array<string, mixed> $block The block data array.
274275
* @return string
275276
*/
276277
public function render_code_block( string $block_content, array $block ): string {
277278
$attrs = $block['attrs'] ?? array();
278279

279-
$language = sanitize_key( $attrs['language'] ?? '' );
280+
$language = sanitize_key( $attrs['language'] ?? '' );
281+
282+
// 'text' is stored as the block attribute value but Prism has no 'text'
283+
// grammar. Map it to 'none' so Prism applies theme styling without
284+
// highlighting. Replace in the saved HTML (which already carries
285+
// language-text on both <pre> and <code>) rather than appending.
286+
if ( 'text' === $language ) {
287+
$block_content = str_replace( 'language-text', 'language-none', $block_content );
288+
$language = '';
289+
}
290+
280291
$line_numbers = ! empty( $attrs['lineNumbers'] );
281292
$line_numbers_start = isset( $attrs['lineNumbersStart'] ) ? (int) $attrs['lineNumbersStart'] : 1;
282293
$word_wrap = ! empty( $attrs['wordWrap'] );
@@ -406,6 +417,7 @@ public static function get_languages(): array {
406417
'scala' => 'Scala',
407418
'sql' => 'SQL',
408419
'swift' => 'Swift',
420+
'text' => 'Plain Text',
409421
'toml' => 'TOML',
410422
'tsx' => 'TSX',
411423
'typescript' => 'TypeScript',

0 commit comments

Comments
 (0)