Skip to content

Instantly share code, notes, and snippets.

@Beej126
Created February 14, 2017 18:24
Show Gist options
  • Save Beej126/d77d6d64788aa9953d509a6aa979ec51 to your computer and use it in GitHub Desktop.
Save Beej126/d77d6d64788aa9953d509a6aa979ec51 to your computer and use it in GitHub Desktop.
Convert WordPress Post HTML to Markdown (by Post Id)
#r "MySql.Data"
#r "Html2Markdown"
#r "NPoco"
//#r "ReverseMarkdown" //this one turned <li> into "-" vs "*"
//install these libs via scriptcs -install libname
using NPoco;
public class Post {
public double id {get; private set;}
public string post_content {get; private set;}
}
var cxnString = "Server=xxx;Database=xxx;User=xxx;Password=xxx;";
var database = new Database(cxnString, DatabaseType.MySQL, MySql.Data.MySqlClient.MySqlClientFactory.Instance);
var query = $@"select
id,
post_title,
post_content,
post_content_filtered
from wp_posts as parent
where post_type = 'post'
and post_parent = 0
/*and post_content_filtered = ''*/
and id in ('{Environment.GetCommandLineArgs()[3]}')
;";
var cxn = new MySql.Data.MySqlClient.MySqlConnection(cxnString);
var savecmd = cxn.CreateCommand();
cxn.Open();
var posts = database.Fetch<Post>(query);
var converter = new Html2Markdown.Converter();
foreach(var post in posts) {
savecmd.CommandText = $"update wp_posts set post_content_filtered = '{converter.Convert(post.post_content).Replace("'", "''")}' where id = {post.id}";
savecmd.ExecuteNonQuery();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment