Skip to content

Commit

Permalink
Move code meta information to child text nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Oct 27, 2020
1 parent deed00c commit e08ff6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
overflow: auto;
}

.shcb-language {
display: none;
}

.hljs {
box-sizing: border-box;
}
Expand Down
36 changes: 18 additions & 18 deletions syntax-highlighting-code-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,10 @@ function get_language_names() {
* @param string $pre_start_tag The `<pre>` start tag.
* @param string $code_start_tag The `<code>` start tag.
* @param array $attributes Attributes.
* @param string $content Content.
* @return string Injected markup.
*/
function inject_markup( $pre_start_tag, $code_start_tag, $attributes ) {
function inject_markup( $pre_start_tag, $code_start_tag, $attributes, $content ) {
$added_classes = 'hljs';

if ( $attributes['language'] ) {
Expand Down Expand Up @@ -441,20 +442,21 @@ function inject_markup( $pre_start_tag, $code_start_tag, $attributes ) {
);
}

$language_names = get_language_names();
$language_name = isset( $language_names[ $attributes['language'] ] ) ? $language_names[ $attributes['language'] ] : $attributes['language'];

$pre_start_tag = str_replace(
'>',
sprintf(
' data-language-slug="%s" data-language-name="%s">',
esc_attr( $attributes['language'] ),
esc_attr( $language_name )
),
$pre_start_tag
);
$end_tags = '</code></div>';

return $pre_start_tag . get_styles( $attributes ) . '<div>' . $code_start_tag;
if ( ! empty( $attributes['language'] ) ) {
$language_names = get_language_names();
$language_name = isset( $language_names[ $attributes['language'] ] ) ? $language_names[ $attributes['language'] ] : $attributes['language'];
$end_tags .= sprintf(
'<small class="shcb-language"><span class="shcb-language__label">%s</span> <span class="shcb-language__name">%s</span> <span class="shcb-language__slug">%s</span></small>',
esc_html__( 'Code language:', 'syntax-highlighting-code-block' ),
esc_attr( $language_name ),
esc_attr( $attributes['language'] )
);
}
$end_tags .= '</pre>';

return $pre_start_tag . get_styles( $attributes ) . '<div>' . $code_start_tag . $content . $end_tags;
}

/**
Expand Down Expand Up @@ -483,8 +485,6 @@ function render_block( $attributes, $content ) {
unset( $attributes['showLines'] );
}

$end_tags = '</code></div></pre>';

/**
* Filters the list of languages that are used for auto-detection.
*
Expand All @@ -496,7 +496,7 @@ function render_block( $attributes, $content ) {
$highlighted = get_transient( $transient_key );

if ( ! DEVELOPMENT_MODE && $highlighted && isset( $highlighted['content'] ) ) {
return inject_markup( $matches['pre_start_tag'], $matches['code_start_tag'], $highlighted['attributes'] ) . $highlighted['content'] . $end_tags;
return inject_markup( $matches['pre_start_tag'], $matches['code_start_tag'], $highlighted['attributes'], $highlighted['content'] );
}

try {
Expand Down Expand Up @@ -553,7 +553,7 @@ function render_block( $attributes, $content ) {
set_transient( $transient_key, compact( 'content', 'attributes' ), MONTH_IN_SECONDS );
}

return inject_markup( $matches['pre_start_tag'], $matches['code_start_tag'], $attributes ) . $content . $end_tags;
return inject_markup( $matches['pre_start_tag'], $matches['code_start_tag'], $attributes, $content );
} catch ( Exception $e ) {
return sprintf(
'<!-- %s(%s): %s -->%s',
Expand Down

0 comments on commit e08ff6e

Please sign in to comment.