5

I'm in the process of migrating my blog from Drupal to Wordpress. I have made liberal use of Markdown in the original posts (and Geshi syntax highlighting).

In see that the WP-Markdown plugin converts to (and from HTML) on save (and edit), so I now have a bunch of posts which appear with the Markdown 'on show'.

Is there an API call which I can use to effectively do an 'edit and save' operation on each post to force the Markdown on Save plugin to operate on them all?

Or alternatively, a different Markdown plugin I should try?

2 Answers 2

7
+100

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.

1
  • In the end, just commenting out the filter lines was sufficient - going through the posts didn't take long and allowed me to fix some tagging errors in the migration Oct 1, 2012 at 19:22
0

The easiest way to migrate a Markdown database from another CMS to WordPress would just be to install Robin Adrianse's Parsedown for WordPress. In this case, there's no need to convert anything. Your posts are ready to go as you originally wrote them, including Markdown footnotes if you used them (Parsedown for WordPress includes the very useful Markdown Extra extensions from Michel Fortrin, most of which made it to Github flavoured Markdown).

If you wish to use Markdown as your primary markup language in WordPress, it's essential to enable Classic Editor to disable Gutenberg. I'd highly advise making to edit only in the plain text editor as it avoids wpautop line break parsing and radically improves performance (plain text text field). This combination works a treat for me on any WordPress from 4.9 to 5.8.

All posts are parsed as Markdown (HTML is still parsed as HTML is part of Markdown spec) but for comments it works fine. Either Markdown or HTML comments are supported, transparently. Nothing is permanently converted. Whatever you type in is converted to HTML on display. If you are using a caching plugin (pretty much obligatory with WordPress), there are no performance issues as the parsed Markdown is cached as HTML whenever the page is cached.

I specify Robin Adrianse's version of Parsedown as there are several other versions which go too far, converting HTML to Markdown permanently. Parsedown for WordPress works fine with WordPress 5.8 and Classic Editor so ignore any version warnings. Robin Adrianse no longer uses WordPress so cannot be bothered keeping up with the never-ending version number changing (real hassle for developers who are mostly volunteers providing open source code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.