Skip to content

Instantly share code, notes, and snippets.

@joshisa
Forked from glennblock/posts.js
Last active September 24, 2016 10:48
Show Gist options
  • Save joshisa/accfd1e644f71809f46c to your computer and use it in GitHub Desktop.
Save joshisa/accfd1e644f71809f46c to your computer and use it in GitHub Desktop.
Embedded 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.
//SPJ: Updated for compatibility with Ghost 0.6.4
/**
* ### Read
* Find a post, by ID, UUID, or Slug
*
* @public
* @param {{id_or_slug (required), context, status, include, ...}} options
* @return {Promise(Post)} Post
*/
read: function read(options) {
var attrs = ['id', 'slug', 'status', 'uuid'],
data = _.pick(options, attrs);
options = _.omit(options, attrs);
// only published posts if no user is present
if (!data.uuid && !(options.context && options.context.user)) {
data.status = 'published';
}
if (options.include) {
options.include = prepareInclude(options.include);
}
return dataProvider.Post.findOne(data, options).then(function (result) {
var omitted;
if (result) {
omitted = result.toJSON(options);
omitted.author = _.omit(omitted.author);
omitted.user = _.omit(omitted.user);
//GB: replace the gist tag with the embedded gist
omitted.html = omitted.html.replace(/\[gist id=([a-z0-9]+)\]/g, "<script src=\"https://gist.github.com/$1.js\"></script>");
return {posts: [omitted]};
}
return Promise.reject(new errors.NotFoundError('Post not found.'));
});
},
@joshisa
Copy link
Author

joshisa commented Aug 25, 2015

change is required since it seems that gists now have id's that are alphanumeric. Therefore, the regex needs to be changed from [0-9] to [a-z0-9] for both references. Also, to allow more than one gist embed, I needed to add the global modifier /g to the regex expression. Demo found @ https://blueghostrox.mybluemix.net/autoarchive-testing-underway/

@joshisa
Copy link
Author

joshisa commented Aug 25, 2015

This hack mod works with Ghost 0.6.4

@joshisa
Copy link
Author

joshisa commented Dec 8, 2015

Gisto comment added

@joshisa
Copy link
Author

joshisa commented Dec 8, 2015

One more BluemixGisto comment

@joshisa
Copy link
Author

joshisa commented Dec 8, 2015

with pain is gain

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