Last active
April 14, 2017 19:45
-
-
Save jducoeur/2ee508ca53c2f7d1bf46284f1a80c352 to your computer and use it in GitHub Desktop.
Zapier JavaScript code for parsing Dreamwidth RSS into Facebook-friendly form
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
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}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to make userheads work correctly. Needed a slightly less constrained regexp, and needs to come before we mangle links.