Skip to content

Commit

Permalink
fix(link): Insert character link duplicates with markdown conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
big-camel committed May 27, 2022
1 parent 7413c7b commit 66c8f44
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
23 changes: 22 additions & 1 deletion packages/engine/src/inline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1055,11 +1055,32 @@ class Inline implements InlineModelInterface {
.shrinkToElementNode()
.createSelection();
const inlines = this.findInlines(node);
const applyInlines: NodeInterface[] = [];
inlines.forEach((inline) => {
if (inline.isCard()) return;
this.flat(inline);
const newInline = this.flat(inline);
if (newInline) applyInlines.push(newInline);
});
selection.move();
const nodeApi = this.editor.node;
applyInlines.forEach((inline) => {
const prev = inline.prev()?.prev();
const next = inline.next()?.next();
if (
prev &&
nodeApi.isMark(prev) &&
prev.get<Element>()!.childNodes.length === 0
) {
prev.remove();
}
if (
next &&
nodeApi.isMark(next) &&
next.get<Element>()!.childNodes.length === 0
) {
next.remove();
}
});
return;
}
if (node.isCard()) return;
Expand Down
21 changes: 0 additions & 21 deletions plugins/link-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default class<
});
this.editor.on('markdown-it', this.markdownIt);
}
editor.on('paste:each', this.pasteHtml);
editor.on('parse:html', this.parseHtml);
editor.on('select', this.bindQuery);
editor.language.add(locales);
Expand Down Expand Up @@ -136,27 +135,7 @@ export default class<
});
};

pasteHtml = (child: NodeInterface) => {
if (child.isText()) {
const text = child.text();
const { node, inline } = this.editor;
if (
/^https?:\/\/\S+$/.test(text.toLowerCase().trim()) &&
inline.closest(child).equal(child)
) {
const newNode = node.wrap(
child,
$(`<${this.tagName} target="_blank" href="${text}"></a>`),
);
inline.repairCursor(newNode);
return false;
}
}
return true;
};

destroy(): void {
this.editor.off('paste:each', this.pasteHtml);
this.editor.off('parse:html', this.parseHtml);
this.editor.off('select', this.bindQuery);
this.editor.off('markdown-it', this.markdownIt);
Expand Down
21 changes: 0 additions & 21 deletions plugins/link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default class<
});
this.editor.on('markdown-it', this.markdownIt);
}
editor.on('paste:each', this.pasteHtml);
editor.on('parse:html', this.parseHtml);
editor.on('select', this.bindQuery);
editor.language.add(locales);
Expand Down Expand Up @@ -136,27 +135,7 @@ export default class<
});
};

pasteHtml = (child: NodeInterface) => {
if (child.isText()) {
const text = child.text();
const { node, inline } = this.editor;
if (
/^https?:\/\/\S+$/.test(text.toLowerCase().trim()) &&
inline.closest(child).equal(child)
) {
const newNode = node.wrap(
child,
$(`<${this.tagName} target="_blank" href="${text}"></a>`),
);
inline.repairCursor(newNode);
return false;
}
}
return true;
};

destroy(): void {
this.editor.off('paste:each', this.pasteHtml);
this.editor.off('parse:html', this.parseHtml);
this.editor.off('select', this.bindQuery);
this.editor.off('markdown-it', this.markdownIt);
Expand Down

0 comments on commit 66c8f44

Please sign in to comment.