Ghost/ghost/admin/lib/koenig-editor/addon/options/parser-plugins.js
Kevin Ansfield 7bd22a3b87 🐛 Fixed backspace deleting words and rich-text paste collapsing paragraphs
refs https://github.com/TryGhost/Ghost/issues/9623
- switch to custom `mobiledoc-kit` build
  - fixes top-level elements not being run through parser plugins (https://github.com/bustle/mobiledoc-kit/pull/627)
  - fixes <kbd>Alt</kbd> getting stuck and causing <kbd>Backspace</kbd> to delete whole words (https://github.com/bustle/mobiledoc-kit/pull/626)
  - fixes error that can occur when a paste results in blank insert (https://github.com/bustle/mobiledoc-kit/pull/620)
- add new `figureToImageCard` parser
  - replaces hacky workaround to detect an image+figcaption inside the `imgToCard` parser plugin
- remove wrapping of html in a `<div>...</div>` when pasting
  - no longer necessary now that top-level elements are parsed
  - fixes rich-text pastes where multiple paragraphs would be collapsed into a single paragraph
2018-06-18 18:48:32 +01:00

93 lines
2.4 KiB
JavaScript

// 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/, '');
}
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;
}
let payload = {src: img.src};
if (figcaption) {
// TODO: Allow rich text in captions
payload.caption = figcaption.textContent;
}
let cardSection = builder.createCardSection('image', payload);
addSection(cardSection);
nodeFinished();
}
export function imgToCard(node, builder, {addSection, nodeFinished}) {
if (node.nodeType !== 1 || node.tagName !== 'IMG') {
return;
}
let payload = {src: node.src};
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();
}
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();
}
}
export default [
brToSoftBreakAtom,
removeLeadingNewline,
figureToImageCard,
imgToCard,
hrToCard,
preCodeToCard
];