From cd111f3a3d290babb3902201626b3adc2e3a60ec Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Sun, 3 Sep 2017 11:36:30 -0400 Subject: [PATCH] distinguish between newline and soft break in editor --- .../Markdown/serializers/remarkSlate.js | 2 +- .../Markdown/serializers/slateRemark.js | 27 +++++-------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/components/Widgets/Markdown/serializers/remarkSlate.js b/src/components/Widgets/Markdown/serializers/remarkSlate.js index f8cc42f0..4e3a8236 100644 --- a/src/components/Widgets/Markdown/serializers/remarkSlate.js +++ b/src/components/Widgets/Markdown/serializers/remarkSlate.js @@ -239,7 +239,7 @@ function convertNode(node, nodes) { * line breaks within a text node. */ case 'break': { - return createText('\n'); + return createText('\n', { isBreak: true }); } /** diff --git a/src/components/Widgets/Markdown/serializers/slateRemark.js b/src/components/Widgets/Markdown/serializers/slateRemark.js index b586cc81..d4ec5f78 100644 --- a/src/components/Widgets/Markdown/serializers/slateRemark.js +++ b/src/components/Widgets/Markdown/serializers/slateRemark.js @@ -56,24 +56,6 @@ function processCodeMark(markTypes) { } -/** - * Returns an array of one or more MDAST text nodes of the given type, derived - * from the text received. Certain transformations, such as line breaks, cause - * multiple nodes to be returned. - */ -function createTextNodes(text, type = 'html') { - /** - * Split the text string at line breaks, then map each substring to an array - * pair consisting of an MDAST text node followed by a break node. This will - * result in nested arrays, so we use `flatMap` to produce a flattened array, - * and `initial` to leave off the superfluous trailing break. - */ - const brokenText = text.split('\n'); - const toPair = str => [u(type, str), u('break')]; - return initial(flatMap(brokenText, toPair)); -} - - /** * Wraps a text node in one or more mark nodes by placing the text node in an * array and using that as the `children` value of a mark node. The resulting @@ -143,12 +125,15 @@ function wrapTextWithMarks(textNode, markTypes) { */ function convertTextNode(node) { + if (get(node.data, 'isBreak')) { + return u('break'); + } /** * If the Slate text node has no "ranges" property, just return an equivalent * MDAST node. */ if (!node.ranges) { - return createTextNodes(node.text); + return u('html', node.text); } /** @@ -172,13 +157,13 @@ function convertTextNode(node) { /** * Create the base text node. */ - const textNodes = createTextNodes(text, textNodeType); + const textNode = u(textNodeType, text); /** * Recursively wrap the base text node in the individual mark nodes, if * any exist. */ - return textNodes.map(textNode => wrapTextWithMarks(textNode, filteredMarkTypes)); + return wrapTextWithMarks(textNode, filteredMarkTypes); }); /**