Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created December 14, 2013 16:44
Show Gist options
  • Save yitsushi/7961607 to your computer and use it in GitHub Desktop.
Save yitsushi/7961607 to your computer and use it in GitHub Desktop.
First image as cover image in Ghost
diff --git a/core/server/models/post.js b/core/server/models/post.js
index b471f51..5e3c680 100644
--- a/core/server/models/post.js
+++ b/core/server/models/post.js
@@ -44,13 +44,19 @@ Post = ghostBookshelf.Model.extend({
saving: function (newPage, attr, options) {
/*jslint unparam:true*/
- var self = this;
+ var self = this,
+ imageMatch;
// Remove any properties which don't belong on the post model
this.attributes = this.pick(this.permittedAttributes);
this.set('html', converter.makeHtml(this.get('markdown')));
+ imageMatch = this.get('markdown').match(/!\[[^\]]+\]\(([^\)]+)\)/);
+ if (imageMatch) {
+ this.set('image', imageMatch[1]);
+ }
+
this.set('title', this.sanitize('title').trim());
if (this.hasChanged('status') && this.get('status') === 'published') {
@yitsushi
Copy link
Author

I made it for social sharing:

default.hbs:

{{#post}}
<meta property="og:title" content="{{title}}" />
{{#if image}}<meta property="og:image" content="{{@blog.url}}{{image}}" />{{/if}}
<meta property="og:description" content="{{excerpt characters=220}}..." />
{{/post}}

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