'; foreach ( $table_rows as $i => $row ) { $tag = 0 === $i ? 'th' : 'td'; $cells = array_map( 'trim', explode( '|', trim( $row, " \t|" ) ) ); $html .= ''; foreach ( $cells as $cell ) { $html .= '<' . $tag . '>' . racn_markdown_inline( $cell ) . ''; } $html .= ''; } $html .= ''; $table_rows = array(); $in_table = false; }; foreach ( $lines as $line ) { if ( 0 === strpos( $line, '```' ) ) { if ( $in_code ) { $html .= ''; $in_code = false; } else { $html .= '
'; $in_code = true; }
			continue;
		}
		if ( $in_code ) { $html .= esc_html( $line ) . "\n"; continue; }

		if ( preg_match( '/^\s*\|.*\|\s*$/', $line ) ) {
			if ( preg_match( '/^\s*\|[\s\-|:]+\|\s*$/', $line ) ) continue; // separator
			if ( false === $in_table ) { $flush_table(); $in_table = true; }
			$table_rows[] = $line;
			continue;
		}
		if ( $in_table ) $flush_table();

		if ( preg_match( '/^(#{1,4})\s+(.+)/', $line, $m ) ) {
			$lvl = strlen( $m[1] );
			$html .= '' . esc_html( $m[2] ) . '';
			continue;
		}
		if ( preg_match( '/^---+$/', trim( $line ) ) ) { $html .= '
'; continue; } if ( '' === trim( $line ) ) continue; if ( preg_match( '/^(\s*)[-*]\s+(.+)/', $line, $m ) ) { $html .= '
  • ' . racn_markdown_inline( $m[2] ) . '
  • '; continue; } $html .= '

    ' . racn_markdown_inline( $line ) . '

    '; } if ( $in_table ) $flush_table(); if ( $in_code ) $html .= '
    '; return $html; } /** * @param string $text * @return string */ function racn_markdown_inline( $text ) { $text = esc_html( $text ); // escape first — everything below is added by us. $text = preg_replace( '/\*\*(.+?)\*\*/', '$1', $text ); $text = preg_replace( '/(?$1', $text ); $text = preg_replace( '/`([^`]+)`/', '$1', $text ); $text = preg_replace( '/\[([^\]]+)\]\(#[^)]*\)/', '$1', $text ); // strip anchor links $text = preg_replace_callback( '/\[([^\]]+)\]\((https?[^)]+)\)/', static function ( $m ) { $url = esc_url( html_entity_decode( $m[2], ENT_QUOTES ) ); return '' === $url ? $m[1] : '' . $m[1] . ''; }, $text ); return $text; }