0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Autofill plaintext field on save (#8304)

refs #8275

- If the HTML field has changed, update the plaintext field
- Use html-to-text to generate a plaintext version of the HTML which retains some structure
- Add a couple of tests - although there's much to do here!
This commit is contained in:
Hannah Wolfe 2017-04-11 09:55:36 +01:00 committed by Katharina Irrgang
parent 6a7879d4f8
commit 06fc5f4508
2 changed files with 37 additions and 0 deletions

View file

@ -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());

View file

@ -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);