From 4370f3308923c90ad09a1d0cacdc41358a645c4c Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Thu, 17 May 2018 16:46:25 +0100 Subject: [PATCH] Koenig - Convert
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 `
` 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) --- .../addon/components/koenig-editor.js | 2 ++ .../addon/options/parser-plugins.js | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 lib/koenig-editor/addon/options/parser-plugins.js diff --git a/lib/koenig-editor/addon/components/koenig-editor.js b/lib/koenig-editor/addon/components/koenig-editor.js index 5026ebb08..6692bf417 100644 --- a/lib/koenig-editor/addon/components/koenig-editor.js +++ b/lib/koenig-editor/addon/components/koenig-editor.js @@ -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'; @@ -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 diff --git a/lib/koenig-editor/addon/options/parser-plugins.js b/lib/koenig-editor/addon/options/parser-plugins.js new file mode 100644 index 000000000..1ac0b71a2 --- /dev/null +++ b/lib/koenig-editor/addon/options/parser-plugins.js @@ -0,0 +1,27 @@ +// mobiledoc by default ignores
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
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, +];