Skip to content

Instantly share code, notes, and snippets.

@frankmeeuwsen
Created November 14, 2021 10:59
Show Gist options
  • Save frankmeeuwsen/4d6d514539661d660285497eff53557d to your computer and use it in GitHub Desktop.
Save frankmeeuwsen/4d6d514539661d660285497eff53557d to your computer and use it in GitHub Desktop.
Markdown to WordPress
if(op.attributes.exists("type") && op.attributes.getOne("type")=="outline"){
var wp_post = opml.parse(op.getCursorOpml());
var title = wp_post.opml.head.title;
var text = "";
var imagepart="";
for (i=0;i<wp_post.opml.body.subs[0].subs.length;i++){
if(wp_post.opml.body.subs[0].subs[i].image !==undefined){
var imagepart="![]("+string.multipleReplaceAll(wp_post.opml.body.subs[0].subs[i].image,{" ":"%20"})+")";
text=text+imagepart;
}
if(wp_post.opml.body.subs[0].subs[i].inlineImage !==undefined){
var imagepart="![]("+string.multipleReplaceAll(wp_post.opml.body.subs[0].subs[i].inlineImage,{" ":"%20"})+")";
text=text+imagepart;
}
var textpart= wp_post.opml.body.subs[0].subs[i].text+"\n";
text=text+textpart;
}
//Thanks to Amit Gawande for the amazing Get Post URL script. See https://www.amitgawande.com/2021/10/23/getting-post-url.html
var created = op.attributes.getOne("created");
var baseurl = opml.getHeaders().urlBlogWebsite;
if(typeof(baseurl) == "undefined"){
baseurl = "http://oldschool.scripting.com/" + opml.getHeaders().ownerTwitterScreenName + "/";
}
var dt = new Date(created)
var day = (dt.getUTCDate() < 10 ? '0' : '') + dt.getUTCDate();
var month = ((dt.getUTCMonth() +1) < 10 ? '0' : '') + (dt.getUTCMonth() + 1);
var year = dt.getUTCFullYear();
var hour = (dt.getUTCHours() < 10 ? '0' : '') + dt.getUTCHours();
var minutes = (dt.getUTCMinutes() < 10 ? '0' : '') + dt.getUTCMinutes()
var seconds = (dt.getUTCSeconds() < 10 ? '0' : '') + dt.getUTCSeconds()
var post = opml.parse(op.getCursorOpml());
var isTitledPost = typeof(post.opml.body.subs[0].subs) != "undefined";
var postUrl = "";
if(isTitledPost) {
postUrl = baseurl + year + "/" + month + "/" + day + "/" + hour + minutes + seconds + ".html";
postUrl = postUrl + "?title=" + op.getLineText();
} else
{
postUrl = baseurl + year + "/" + month + "/" + day + ".html#a" + hour + minutes + seconds;
}
var textoutro = "\n\n\n_Published on my [Oldschool Drummer blog]("+string.multipleReplaceAll(postUrl,{" ":"%20"})+")_";
text=text+textoutro
var data =
{
"action": "create_post",
"title": title,
"content": text,
"status": "draft"
}
var url = root.env.endpoint;
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Authorization", "Basic " + btoa(root.env.basic));
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responstext);
var returndata = JSON.parse(xhr.responseText);
op.attributes.setOne("wp_postid",returndata.id)
clock.waitSeconds(2);
webBrowser.openUrl(returndata.link);
}
};
xhr.send(JSON.stringify(data));
}else{
dialog.alert("You\'re on the wrong level");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment