From abcb9da4450199a1cc4ecef277d92237a46840e0 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 17 Apr 2020 17:49:16 +0100 Subject: [PATCH] Prevent {} text replacements from receiving any formatting no issue - we don't want to allow text replacement strings to be split in half by html tags so we disallow any formats to be applied to them - in the ``'s mobiledoc editor's `didUpdatePost` hook handler we loop over all markers in the post and if they have a code markup (we use this to represent replacement strings) then we strip any other markups --- .../koenig-text-replacement-html-input.js | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/ghost/admin/lib/koenig-editor/addon/components/koenig-text-replacement-html-input.js b/ghost/admin/lib/koenig-editor/addon/components/koenig-text-replacement-html-input.js index 22110eddf4..debec1de72 100644 --- a/ghost/admin/lib/koenig-editor/addon/components/koenig-text-replacement-html-input.js +++ b/ghost/admin/lib/koenig-editor/addon/components/koenig-text-replacement-html-input.js @@ -343,7 +343,24 @@ export default Component.extend({ // - first section must be a markerable section // - if first section is a list, grab the content of the first list item didUpdatePost(postEditor) { - let {builder, editor, editor: {post}} = postEditor; + let {editor, editor: {post, range}} = postEditor; + + // remove any other formatting from code formats + let markers = []; + try { + markers = post.markersContainedByRange(post.toRange()); + } catch (e) { + // post.toRange() can fail if a list item was just removed + // TODO: mobiledoc-kit bug? + } + markers.forEach((marker) => { + let {markups} = marker; + if (markups.length > 1 && marker.hasMarkup('code')) { + markups.rejectBy('tagName', 'code').forEach((markup) => { + marker.removeMarkup(markup); + }); + } + }); // remove any non-markerable/non-list sections post.sections.forEach((section) => { @@ -355,24 +372,6 @@ export default Component.extend({ } } }); - - // strip all sections other than the first - // if (post.sections.length > 1) { - // while (post.sections.length > 1) { - // postEditor.removeSection(post.sections.tail); - // } - // postEditor.setRange(post.sections.head.tailPosition()); - // } - - // convert list section to a paragraph section - if (post.sections.head.isListSection) { - let list = post.sections.head; - let listItem = list.items.head; - let newMarkers = listItem.markers.map(m => m.clone()); - let p = builder.createMarkupSection('p', newMarkers); - postEditor.replaceSection(list, p); - postEditor.setRange(post.sections.head.tailPosition()); - } }, postDidChange() {