Created
February 14, 2017 18:24
-
-
Save Beej126/d77d6d64788aa9953d509a6aa979ec51 to your computer and use it in GitHub Desktop.
Convert WordPress Post HTML to Markdown (by Post Id)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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