]*>(.*)#s', $html, $m ) ) { return $html; } Assets::request(); return self::markup( self::text( $m[1] ), $attrs ); } /** * Code text from the saved HTML (rich-text formatting dropped). * * @param string $html Inner HTML of . * @return string */ public static function text( $html ) { $html = preg_replace( '##i', "\n", $html ); return html_entity_decode( wp_strip_all_tags( $html ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } /** * Code block markup. * * @param string $code Code. * @param array $attrs language, fileName, highlightLines, lineNumbers, wrap, className. * @return string */ public static function markup( $code, array $attrs ) { $lang = Highlighter::language( $attrs['language'] ?? '' ); $file = trim( (string) ( $attrs['fileName'] ?? '' ) ); if ( 'mermaid' === $lang ) { // Rendered in the browser by Mermaid when diagrams are enabled. return '
' . esc_html( $code ) . '
'; } $marked = array_flip( Highlighter::lines( (string) ( $attrs['highlightLines'] ?? '' ) ) ); $numbers = ! empty( $attrs['lineNumbers'] ); $lines = explode( "\n", Highlighter::highlight( rtrim( $code, "\n" ), 'diff' === $lang ? 'text' : $lang ) ); $rows = array(); foreach ( $lines as $i => $line ) { $class = 'kdocs-line'; if ( isset( $marked[ $i + 1 ] ) ) { $class .= ' is-marked'; } if ( 'diff' === $lang ) { $first = substr( html_entity_decode( wp_strip_all_tags( $line ), ENT_QUOTES, 'UTF-8' ), 0, 1 ); $class .= '+' === $first ? ' is-add' : ( '-' === $first ? ' is-del' : '' ); } $rows[] = '' . ( '' === $line ? ' ' : $line ) . ''; } $classes = array( 'kdocs-codeblock' ); if ( $numbers ) { $classes[] = 'has-numbers'; } if ( ! empty( $attrs['wrap'] ) ) { $classes[] = 'is-wrapped'; } if ( ! empty( $attrs['className'] ) ) { $classes[] = sanitize_html_class( $attrs['className'] ); } $label = $file ? $file : ( 'text' === $lang ? '' : Highlighter::LANGUAGES[ $lang ] ); $out = '
'; $out .= '
'; $out .= '' . esc_html( $label ) . ''; $out .= ''; $out .= '
'; $out .= '
' . implode( "\n", $rows ) . '
'; return $out . '
'; } }