fix: filter and parse the extra line breaks of the text

This commit is contained in:
yanmao 2021-12-24 13:21:55 +08:00
parent f865c2d9a9
commit cc935e2645
3 changed files with 27 additions and 9 deletions

View File

@ -1065,15 +1065,17 @@ class Inline implements InlineModelInterface {
else { else {
let parentMark: NodeInterface | undefined = let parentMark: NodeInterface | undefined =
markApi.closest(node); markApi.closest(node);
const element = node as NodeInterface;
while ( while (
parentMark && parentMark &&
!parentMark.equal(node) && !parentMark.equal(node) &&
nodeApi.isMark(parentMark, schema) nodeApi.isMark(parentMark, schema)
) { ) {
const cloneMark = parentMark.clone(); const cloneMark = parentMark.clone();
const inlineMark = node.clone(); const cloneInline = node.clone();
const children = parentMark.children(); const children = parentMark.children();
children.each((markChild) => { children.each((markChild, index) => {
// 零宽字符的文本跳过 // 零宽字符的文本跳过
if ( if (
markChild.nodeType === 3 && markChild.nodeType === 3 &&
@ -1081,13 +1083,13 @@ class Inline implements InlineModelInterface {
) { ) {
return; return;
} }
if ((node as NodeInterface).equal(markChild)) { if (
element.equal(markChild) ||
children.eq(index)?.contains(element)
) {
node = nodeApi.wrap( node = nodeApi.wrap(
nodeApi.replace( nodeApi.replace(element, cloneMark),
node as NodeInterface, cloneInline,
cloneMark,
),
inlineMark,
); );
this.repairBoth(node); this.repairBoth(node);
} else { } else {

View File

@ -607,6 +607,18 @@ class Parser implements ParserInterface {
schema || this.editor.schema, schema || this.editor.schema,
) )
) { ) {
const children = node.children().toArray();
// 子节点还有block节点则不换行
if (
children.some((child) => child.name === 'br') ||
children.some((child) =>
this.editor.node.isBlock(
child,
schema || this.editor.schema,
),
)
)
return;
result.push('\n'); result.push('\n');
} }
}, },

View File

@ -209,7 +209,11 @@ export default class<T extends CodeBlockOptions> extends Plugin<T> {
} }
} }
} }
let code = new Parser(node, this.editor).toText(); let code = new Parser(node, this.editor).toText(
undefined,
undefined,
false,
);
code = unescape(code.replace(/\u200b/g, '')); code = unescape(code.replace(/\u200b/g, ''));
this.editor.card.replaceNode<CodeBlockValue>(node, 'codeblock', { this.editor.card.replaceNode<CodeBlockValue>(node, 'codeblock', {
mode: syntax || 'plain', mode: syntax || 'plain',