Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/message-parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const isValidLink = (link: string) => {
}
};

const hasAbsoluteSchemePrefix = (src: string) => /^[A-Za-z0-9+-]{1,32}:\/\//.test(src);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

export const link = (src: string, label?: Markup[]): Link => ({
type: 'LINK',
value: { src: plain(src), label: label ?? [plain(src)] },
Expand All @@ -92,7 +94,7 @@ export const autoLink = (src: string, customDomains?: string[]) => {
return plain(src);
}

const href = isValidLink(src) || src.startsWith('//') ? src : `//${src}`;
const href = isValidLink(src) || src.startsWith('//') || hasAbsoluteSchemePrefix(src) ? src : `//${src}`;

return link(href, [plain(src)]);
};
Expand Down
7 changes: 7 additions & 0 deletions packages/message-parser/tests/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ test.each([
['https://test', [paragraph([plain('https://test')])]],
['httpsss://rocket.chat/test', [paragraph([link('httpsss://rocket.chat/test')])]],
['https://rocket.chat:3000/test', [paragraph([link('https://rocket.chat:3000/test')])]],
['https://rocket.chat:99999', [paragraph([link('https://rocket.chat:99999')])]],
['https://rocket.chat:99999/test', [paragraph([link('https://rocket.chat:99999/test')])]],
['https://rocket.chat/test?search', [paragraph([link('https://rocket.chat/test?search')])]],
['https://rocket.chat/test?search=test', [paragraph([link('https://rocket.chat/test?search=test')])]],
['https://rocket.chat', [paragraph([link('https://rocket.chat')])]],
Expand Down Expand Up @@ -157,6 +159,11 @@ describe('autoLink helper function', () => {
expect(autoLink('http://rocket.chat/test')).toMatchObject(link('http://rocket.chat/test'));
});

it('should preserve the original protocol for invalid absolute URLs', () => {
expect(autoLink('https://rocket.chat:99999')).toMatchObject(link('https://rocket.chat:99999'));
expect(autoLink('https://rocket.chat:65536')).toMatchObject(link('https://rocket.chat:65536'));
});

it('should preserve the original protocol even if for custom protocols', () => {
expect(autoLink('custom://rocket.chat/test')).toMatchObject(link('custom://rocket.chat/test'));
});
Expand Down