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/code.js
Kevin Ansfield fa4e68ba13 Added transformer methods to mobiledoc cards
no issue

- adds abolsute->relative and relative->absolute transformer methods to card definitions
- allows for each card to tailor it's transformation to the specific needs of it's payload so that the `mobiledoc` field can be transformed successfully during API serialization/deserialization
2019-10-07 22:59:19 +01:00

48 lines
1.4 KiB
JavaScript

const createCard = require('../create-card');
module.exports = createCard({
name: 'code',
type: 'dom',
render(opts) {
let payload = opts.payload;
let dom = opts.env.dom;
if (!payload.code) {
return '';
}
let pre = dom.createElement('pre');
let code = dom.createElement('code');
if (payload.language) {
code.setAttribute('class', `language-${payload.language}`);
}
code.appendChild(dom.createTextNode(payload.code));
pre.appendChild(code);
if (payload.caption) {
let figure = dom.createElement('figure');
figure.setAttribute('class', 'kg-card kg-code-card');
figure.appendChild(pre);
let figcaption = dom.createElement('figcaption');
figcaption.appendChild(dom.createRawHTMLSection(payload.caption));
figure.appendChild(figcaption);
return figure;
} else {
return pre;
}
},
absoluteToRelative(urlUtils, payload, options) {
payload.caption = payload.caption && urlUtils.htmlAbsoluteToRelative(payload.caption, options);
return payload;
},
relativeToAbsolute(urlUtils, payload, options) {
payload.caption = payload.caption && urlUtils.htmlRelativeToAbsolute(payload.caption, options);
return payload;
}
});