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-05-18 15:55:46 +03:00
|
|
|
export function imgToCard(node, builder, {addSection, nodeFinished}) {
|
|
|
|
if (node.nodeType !== 1 || node.tagName !== 'IMG') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let payload = {src: node.src};
|
2018-05-18 16:26:26 +03:00
|
|
|
|
|
|
|
// TODO: this is a workaround for grabbing a figure>img+figcaption until
|
|
|
|
// https://github.com/bustle/mobiledoc-kit/issues/494 is resolved
|
|
|
|
// NOTE: it will only work in a strict <figure><img><figcaption></figure> case
|
|
|
|
let nextSibling = node.nextSibling;
|
|
|
|
if (nextSibling && nextSibling.tagName === 'FIGCAPTION') {
|
|
|
|
payload.caption = nextSibling.textContent;
|
|
|
|
node.parentNode.removeChild(nextSibling);
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
];
|