Skip to content

Commit

Permalink
should not auto link immediately after a link
Browse files Browse the repository at this point in the history
  • Loading branch information
lucywang000 committed Jun 7, 2021
1 parent d42c08d commit 4e267e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
29 changes: 29 additions & 0 deletions core/components/__tests__/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,35 @@ describe('Input rule', () => {
</para>
</doc>,
],
[
<doc>
<para>
<link href="http://123.com">123.com</link>
{' '}def.com[]
</para>
</doc>,
<doc>
<para>
<link href="http://123.com">123.com</link>
{' '}<link href="http://def.com">def.com</link>{' '}[]
</para>
</doc>,
],
// No auto link when there is no space after the prev link
[
<doc>
<para>
<link href="http://123.com">123.com</link>
def.com[]
</para>
</doc>,
<doc>
<para>
<link href="http://123.com">123.com</link>
def.com []
</para>
</doc>,
],
])('%# input rule', async (input, expected) => {
const { editorView } = testEditor(input);

Expand Down
23 changes: 22 additions & 1 deletion core/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,28 @@ function autoLinkInputRule(type) {
return null;
}
const [_, leadingSpace, text, scheme] = match;
// If no scheme, use default scheme http://
if (!leadingSpace) {
// Do nothing there is already a link within [start, end] This is for
// cases like "<link>abc.com</link>def.com[]". In such case typing a space
// after def.com should not auto link.
let ignore = false;
state.doc.nodesBetween(start, end, (node) => {
if (ignore) {
return false;
}
if (type.isInSet(node.marks)) {
ignore = true;
return false;
}
return true;
});

if (ignore) {
return null;
}
}
// If no scheme, use default scheme "http://". Most https sites would do a
// redirect for http request anyway.
const href = scheme ? text : `http://${text}`;
const tr = state.tr;
tr.addMark(
Expand Down

0 comments on commit 4e267e6

Please sign in to comment.