Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created July 6, 2024 14:00
Show Gist options
  • Save BhargavBhandari90/9f5b549a220fb7c18b3348085d739c59 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/9f5b549a220fb7c18b3348085d739c59 to your computer and use it in GitHub Desktop.
Syntax Highliter for BB
<?php
function bbtest_the_content( $content ) {
$search = array( '<pre>' );
$replace = array( '<pre class="wp-block-syntaxhighlighter-code">' );
$content = str_replace( $search, $replace, $content );
$syntax_obj = new SyntaxHighlighter();
$attributes = array( 'language' => 'as3' );
$remaps = array(
'lineNumbers' => 'gutter',
'firstLineNumber' => 'firstline',
'highlightLines' => 'highlight',
'wrapLines' => 'wraplines',
'makeURLsClickable' => 'autolinks',
'quickCode' => 'quickcode',
);
$classnames = ! empty( $attributes['className'] ) ? array( esc_attr( $attributes['className'] ) ) : array();
if ( ! empty( $attributes['align'] ) ) {
$classnames[] = 'align' . esc_attr( $attributes['align'] );
}
foreach ( $remaps as $from => $to ) {
if ( isset( $attributes[ $from ] ) ) {
if ( is_bool( $attributes[ $from ] ) ) {
$attributes[ $to ] = ( $attributes[ $from ] ) ? '1' : '0';
} else {
$attributes[ $to ] = $attributes[ $from ];
}
unset( $attributes[ $from ] );
}
}
$code = preg_replace( '#<pre [^>]+>([^<]+)?</pre>#', '$1', $content );
// Undo escaping done by WordPress
$code = htmlspecialchars_decode( $code );
$code = preg_replace( '/^(\s*https?:)&#0?47;&#0?47;([^\s<>"]+\s*)$/m', '$1//$2', $code );
$code = $syntax_obj->shortcode_callback( $attributes, $code, 'code' );
return '<div class="wp-block-syntaxhighlighter-code ' . esc_attr( join( ' ', $classnames ) ) . '">' . $code . '</div>';
return $content;
}
add_filter( 'bbp_get_reply_content', 'bbtest_the_content' );
function bb_test_footer() {
?>
<script>
jQuery(document).ready(function() {
jQuery(document).on( 'click', '#forums-codeblock-button', function (e) {
e.preventDefault();
jQuery('#bbp_editor_reply_content_forums_editor_5').append( '<textarea class="block-editor-plain-text" rows="1"></textarea>' );
});
jQuery(document).on( 'keyup', '.block-editor-plain-text', function (e) {
jQuery(this).html( jQuery(this).val() );
});
});
</script>
<style>
.block-editor-plain-text {
tab-size: 4;
overflow: hidden;
overflow-wrap: break-word;
resize: none;
height: 189px;
width: 100%;
}
</style>
<?php
}
add_action( 'wp_footer', 'bb_test_footer' );
function bbtest_new_reply_pre_content( $content ) {
$search = array( '<textarea class=\"block-editor-plain-text\" rows=\"1\">', '</textarea>' );
$replace = array( '<pre class="wp-block-syntaxhighlighter-code">', '</pre>' );
$content = str_replace( $search, $replace, $content );
return $content;
}
add_filter( 'bbp_new_reply_pre_content', 'bbtest_new_reply_pre_content' );
function bbtest_after_forums_form_attachments() {
?>
<div class="post-elements-buttons-item post-codeblock codeblock-support">
<a href="#" id="forums-codeblock-button" class="toolbar-button bp-tooltip" data-bp-tooltip-pos="down-left" data-bp-tooltip="<?php esc_attr_e( 'Highlite', 'buddyboss' ); ?>">
<i class="bb-icon-l bb-icon-code"></i>
</a>
</div>
<?php
}
add_action( 'bbp_theme_after_forums_form_attachments', 'bbtest_after_forums_form_attachments' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment