2018-08-13 12:41:01 +03:00
|
|
|
import {cleanBasicHtml} from 'koenig-editor/helpers/clean-basic-html';
|
|
|
|
|
2018-05-17 18:46:25 +03:00
|
|
|
// mobiledoc by default ignores <BR> tags but we have a custom SoftReturn atom
|
|
|
|
export function brToSoftBreakAtom(node, builder, {addMarkerable, nodeFinished}) {
|
|
|
|
if (node.nodeType !== 1 || node.tagName !== 'BR') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let softReturn = builder.createAtom('soft-return');
|
|
|
|
addMarkerable(softReturn);
|
|
|
|
|
|
|
|
nodeFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
// leading newlines in text nodes will add a space to the beginning of the text
|
|
|
|
// which doesn't render correctly if we're replacing <br> with SoftReturn atoms
|
|
|
|
// after parsing text as markdown to html
|
|
|
|
export function removeLeadingNewline(node) {
|
|
|
|
if (node.nodeType !== 3 || node.nodeName !== '#text') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node.nodeValue = node.nodeValue.replace(/^\n/, '');
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:48:32 +03:00
|
|
|
export function figureToImageCard(node, builder, {addSection, nodeFinished}) {
|
|
|
|
if (node.nodeType !== 1 || node.tagName !== 'FIGURE') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let img = node.querySelector('img');
|
|
|
|
let figcaption = node.querySelector('figcaption');
|
|
|
|
|
|
|
|
if (!img) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-13 11:50:56 +03:00
|
|
|
let payload = {
|
|
|
|
src: img.src,
|
|
|
|
alt: img.alt,
|
|
|
|
title: img.title
|
|
|
|
};
|
2018-06-18 20:48:32 +03:00
|
|
|
|
|
|
|
if (figcaption) {
|
2018-08-13 12:41:01 +03:00
|
|
|
let cleanHtml = cleanBasicHtml(figcaption.innerHTML);
|
|
|
|
payload.caption = cleanHtml;
|
2018-06-18 20:48:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let cardSection = builder.createCardSection('image', payload);
|
|
|
|
addSection(cardSection);
|
|
|
|
nodeFinished();
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:55:46 +03:00
|
|
|
export function imgToCard(node, builder, {addSection, nodeFinished}) {
|
|
|
|
if (node.nodeType !== 1 || node.tagName !== 'IMG') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-13 11:50:56 +03:00
|
|
|
let payload = {
|
|
|
|
src: node.src,
|
|
|
|
alt: node.alt,
|
|
|
|
title: node.title
|
|
|
|
};
|
2018-05-18 16:26:26 +03:00
|
|
|
|
2018-05-18 15:55:46 +03:00
|
|
|
let cardSection = builder.createCardSection('image', payload);
|
|
|
|
addSection(cardSection);
|
|
|
|
nodeFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hrToCard(node, builder, {addSection, nodeFinished}) {
|
|
|
|
if (node.nodeType !== 1 || node.tagName !== 'HR') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cardSection = builder.createCardSection('hr');
|
|
|
|
addSection(cardSection);
|
|
|
|
nodeFinished();
|
|
|
|
}
|
|
|
|
|
2018-05-18 16:06:35 +03:00
|
|
|
export function preCodeToCard(node, builder, {addSection, nodeFinished}) {
|
|
|
|
if (node.nodeType !== 1 || node.tagName !== 'PRE') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [codeElement] = node.children;
|
|
|
|
|
|
|
|
if (codeElement && codeElement.tagName === 'CODE') {
|
|
|
|
let payload = {code: codeElement.textContent};
|
|
|
|
let cardSection = builder.createCardSection('code', payload);
|
|
|
|
addSection(cardSection);
|
|
|
|
nodeFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:46:25 +03:00
|
|
|
export default [
|
|
|
|
brToSoftBreakAtom,
|
|
|
|
removeLeadingNewline,
|
2018-06-18 20:48:32 +03:00
|
|
|
figureToImageCard,
|
2018-05-18 15:55:46 +03:00
|
|
|
imgToCard,
|
2018-05-18 16:06:35 +03:00
|
|
|
hrToCard,
|
|
|
|
preCodeToCard
|
2018-05-17 18:46:25 +03:00
|
|
|
];
|