Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoSven committed Jul 19, 2022
1 parent b71ea89 commit 984eae4
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 233 deletions.
113 changes: 112 additions & 1 deletion packages/remirror__extension-link/__tests__/link-extension.spec.ts
Expand Up @@ -737,7 +737,7 @@ describe('autolinking', () => {
);
});

it('detects seperating two links', () => {
it('detects seperating two links by entering a space', () => {
editor.add(doc(p('<cursor>'))).insertText('github.comremirror.io');

expect(editor.doc).toEqualRemirrorDocument(
Expand All @@ -757,6 +757,20 @@ describe('autolinking', () => {
);
});

it('detects seperating two links by `Enter` key press', () => {
editor
.add(doc(p('test.coremirror.io')))
.selectText(8)
.press('Enter');

expect(editor.doc).toEqualRemirrorDocument(
doc(
p(link({ auto: true, href: '//test.co' })('test.co')),
p(link({ auto: true, href: '//remirror.io' })('remirror.io')),
),
);
});

it('supports deleting selected to to invalidate the match', () => {
editor
.add(doc(p('<cursor>')))
Expand Down Expand Up @@ -1082,6 +1096,61 @@ describe('autolinking', () => {
);
});

it('does not create a link if not in selection range - edit text after', () => {
editor.add(doc(p('remirror.io tester'))).backspace(2);

expect(editor.doc).toEqualRemirrorDocument(doc(p('remirror.io', ' test')));
});

it('does not create a link if not in selection range - edit text before', () => {
editor
.add(doc(p('tester remirror.io')))
.selectText(7)
.backspace(2);

expect(editor.doc).toEqualRemirrorDocument(doc(p('test', ' remirror.io')));
});

it('does not create a link if not in selection range - create link after', () => {
editor
.add(doc(p('remirror.io test ')))
.backspace()
.insertText('.com');

expect(editor.doc).toEqualRemirrorDocument(
doc(p('remirror.io ', link({ auto: true, href: '//test.com' })('test.com'))),
);
});

it('does not create a link if not in selection range - create link before', () => {
editor
.add(doc(p('test remirror.io')))
.selectText(5)
.insertText('.com');

expect(editor.doc).toEqualRemirrorDocument(
doc(p(link({ auto: true, href: '//test.com' })('test.com'), ' remirror.io')),
);
});

it('allows creating identical links', () => {
editor
.add(doc(p(link({ auto: true, href: '//test.com' })('test.com'))))
.press('Enter')
.insertText('test.com test.com');

expect(editor.doc).toEqualRemirrorDocument(
doc(
p(link({ auto: true, href: '//test.com' })('test.com')),
p(
link({ auto: true, href: '//test.com' })('test.com'),
' ',
link({ auto: true, href: '//test.com' })('test.com'),
),
),
);
});

it('detects emails as auto link', () => {
editor.add(doc(p('<cursor>'))).insertText('user@example.com');

Expand Down Expand Up @@ -1635,6 +1704,27 @@ describe('adjacent punctuations', () => {
);
});

it('should remove unbalanced parts - URL path', () => {
const editor = renderEditor([
new LinkExtension({
autoLink: true,
}),
]);
const {
attributeMarks: { link },
nodes: { doc, p },
} = editor;

editor
.add(doc(p('<cursor>')))
.insertText('remirror.io/test(balance))')
.backspace(2);

expect(editor.doc).toEqualRemirrorDocument(
doc(p(link({ auto: true, href: '//remirror.io/test' })('remirror.io/test'), '(balance')),
);
});

it('should check for balanced braces - URL query', () => {
const editor = renderEditor([
new LinkExtension({
Expand All @@ -1657,4 +1747,25 @@ describe('adjacent punctuations', () => {
),
);
});

it('should remove unbalanced parts - URL query', () => {
const editor = renderEditor([
new LinkExtension({
autoLink: true,
}),
]);
const {
attributeMarks: { link },
nodes: { doc, p },
} = editor;

editor
.add(doc(p('<cursor>')))
.insertText('remirror.io?test=(balance))')
.backspace(2);

expect(editor.doc).toEqualRemirrorDocument(
doc(p(link({ auto: true, href: '//remirror.io?test=' })('remirror.io?test='), '(balance')),
);
});
});
58 changes: 16 additions & 42 deletions packages/remirror__extension-link/src/link-extension-utils.ts
Expand Up @@ -85,53 +85,27 @@ export const isBalanced = (input: string): boolean => {
return stack.length === 0;
};

export const getBalancedIndex = (input: string, index: number): number => {
const newString = input.slice(0, index);
export const getBalancedIndex = (input: string, index: number): number =>
!isBalanced(input.slice(0, index)) ? getBalancedIndex(input, --index) : index;

if (!isBalanced(newString)) {
return getBalancedIndex(input, --index);
}

return index;
};

export const getTrailingPunctuationIndex = (input: string, index: number): number => {
const newString = input.slice(0, index);

if (SENTENCE_PUNCTUATIONS.includes(newString.slice(-1))) {
return getTrailingPunctuationIndex(input, --index);
}

return index;
};
export const getTrailingPunctuationIndex = (input: string, index: number): number =>
SENTENCE_PUNCTUATIONS.includes(input.slice(0, index).slice(-1))
? getTrailingPunctuationIndex(input, --index)
: index;

export const getAdjacentCharCount = ({
direction,
export const getTrailingCharIndex = ({
adjacentPunctuations,
input,
url,
url = '',
}: {
direction: 0 | 1;
adjacentPunctuations: typeof DEFAULT_ADJACENT_PUNCTUATIONS;
input: string;
url: string | undefined;
}) => {
const length = ((url && input.split(url)[direction]) || '').length;
url: string;
}): number | undefined =>
(input.split(url)[1] || '').length * -1 ||
(adjacentPunctuations.includes(input.slice(-1)) ? -1 : undefined);

if (length > 1) {
return Math.abs(length) * (direction === 1 ? -1 : 1);
}

return;
};

export const addProtocol = (input: string) =>
export const addProtocol = (input: string, defaultProtocol?: string): string =>
['http://', 'https://', 'ftp://'].some((protocol) => input.startsWith(protocol))
? input
: `https://${input}`;

export const createNewURL = (input: string) => {
try {
return new URL(addProtocol(input));
} catch {
return;
}
};
: `${defaultProtocol && defaultProtocol.length > 0 ? defaultProtocol : 'https:'}//${input}`;

0 comments on commit 984eae4

Please sign in to comment.