Skip to content

Commit

Permalink
Removed ghost editor dependency (#8137)
Browse files Browse the repository at this point in the history
refs #7429
- ☢️ 👷🏻‍♀️ This PR removes the dependency on Ghost-Editor and replaces it with the Mobiledoc DOM renderer. It includes new DOM based default cards and atoms.
  • Loading branch information
disordinary authored and kevinansfield committed Mar 14, 2017
1 parent 8382be1 commit f61aa66
Show file tree
Hide file tree
Showing 20 changed files with 295 additions and 726 deletions.
3 changes: 3 additions & 0 deletions core/server/apps/default-cards/atoms/index.js
@@ -0,0 +1,3 @@
var softReturn = require('./soft-return');

module.exports = [softReturn];
7 changes: 7 additions & 0 deletions core/server/apps/default-cards/atoms/soft-return.js
@@ -0,0 +1,7 @@
module.exports = {
name: 'soft-return',
type: 'dom',
render(opts) {
return opts.env.dom.createElement('br');
}
};
7 changes: 7 additions & 0 deletions core/server/apps/default-cards/cards/hr.js
@@ -0,0 +1,7 @@
module.exports = {
name: 'hr-card',
type: 'dom',
render(opts) {
return opts.env.dom.createElement('hr');
}
};
12 changes: 12 additions & 0 deletions core/server/apps/default-cards/cards/html.js
@@ -0,0 +1,12 @@
var SimpleDom = require('simple-dom'),
tokenizer = require('simple-html-tokenizer').tokenize,
parser;

module.exports = {
name: 'html-card',
type: 'dom',
render(opts) {
parser = new SimpleDom.HTMLParser(tokenizer, opts.env.dom, SimpleDom.voidMap);
return parser.parse('<div>' + opts.payload.html + '</div>');
}
};
9 changes: 9 additions & 0 deletions core/server/apps/default-cards/cards/image.js
@@ -0,0 +1,9 @@
module.exports = {
name: 'image-card',
type: 'dom',
render(opts) {
var img = opts.env.dom.createElement('img');
img.setAttribute('src', opts.payload.img);
return img;
}
};
6 changes: 6 additions & 0 deletions core/server/apps/default-cards/cards/index.js
@@ -0,0 +1,6 @@
var hr = require('./hr'),
html = require('./html'),
image = require('./image'),
markdown = require('./markdown');

module.exports = [hr, html, image, markdown];
14 changes: 14 additions & 0 deletions core/server/apps/default-cards/cards/markdown.js
@@ -0,0 +1,14 @@
var SimpleDom = require('simple-dom'),
tokenizer = require('simple-html-tokenizer').tokenize,
Showdown = require('showdown-ghost'),
converter = new Showdown.converter({extensions: ['ghostgfm', 'footnotes', 'highlight']}),
parser;

module.exports = {
name: 'markdown-card',
type: 'dom',
render(opts) {
parser = new SimpleDom.HTMLParser(tokenizer, opts.env.dom, SimpleDom.voidMap);
return parser.parse('<div>' + converter.makeHtml(opts.payload.markdown || '') + '</div>');
}
};
9 changes: 9 additions & 0 deletions core/server/apps/default-cards/index.js
@@ -0,0 +1,9 @@
var cards = require('./cards'),
atoms = require('./atoms');
module.exports = {
cards: cards,
atoms: atoms,
activate: function () {
// needed by ghost
}
};
18 changes: 18 additions & 0 deletions core/server/apps/default-cards/tests/hr_spec.js
@@ -0,0 +1,18 @@
var should = require('should'),
card = require('../cards/hr'),
SimpleDom = require('simple-dom'),
opts;

should = should;
describe('HR card', function () {
it('generates a horizontal rule', function () {
opts = {
env: {
dom: new SimpleDom.Document()
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<hr></hr>');
});
});
47 changes: 47 additions & 0 deletions core/server/apps/default-cards/tests/html_spec.js
@@ -0,0 +1,47 @@
var should = require('should'),
card = require('../cards/html'),
SimpleDom = require('simple-dom'),
opts;

should = should;
describe('HTML card', function () {
it('HTML Card renders', function () {
opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
html: '<h1>HEADING</h1><p>PARAGRAPH</p>'
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<div><h1>HEADING</h1><p>PARAGRAPH</p></div>');
});
it('Plain content renders', function () {
opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
html: 'CONTENT'
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<div>CONTENT</div>');
});
it.skip('Invalid HTML returns', function () {
opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
html: '<h1>HEADING<'
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<div><h1>HEADING<</div>');
});
});
21 changes: 21 additions & 0 deletions core/server/apps/default-cards/tests/image_spec.js
@@ -0,0 +1,21 @@
var should = require('should'),
card = require('../cards/image'),
SimpleDom = require('simple-dom'),
opts;

should = should;
describe('Image card', function () {
it('generates an image', function () {
opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
img: 'https://www.ghost.org/image.png'
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<img src="https://www.ghost.org/image.png"></img>');
});
});
21 changes: 21 additions & 0 deletions core/server/apps/default-cards/tests/markdown_spec.js
@@ -0,0 +1,21 @@
var should = require('should'),
card = require('../cards/markdown'),
SimpleDom = require('simple-dom'),
opts;

should = should;
describe('Markdown card', function () {
it('Markdown Card renders', function () {
opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n- list\r\n- items'
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<div><h1 id="heading">HEADING</h1>\n\n<ul>\n<li>list</li>\n<li>items</li>\n</ul></div>');
});
});
18 changes: 18 additions & 0 deletions core/server/apps/default-cards/tests/soft-return_spec.js
@@ -0,0 +1,18 @@
var should = require('should'),
card = require('../atoms/soft-return'),
SimpleDom = require('simple-dom'),
opts;

should = should;
describe('Soft return card', function () {
it('generates a `br` tag', function () {
opts = {
env: {
dom: new SimpleDom.Document()
}
};

var serializer = new SimpleDom.HTMLSerializer([]);
serializer.serialize(card.render(opts)).should.match('<br></br>');
});
});
3 changes: 2 additions & 1 deletion core/server/config/overrides.json
Expand Up @@ -14,7 +14,8 @@
"internalApps": [
"private-blogging",
"subscribers",
"amp"
"amp",
"default-cards"
],
"routeKeywords": {
"tag": "tag",
Expand Down
5 changes: 1 addition & 4 deletions core/server/models/post.js
Expand Up @@ -7,9 +7,6 @@ var _ = require('lodash'),
errors = require('../errors'),
Showdown = require('showdown-ghost'),
legacyConverter = new Showdown.converter({extensions: ['ghostgfm', 'footnotes', 'highlight']}),
Mobiledoc = require('mobiledoc-html-renderer').default,
mobileDocOptions = require('ghost-editor').htmlOptions,
converter = new Mobiledoc(mobileDocOptions),
ghostBookshelf = require('./base'),
events = require('../events'),
config = require('../config'),
Expand Down Expand Up @@ -204,7 +201,7 @@ Post = ghostBookshelf.Model.extend({
ghostBookshelf.Model.prototype.onSaving.call(this, model, attr, options);

if (mobiledoc) {
this.set('html', converter.render(JSON.parse(mobiledoc)).result);
this.set('html', utils.mobiledocConverter.render(JSON.parse(mobiledoc)));
} else {
// legacy showdown mode
this.set('html', legacyConverter.makeHtml(_.toString(this.get('markdown'))));
Expand Down
3 changes: 2 additions & 1 deletion core/server/utils/index.js
Expand Up @@ -111,7 +111,8 @@ utils = {
url: require('./url'),
tokens: require('./tokens'),
sequence: require('./sequence'),
ghostVersion: require('./ghost-version')
ghostVersion: require('./ghost-version'),
mobiledocConverter: require('./mobiledoc-converter')
};

module.exports = utils;
40 changes: 40 additions & 0 deletions core/server/utils/mobiledoc-converter.js
@@ -0,0 +1,40 @@
var SimpleDom = require('simple-dom'),
Renderer = require('mobiledoc-dom-renderer').default,
config = require('../config'),
defaults = require(config.get('paths').internalAppPath + 'default-cards'),
options = {
dom: new SimpleDom.Document(),
cards: defaults.cards,
atoms: defaults.atoms
};

// function getCards() {
// return config.get('internalApps').reduce(
// function (cards, appName) {
// var app = require(path.join(config.get('paths').internalAppPath, appName));
// if (app.hasOwnProperty('cards')) {
// cards = cards.concat(app.cards);
// }
// return cards;
// }, [ ]);
// }
// function getAtoms() {
// return config.get('internalApps').reduce(
// function (atoms, appName) {
// var app = require(path.join(config.get('paths').internalAppPath, appName));
// if (app.hasOwnProperty('atoms')) {
// atoms = atoms.concat(app.atoms);
// }
// return atoms;
// }, [ ]);
// }

module.exports = {
render: function (mobiledoc) {
var renderer = new Renderer(options),
rendered = renderer.render(mobiledoc),
serializer = new SimpleDom.HTMLSerializer([]),
html = serializer.serializeChildren(rendered.result);
return html;
}
};
31 changes: 31 additions & 0 deletions core/test/unit/utils/mobiledoc-converter_spec.js
@@ -0,0 +1,31 @@
var converter = require('../../../server/utils/mobiledoc-converter');

describe('Convert mobiledoc to HTML ', function () {
var mobiledoc = {
version: '0.3.1',
atoms: [],
cards: [
['markdown-card',
{
pos: 'top',
card_name: 'markdown-card',
markdown: '#heading\n\n- list one\n- list two\n- list three'
}
],
['markdown-card', {
pos: 'top'
}]
],
markups: [],
sections: [
[1, 'p', [
[0, [], 0, 'test']
]],
[10, 0],
[10, 1]
]
};
it('Converts a mobiledoc to HTML', function () {
converter.render(mobiledoc).should.match('<p>test</p><div><h1 id="heading">heading</h1>\n\n<ul>\n<li>list one</li>\n<li>list two</li>\n<li>list three</li>\n</ul></div><div></div>');
});
});
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -47,7 +47,6 @@
"express-hbs": "1.0.4",
"extract-zip-fork": "1.5.1",
"fs-extra": "2.0.0",
"ghost-editor": "0.1.10",
"ghost-gql": "0.0.6",
"ghost-ignition": "2.8.9",
"glob": "5.0.15",
Expand All @@ -61,7 +60,7 @@
"knex": "0.12.7",
"knex-migrator": "2.0.13",
"lodash": "4.17.4",
"mobiledoc-html-renderer": "0.3.1",
"mobiledoc-dom-renderer": "0.6.5",
"moment": "2.17.1",
"moment-timezone": "0.5.9",
"multer": "1.3.0",
Expand All @@ -79,6 +78,8 @@
"sanitize-html": "1.14.1",
"semver": "5.3.0",
"showdown-ghost": "0.3.6",
"simple-dom": "0.3.2",
"simple-html-tokenizer": "0.4.0",
"superagent": "3.5.0",
"unidecode": "0.1.8",
"uuid": "3.0.0",
Expand Down

0 comments on commit f61aa66

Please sign in to comment.