Skip to content

Instantly share code, notes, and snippets.

@glennblock
Last active July 9, 2020 14:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save glennblock/8662218 to your computer and use it in GitHub Desktop.
Save glennblock/8662218 to your computer and use it in GitHub Desktop.
Embeded gist syntax for Ghost. Place in ./core/server/api/posts.js.
//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'});
});
},
@joshisa
Copy link

joshisa commented May 19, 2014

This works great. Just a FYI that gist id's now seem to have gone from numeric to alphanumeric ... so the regex's that I've found useful are [a-z0-9] rather than [0-9]. Thanks for sharing. This is really useful for sharing code snips in certain blog posts.

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