我正在尝试在 slatejs 编辑器中对 **text** 应用粗体,到目前为止我的尝试没有成功。
我遇到了这个答案,这似乎是解决问题的可能方法。
但是,在修改该答案后,它仍然拒绝应用粗体。
我尝试添加match: n => Text.isText(n)
,这使整个段落变得粗体。
预期结果:**文本** => **文本**
实际结果:**文本** => **文本**
我该如何修改它以按预期工作?
const withMarkdown = editor => {
const { normalizeNode } = editor;
editor.normalizeNode = entry => {
const [node, path] = entry;
if (!Text.isText(node)) {
return normalizeNode([node, path]);
}
const boldMatch = node.text.match(/([*]{2})(.+?)([*]{2})/);
if (!boldMatch) {
return normalizeNode([node, path]);
}
let [searchMatch, asteriskMatch] = boldMatch;
const { index: startIndex } = boldMatch;
const endIndex = startIndex + searchMatch.length;
/* does not apply bold */
Transforms.setNodes(editor, { bold: true }, {
at: {
anchor: { path, offset: startIndex },
focus: { path, offset: endIndex },
}
})
normalizeNode([node, path]);
}
return editor;
}
编辑:尝试了这个并得到了预期的结果,但随之而来的是一个错误。
Transforms.insertText(editor, searchMatch, {
at: {
anchor: { path, offset: startIndex },
focus: { path, offset: endIndex },
}
})
Transforms.setNodes(editor,
{ bold: true },
{
at: {
anchor: { path, offset: startIndex },
focus: { path, offset: endIndex }
},
match: n => Text.isText(n), split: true
}
);
错误信息:
Could not completely normalize the editor after 126 iterations! This is usually due to incorrect normalization logic that leaves a node in an invalid state.