mirror of
https://github.com/TryGhost/Ghost-Admin.git
synced 2023-12-14 02:33:04 +01:00
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)
This commit is contained in:
parent
faba3f59e2
commit
4370f33089
2 changed files with 29 additions and 0 deletions
|
@ -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
|
||||
|
|
27
lib/koenig-editor/addon/options/parser-plugins.js
Normal file
27
lib/koenig-editor/addon/options/parser-plugins.js
Normal file
|
@ -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,
|
||||
];
|
Loading…
Reference in a new issue