diff --git a/core/server/models/post.js b/core/server/models/post.js index 207dc0c201..6bc63be264 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -7,6 +7,7 @@ var _ = require('lodash'), errors = require('../errors'), Showdown = require('showdown-ghost'), legacyConverter = new Showdown.converter({extensions: ['ghostgfm', 'footnotes', 'highlight']}), + htmlToText = require('html-to-text'), ghostBookshelf = require('./base'), events = require('../events'), config = require('../config'), @@ -207,6 +208,18 @@ Post = ghostBookshelf.Model.extend({ this.set('html', legacyConverter.makeHtml(_.toString(this.get('markdown')))); } + if (this.hasChanged('html')) { + this.set('plaintext', htmlToText.fromString(this.get('html'), { + wordwrap: 80, + ignoreImage: true, + linkHrefBaseUrl: utils.url.urlFor('home').replace(/\/$/, ''), + hideLinkHrefIfSameAsText: true, + preserveNewlines: true, + returnDomByDefault: true, + uppercaseHeadings: false + })); + } + // disabling sanitization until we can implement a better version title = this.get('title') || i18n.t('errors.models.post.untitled'); this.set('title', _.toString(title).trim()); diff --git a/core/test/integration/model/model_posts_spec.js b/core/test/integration/model/model_posts_spec.js index 2f47562450..1d404b2f86 100644 --- a/core/test/integration/model/model_posts_spec.js +++ b/core/test/integration/model/model_posts_spec.js @@ -59,6 +59,11 @@ describe('Post Model', function () { firstPost.updated_by.name.should.equal(DataGenerator.Content.users[0].name); firstPost.published_by.name.should.equal(DataGenerator.Content.users[0].name); firstPost.tags[0].name.should.equal(DataGenerator.Content.tags[0].name); + + // Formats + // @TODO change / update this for mobiledoc in + firstPost.markdown.should.match(/HTML Ipsum Presents/); + firstPost.html.should.match(/HTML Ipsum Presents/); } describe('findAll', function () { @@ -425,6 +430,23 @@ describe('Post Model', function () { }).catch(done); }); + it('converts html to plaintext', function (done) { + var postId = testUtils.DataGenerator.Content.posts[0].id; + + PostModel.findOne({id: postId}).then(function (results) { + should.exist(results); + results.attributes.html.should.match(/HTML Ipsum Presents/); + should.not.exist(results.attributes.plaintext); + return PostModel.edit({updated_at: Date.now()}, _.extend({}, context, {id: postId})); + }).then(function (edited) { + should.exist(edited); + + edited.attributes.html.should.match(/HTML Ipsum Presents/); + edited.attributes.plaintext.should.match(/HTML Ipsum Presents/); + done(); + }).catch(done); + }); + it('can publish draft post', function (done) { var postId = testUtils.DataGenerator.Content.posts[3].id; @@ -905,6 +927,8 @@ describe('Post Model', function () { createdPost.get('markdown').should.equal(newPost.markdown, 'markdown is correct'); createdPost.has('html').should.equal(true); createdPost.get('html').should.equal(newPostDB.html); + createdPost.has('plaintext').should.equal(true); + createdPost.get('plaintext').should.match(/^testing/); createdPost.get('slug').should.equal(newPostDB.slug + '-2'); (!!createdPost.get('featured')).should.equal(false); (!!createdPost.get('page')).should.equal(false);