'; $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;
}