Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Koenig - Convert <br>s to soft-break atoms on paste
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#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)
  • Loading branch information
kevinansfield committed May 17, 2018
1 parent faba3f5 commit 4370f33
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/koenig-editor/addon/components/koenig-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import defaultAtoms from '../options/atoms';
import defaultCards from '../options/cards';
import formatMarkdown from 'ghost-admin/utils/format-markdown';
import layout from '../templates/components/koenig-editor';
import parserPlugins from '../options/parser-plugins';
import registerKeyCommands from '../options/key-commands';
import registerTextExpansions from '../options/text-expansions';
import validator from 'npm:validator';
Expand Down Expand Up @@ -225,6 +226,7 @@ export default Component.extend({
editorOptions.mobiledoc = mobiledoc;
editorOptions.showLinkTooltips = false;
editorOptions.undoDepth = UNDO_DEPTH;
editorOptions.parserPlugins = parserPlugins;

let componentHooks = {
// triggered when a card section is added to the mobiledoc
Expand Down
27 changes: 27 additions & 0 deletions lib/koenig-editor/addon/options/parser-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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,
];

0 comments on commit 4370f33

Please sign in to comment.