Ghost/ghost/admin/lib/koenig-editor/addon/options/parser-plugins.js
Kevin Ansfield 0fa0068f6a Koenig - Convert <br>s to soft-break atoms on paste
refs https://github.com/TryGhost/Ghost/issues/9623
- adds `parserPlugins` option with array of parser plugins that read node values and convert them when pasting
- converts `<br>` to a soft-break atom for line breaks
- removes leading newlines from text nodes to avoid leading spaces in the render output (common when pasting MD with line breaks)
2018-05-17 16:46:25 +01:00

28 lines
809 B
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 default [
brToSoftBreakAtom,
removeLeadingNewline,
];