Skip to content

Commit f1f1d13

Browse files
authored
fix(markdown): remove empty links when text is deleted (#7702)
When a user deletes all text within a link in the visual markdown editor, the empty link markup was being left behind (visible when switching to raw markdown mode). This adds a Slate normalizer that automatically removes link nodes when they become empty (have no text content), ensuring the link markup is properly cleaned up along with the text. Fixes #7640
1 parent e795271 commit f1f1d13

File tree

1 file changed

+19
-1
lines changed
  • packages/decap-cms-widget-markdown/src/MarkdownControl/plugins/inlines

1 file changed

+19
-1
lines changed

packages/decap-cms-widget-markdown/src/MarkdownControl/plugins/inlines/withInlines.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1+
import { Element, Node, Transforms } from 'slate';
2+
13
import keyDown from './events/keyDown';
24

35
function withInlines(editor) {
4-
const { isInline, isVoid } = editor;
6+
const { isInline, isVoid, normalizeNode } = editor;
57

68
editor.isInline = element =>
79
['link', 'button', 'break', 'image'].includes(element.type) || isInline(element);
810

911
editor.isVoid = element =>
1012
['break', 'image', 'thematic-break'].includes(element.type) || isVoid(element);
1113

14+
// Remove empty links (fixes #7640: hidden link remains when deleting linked text)
15+
editor.normalizeNode = entry => {
16+
const [node, path] = entry;
17+
18+
if (Element.isElement(node) && node.type === 'link') {
19+
// Check if link is empty (no text content)
20+
const text = Node.string(node);
21+
if (text === '') {
22+
Transforms.removeNodes(editor, { at: path });
23+
return;
24+
}
25+
}
26+
27+
normalizeNode(entry);
28+
};
29+
1230
if (editor.keyDownHandlers === undefined) {
1331
editor.keyDownHandlers = [];
1432
}

0 commit comments

Comments
 (0)