Skip to content

Instantly share code, notes, and snippets.

@jducoeur
Created July 17, 2017 17:37
Show Gist options
  • Save jducoeur/23b2b6718fcefaad38c1485339c5e15b to your computer and use it in GitHub Desktop.
Save jducoeur/23b2b6718fcefaad38c1485339c5e15b to your computer and use it in GitHub Desktop.
Filter code for IFTTT script to translate Dreamwidth RSS to Facebook
var initialHtml = Feed.newFeedItem.EntryContent;
var crlf = "<br>&nbsp;<br>";
var crlf2 = "<br>&nbsp;<br>&nbsp;<br>";
// 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' + crlf],
['<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" + crlf],
// 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 =
"== " + Feed.newFeedItem.EntryTitle + " ==" + crlf +
finalText + crlf +
"Original post: " + Feed.newFeedItem.EntryUrl;
Facebook.createStatusMessage.setMessage(textToPost);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment