Skip to content

Instantly share code, notes, and snippets.

@rizowski
Forked from glennblock/posts.js
Created June 27, 2014 15:49
Show Gist options
  • Save rizowski/a857dfe8ae79c3eaa046 to your computer and use it in GitHub Desktop.
Save rizowski/a857dfe8ae79c3eaa046 to your computer and use it in GitHub Desktop.
//the code below will embed replace [gist id=x] tags with an embedded gist similar to the way the Wordpress Gist
//plugin works. I wrote this in order to import posts from Wordpress. See //GB: for the changes.
// **takes:** filter / pagination parameters
browse: function browse(options) {
options = options || {};
// **returns:** a promise for a page of posts in a json object
//return dataProvider.Post.findPage(options);
return dataProvider.Post.findPage(options).then(function (result) {
var i = 0,
omitted = result, html;
for (i = 0; i < omitted.posts.length; i = i + 1) {
omitted.posts[i].author = _.omit(omitted.posts[i].author, filteredUserAttributes);
omitted.posts[i].user = _.omit(omitted.posts[i].user, filteredUserAttributes);
html = omitted.posts[i].html;
//GB: remove any gist id from the preview in case they are in the first para
omitted.posts[i].html = html.replace(/\[gist id=([0-9]+)\]/, "");
}
return omitted;
});
},
// #### Read
// **takes:** an identifier (id or slug?)
read: function read(args) {
// **returns:** a promise for a single post in a json object
return dataProvider.Post.findOne(args).then(function (result) {
var omitted;
if (result) {
omitted = result.toJSON();
omitted.author = _.omit(omitted.author, filteredUserAttributes);
omitted.user = _.omit(omitted.user, filteredUserAttributes);
//GB: replace the gist tag with the embedded gist
omitted.html = omitted.html.replace(/\[gist id=([0-9]+)\]/, "<script src=\"https://gist.github.com/$1.js\"></script>");
return omitted;
}
return when.reject({errorCode: 404, message: 'Post not found'});
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment