0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/lib/mobiledoc/cards/image.js
Kevin Ansfield 4f9e687f62 Updated mobiledoc renderer to insert begin/end HTML comments around cards
no issue

We're creating tooling to convert HTML to Ghost flavoured mobiledoc, however we have cards that allow arbitrary content without a wrapper element which means that we're unable to do a 1:1 mapping of mobiledoc->html->mobiledoc. To work around this problem we now output HTML comments before/after the output of each card so that our converter can extract card content correctly when parsing HTML.

- added `createCard` method which wraps a card's `render()` method to add begin/end comments and updated all cards to use it
- only takes affect for newly added or re-saved posts/pages
2019-02-25 18:05:01 +07:00

43 lines
1.3 KiB
JavaScript

const createCard = require('../create-card');
module.exports = createCard({
name: 'image',
type: 'dom',
render(opts) {
let payload = opts.payload;
// let version = opts.options.version;
let dom = opts.env.dom;
if (!payload.src) {
return '';
}
let figure = dom.createElement('figure');
let figureClass = 'kg-card kg-image-card';
if (payload.cardWidth) {
figureClass = `${figureClass} kg-width-${payload.cardWidth}`;
}
figure.setAttribute('class', figureClass);
let img = dom.createElement('img');
img.setAttribute('src', payload.src);
img.setAttribute('class', 'kg-image');
if (payload.alt) {
img.setAttribute('alt', payload.alt);
}
if (payload.title) {
img.setAttribute('title', payload.title);
}
figure.appendChild(img);
if (payload.caption) {
let figcaption = dom.createElement('figcaption');
figcaption.appendChild(dom.createRawHTMLSection(payload.caption));
figure.appendChild(figcaption);
figure.setAttribute('class', `${figure.getAttribute('class')} kg-card-hascaption`);
}
return figure;
}
});