Skip to content

Commit eb2d65d

Browse files
committed
refactor(markdown-slate): clarify functionality
Signed-off-by: irmerk <[email protected]>
1 parent c474baa commit eb2d65d

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

packages/markdown-slate/src/slateToCiceroMarkDom.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,19 @@
1717
const NS = 'org.accordproject.commonmark';
1818
const NS_CICERO = 'org.accordproject.ciceromark';
1919

20-
const lastNode = (input) => input.nodes[input.nodes.length - 1] &&
21-
input.nodes[input.nodes.length - 1].$class;
22-
23-
const isSecondLastClause = (input) => (input.nodes.length > 1) &&
24-
input.nodes[input.nodes.length - 2].$class === 'org.accordproject.commonmark.Clause';
25-
26-
const islastNodeParagraph = (input) => lastNode(input)
27-
.$class === 'org.accordproject.commonmark.Paragraph';
28-
29-
const isParagraphEmpty = (input) => lastNode(input).nodes
30-
? lastNode(input).nodes[0].text === ''
31-
: null;
32-
33-
const isEmptyParaAfterClause = (input) => {
34-
if (!input.nodes || !input.nodes.length) { return false; }
35-
36-
if (islastNodeParagraph(input) &&
37-
isParagraphEmpty(input) &&
38-
isSecondLastClause(input)) {
39-
return {
40-
$class : 'org.accordproject.commonmark.Document',
41-
xmlns : 'http://commonmark.org/xml/1.0',
42-
nodes : input.nodes.splice(0, input.nodes.length-1)
43-
};
20+
21+
/**
22+
* Removed the final node if it is an empty paragraph following a clause
23+
* @param {*} input the current result of slateToCiceroMarkDom
24+
* @returns {*} the final result of slateToCiceroMarkDom
25+
*/
26+
const fixEmptyParaAfterClause = (input) => {
27+
const nodes = input.nodes;
28+
if (nodes.length >= 2 &&
29+
nodes[nodes.length - 2].$class === 'org.accordproject.commonmark.Clause' &&
30+
nodes[nodes.length - 1].$class === 'org.accordproject.commonmark.Paragraph' &&
31+
nodes[nodes.length - 1].text === '') {
32+
input.nodes = nodes.splice(0, nodes.length-1);
4433
}
4534
return input;
4635
};
@@ -60,7 +49,7 @@ function slateToCiceroMarkDom(document) {
6049
// convert the value to a plain object
6150
const json = JSON.parse(JSON.stringify(document));
6251
_recursive(result, json.nodes);
63-
return isEmptyParaAfterClause(result);
52+
return fixEmptyParaAfterClause(result);
6453
}
6554

6655
/**

0 commit comments

Comments
 (0)