Skip to content

Instantly share code, notes, and snippets.

@jducoeur
Last active April 14, 2017 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jducoeur/2ee508ca53c2f7d1bf46284f1a80c352 to your computer and use it in GitHub Desktop.
Save jducoeur/2ee508ca53c2f7d1bf46284f1a80c352 to your computer and use it in GitHub Desktop.
Zapier JavaScript code for parsing Dreamwidth RSS into Facebook-friendly form
var initialHtml = inputData.postHtml;
// These are the RegExp+replacement transformations we are going to do on the HTML:
var transforms = [
// A DW userhead:
["<span style='white-space: nowrap;'><a href='(?:.*?)'><img src='(?:.*?)' alt='(?:.*?)' width='17' height='17' style='(?:.*?)' /></a><a href='https://(?:.*?).dreamwidth.org/'><b>(.*?)</b></a></span>", "@$1"],
["<p>(.*?)</p>", "$1"],
['<a href="(.*?)">(.*?)</a>', "$2 ($1)"],
['<em>(.*?)</em>', "*$1*"],
['<strong>(.*?)</strong>', "**$1**"],
// We simply strip <ul>, and turn <li> into bullets:
['<ul>(.*?)</ul>', "$1"],
['<li></li>', "* $1"],
// The dynamic comment-viewing thingy at the bottom doesn't work in Facebook, so just strip it:
['<br /><br /><img (?:.*?)/> comments', ""]
];
// Do the transformations. This is ugly and mutable, but we're stuck with vanilla JS:
var curText = initialHtml;
var curExp = new RegExp("");
transforms.forEach(function doOne(transform) {
curExp = new RegExp(transform[0], "gi");
curText = curText.replace(curExp, transform[1]);
});
var finalText = curText;
var textToPost =
"== " + inputData.postTitle + " ==\n\n" +
finalText + "\n" +
"Tags: " + inputData.postTags + "\n" +
"Original post:";
output = [{textToPost: textToPost}];
@jducoeur
Copy link
Author

Updated to make userheads work correctly. Needed a slightly less constrained regexp, and needs to come before we mangle links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment