Migrating Markdown (from Drupal)

Note: The following is largely untested (It worked after testing on one post).

As you’ve pointed out when you edit a post, the plug-in takes the content in the database (HTML) and converts that to MarkDown for editing. Since in your case the content is actually not in HTML, but already in MarkDown, we want to stop this happening. Then by updating the post, the plug-in should convert the MarkDown to HTML.

This should be fairly easy to do since this parsing is just hooked onto some filters, you simply remove the appropriate callbacks to prevent the plug-in from doing anything. (Then again, given the class structure of the plug-in, maybe its not quite so easy). Given that, you may just wish to manually edit the plug-in files to remove the filters.

The filters in question are added here.

add_filter( 'edit_post_content', array( $this, 'wpautop' ), 10, 2 );
add_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10, 2 );

Removing those (manually or otherwise) and then updating each of the posts should work. But that could take a while, so lets go for an automated solution…

   function wpse65948_correct_markdown(){

        //Only run script if ?markdown=correct is set
        if( empty($_GET['markdown']) || 'correct' !== $_GET['markdown'] )
             return;

        if( !current_user_can('publish_posts') )
             return;

        //Do a query to get all posts that are expected to contain markdown
        //Typically will be decided by post type. Set post_status to 'any'.
        $markdowns = get_posts( array(
             'fields'=>'ids',
             'post_type'=>'post',
             'post_status'=>'any',
         ) );

         //If no posts found abort.
         if( !$markdowns )
            return;

         /** !Important 
          *  At this point the filters should be removed. Either remove them now
          *  or ensure they have been manually removed prior to triggering this script.
          */
         foreach ($markdowns as $pid ){

             // Get the content for editing
             $markdown = get_post_to_edit( $pid );

             //$markdown->post_content should contain the correct MarkDown
             $update_post = array();
             $update_post ['ID'] = $pid;
             $update_post ['post_content'] = $markdown->post_content;

             //Update the post into the database
             wp_update_post( $update_post);
          }
   }

   add_action('admin_init','wpse65948_correct_markdown',20);

You should test this script with one of your posts first to check that it works before running for the rest.

The script can be added to the very bottom of the wp-markdown.php file or functions.php. Remove again after use. To trigger the script just append ?markdown=correct to an admin url.

If you manually remove the filters before running this script, make sure you replace them again.

Leave a Comment