I'm getting a bit angry on the Markdown plugins of WordPress1: all of them will store the text as HTML, while the original goal for me would be to store in Markdown, for future readability. With Parsedown2
- a persistent WordPress Object cache3 parsing on the fly is really not an issue.
So as always, I need to come up with a solution - a hack if that sounds better - to store posts in Markdown Extra4. The nasty part is the editor, especially with media, so I decided chop it into pieces.
This is for images only, since video or audio is not supported by any of the Markdown flavours I know.
<?php
add_filter( 'post_thumbnail_html', 'rebuild_media_string', 10 );
add_filter( 'image_send_to_editor', 'rebuild_media_string', 10 );
function preg_value ( $string, $pattern, $index = 1 ) {
preg_match( $pattern, $string, $results );
if ( isset ( $results[ $index ] ) && !empty ( $results [ $index ] ) )
return $results [ $index ];
else
return false;
}
function rebuild_media_string( $str ) {
if ( strstr ( $str, '<img' ) )
$src = preg_value ( $str, '/src="([^"]+)"/' );
$title = preg_value ( $str, '/title="([^"]+)"/' );
$alt = preg_value ( $str, '/alt="([^"]+)"/' );
if ( empty ( $alt ) && !empty ( $title ) ) $alt = $title;
$wpid = preg_value ( $str, '/wp-image-(\d*)/' );
$src = preg_value ( $str, '/src="([^"]+)"/' );
$cl = preg_value ( $str, '/class="([^"]+)?(align(left|right|center))([^"]+)?"/', 2 );
$img = '!['.$alt.']('. $src .' '. $title .'){#img-'. $wpid .' .'.$cl.'}';
return $img;
}
else {
return $str;
}
}
(Oh, by the way: this entry was written by Peter Molnar, and originally posted on petermolnar dot net.)