mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-08 02:52:39 -05:00
Koenig - Embed card renderer
refs https://github.com/TryGhost/Ghost/issues/9623 - add `embed` card renderer
This commit is contained in:
parent
ca20f3a6b0
commit
fe8c07333d
4 changed files with 105 additions and 3 deletions
25
core/server/lib/mobiledoc/cards/embed.js
Normal file
25
core/server/lib/mobiledoc/cards/embed.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
module.exports = {
|
||||
name: 'embed',
|
||||
type: 'dom',
|
||||
render(opts) {
|
||||
if (!opts.payload.html) {
|
||||
return '';
|
||||
}
|
||||
|
||||
let {payload, env: {dom}} = opts;
|
||||
|
||||
let figure = dom.createElement('figure');
|
||||
figure.setAttribute('class', 'kg-embed-card');
|
||||
|
||||
let html = dom.createRawHTMLSection(payload.html);
|
||||
figure.appendChild(html);
|
||||
|
||||
if (payload.caption) {
|
||||
let figcaption = dom.createElement('figcaption');
|
||||
figcaption.appendChild(dom.createTextNode(payload.caption));
|
||||
figure.appendChild(figcaption);
|
||||
}
|
||||
|
||||
return figure;
|
||||
}
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
module.exports = [
|
||||
require('./card-markdown'),
|
||||
require('./code'),
|
||||
require('./embed'),
|
||||
require('./hr'),
|
||||
require('./html'),
|
||||
require('./image'),
|
||||
|
|
72
core/test/unit/lib/mobiledoc/cards/embed_spec.js
Normal file
72
core/test/unit/lib/mobiledoc/cards/embed_spec.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
const should = require('should');
|
||||
const card = require('../../../../../server/lib/mobiledoc/cards/embed');
|
||||
const SimpleDom = require('simple-dom');
|
||||
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
|
||||
|
||||
describe('Embed card', function () {
|
||||
it('Embed Card renders', function () {
|
||||
let opts = {
|
||||
env: {
|
||||
dom: new SimpleDom.Document()
|
||||
},
|
||||
payload: {
|
||||
html: '<h1>HEADING</h1><p>PARAGRAPH</p>'
|
||||
}
|
||||
};
|
||||
|
||||
serializer.serialize(card.render(opts)).should.match('<figure class="kg-embed-card"><h1>HEADING</h1><p>PARAGRAPH</p></figure>');
|
||||
});
|
||||
|
||||
it('Plain content renders', function () {
|
||||
let opts = {
|
||||
env: {
|
||||
dom: new SimpleDom.Document()
|
||||
},
|
||||
payload: {
|
||||
html: 'CONTENT'
|
||||
}
|
||||
};
|
||||
|
||||
serializer.serialize(card.render(opts)).should.match('<figure class="kg-embed-card">CONTENT</figure>');
|
||||
});
|
||||
|
||||
it('Invalid HTML returns', function () {
|
||||
let opts = {
|
||||
env: {
|
||||
dom: new SimpleDom.Document()
|
||||
},
|
||||
payload: {
|
||||
html: '<h1>HEADING<'
|
||||
}
|
||||
};
|
||||
|
||||
serializer.serialize(card.render(opts)).should.match('<figure class="kg-embed-card"><h1>HEADING<</figure>');
|
||||
});
|
||||
|
||||
it('Renders nothing when payload is undefined', function () {
|
||||
let opts = {
|
||||
env: {
|
||||
dom: new SimpleDom.Document()
|
||||
},
|
||||
payload: {
|
||||
html: undefined
|
||||
}
|
||||
};
|
||||
|
||||
serializer.serialize(card.render(opts)).should.match('');
|
||||
});
|
||||
|
||||
it('Renders caption when provided', function () {
|
||||
let opts = {
|
||||
env: {
|
||||
dom: new SimpleDom.Document()
|
||||
},
|
||||
payload: {
|
||||
html: 'Testing',
|
||||
caption: 'Caption'
|
||||
}
|
||||
};
|
||||
|
||||
serializer.serialize(card.render(opts)).should.match('<figure class="kg-embed-card">Testing<figcaption>Caption</figcaption></figure>');
|
||||
});
|
||||
});
|
|
@ -47,7 +47,10 @@ describe('Mobiledoc converter', function () {
|
|||
}],
|
||||
['html', {
|
||||
html: '<h2>HTML card</h2>\n<div><p>Some HTML</p></div>'
|
||||
}]
|
||||
}],
|
||||
['embed', {
|
||||
html: '<h2>Embed card</h2>'
|
||||
}],
|
||||
],
|
||||
markups: [],
|
||||
sections: [
|
||||
|
@ -66,11 +69,12 @@ describe('Mobiledoc converter', function () {
|
|||
[0, [], 0, 'Four']
|
||||
]],
|
||||
[10, 3],
|
||||
[1, 'p', []]
|
||||
[10, 4],
|
||||
[1, 'p', []],
|
||||
]
|
||||
};
|
||||
|
||||
converter.render(mobiledoc, 2).should.eql('<div class="kg-post">\n<p>One<br>Two</p><h1 id="markdowncard">Markdown card</h1>\n<p>Some markdown</p>\n<p>Three</p><hr><figure class="kg-image-card"><img src="/content/images/2018/04/NatGeo06.jpg" class="kg-image kg-image-wide"><figcaption>Birdies</figcaption></figure><p>Four</p><h2>HTML card</h2>\n<div><p>Some HTML</p></div>\n</div>');
|
||||
converter.render(mobiledoc, 2).should.eql('<div class="kg-post">\n<p>One<br>Two</p><h1 id="markdowncard">Markdown card</h1>\n<p>Some markdown</p>\n<p>Three</p><hr><figure class="kg-image-card"><img src="/content/images/2018/04/NatGeo06.jpg" class="kg-image kg-image-wide"><figcaption>Birdies</figcaption></figure><p>Four</p><h2>HTML card</h2>\n<div><p>Some HTML</p></div><figure class="kg-embed-card"><h2>Embed card</h2></figure>\n</div>');
|
||||
});
|
||||
|
||||
it('wraps output with a .kg-post div', function () {
|
||||
|
|
Loading…
Add table
Reference in a new issue