fix editor parsing of styled inline code
This commit is contained in:
@ -17,6 +17,7 @@ describe('slate', () => {
|
|||||||
expect(process('**[a](b)**')).toEqual('**[a](b)**\n');
|
expect(process('**[a](b)**')).toEqual('**[a](b)**\n');
|
||||||
expect(process('****')).toEqual('****\n');
|
expect(process('****')).toEqual('****\n');
|
||||||
expect(process('_`a`_')).toEqual('_`a`_\n');
|
expect(process('_`a`_')).toEqual('_`a`_\n');
|
||||||
|
expect(process('_`a`b_')).toEqual('_`a`b_\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should condense adjacent, identically styled text and inline nodes', () => {
|
it('should condense adjacent, identically styled text and inline nodes', () => {
|
||||||
|
@ -98,39 +98,48 @@ function processMarkNode(node, parentMarks = []) {
|
|||||||
const markType = markMap[node.type];
|
const markType = markMap[node.type];
|
||||||
const marks = markType ? [...parentMarks, { type: markMap[node.type] }] : parentMarks;
|
const marks = markType ? [...parentMarks, { type: markMap[node.type] }] : parentMarks;
|
||||||
|
|
||||||
/**
|
const children = flatMap(node.children, childNode => {
|
||||||
* Set an array to collect sections of text.
|
switch (childNode.type) {
|
||||||
*/
|
/**
|
||||||
const slateNodes = [];
|
* If a text node is a direct child of the current node, it should be
|
||||||
|
* set aside as a range, and all marks that have been collected in the
|
||||||
|
* `marks` array should apply to that specific range.
|
||||||
|
*/
|
||||||
|
case 'html':
|
||||||
|
case 'text':
|
||||||
|
return { text: childNode.value, marks };
|
||||||
|
|
||||||
node.children && node.children.forEach(childNode => {
|
/**
|
||||||
/**
|
* MDAST inline code nodes don't have children, just a text value, similar
|
||||||
* If a text node is a direct child of the current node, it should be
|
* to a text node, so it receives the same treatment as a text node, but we
|
||||||
* set aside as a range, and all marks that have been collected in the
|
* first add the inline code mark to the marks array.
|
||||||
* `marks` array should apply to that specific range.
|
*/
|
||||||
*/
|
case 'inlineCode': {
|
||||||
if (['html', 'text'].includes(childNode.type)) {
|
const childMarks = [ ...marks, { type: markMap['inlineCode'] } ];
|
||||||
slateNodes.push({ text: childNode.value, marks });
|
return { text: childNode.value, marks: childMarks };
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process nested style nodes. The recursive results should be pushed into
|
||||||
|
* the ranges array. This way, every MDAST nested text structure becomes a
|
||||||
|
* flat array of ranges that can serve as the value of a single Slate Raw
|
||||||
|
* text node.
|
||||||
|
*/
|
||||||
|
case 'strong':
|
||||||
|
case 'emphasis':
|
||||||
|
case 'delete':
|
||||||
|
return processMarkNode(childNode, marks);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remaining nodes simply need mark data added to them, and to then be
|
||||||
|
* added into the cumulative children array.
|
||||||
|
*/
|
||||||
|
default:
|
||||||
|
return { ...childNode, data: { marks } };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Process nested style nodes. The recursive results should be pushed into
|
|
||||||
* the ranges array. This way, every MDAST nested text structure becomes a
|
|
||||||
* flat array of ranges that can serve as the value of a single Slate Raw
|
|
||||||
* text node.
|
|
||||||
*/
|
|
||||||
if (['strong', 'emphasis', 'delete'].includes(childNode.type)) {
|
|
||||||
const nestedSlateNodes = processMarkNode(childNode, marks);
|
|
||||||
slateNodes.push(...nestedSlateNodes);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const nestedSlateNode = { ...childNode, data: { marks } };
|
|
||||||
slateNodes.push(nestedSlateNode);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return slateNodes;
|
return children;
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertMarkNode(node) {
|
function convertMarkNode(node) {
|
||||||
|
Reference in New Issue
Block a user