-
Notifications
You must be signed in to change notification settings - Fork 13.9k
chore: normalize urls extraction for messages #38795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ggazzo
merged 18 commits into
develop
from
copilot/add-link-preview-for-schema-less-links
Feb 20, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b722265
Initial plan
Copilot 88d4431
Add URL extraction from message AST and blocks for link previews
Copilot 041bb74
Parse message text to extract URLs from schema-less links
Copilot 8d8a4b6
Address code review feedback - improve types and error logging
Copilot fed775c
Remove deprecated extractTextFromBlocks function and add unit tests f…
ggazzo cfdadc9
Add test case for local image URL parsing in url.test.ts
ggazzo ecb75d9
Refactor chat end-to-end test to use async/await and implement retry …
ggazzo caa4e62
Update autoLink function to enable IP address detection in URL parsing
ggazzo 3a4f6b2
Fix message link formatting in live chat tests to correctly place quo…
ggazzo 934eb0e
Move parseUrlsInMessage call back to before Message.beforeSave in sen…
Copilot 71bea1b
Move parseUrlsInMessage inside Message.beforeSave method
Copilot 8cebc11
Remove completed TODO comment from parseUrlsInMessage
Copilot eaeea57
Refactor insertMessage and parseUrlsInMessage to improve URL handling…
ggazzo c52a37d
remove consoles
ggazzo d408d13
message.parseUrls
ggazzo dda52b6
Refactor parseUrlsInMessage to include optional parseUrls flag in mes…
ggazzo e4a7ee9
Update sendMessage and updateMessage functions to include parseUrls f…
ggazzo e5a3f47
Refactor chat end-to-end test to improve readability and add retry op…
ggazzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
apps/meteor/app/lib/server/functions/extractUrlsFromMessageAST.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { expect } from 'chai'; | ||
| import { describe, it } from 'mocha'; | ||
|
|
||
| import { extractUrlsFromMessageAST } from './extractUrlsFromMessageAST'; | ||
|
|
||
| describe('extractUrlsFromMessageAST', () => { | ||
| it('should extract URLs from LINK nodes', () => { | ||
| const md = [ | ||
| { | ||
| type: 'PARAGRAPH', | ||
| value: [ | ||
| { | ||
| type: 'LINK', | ||
| value: { | ||
| src: { | ||
| type: 'PLAIN_TEXT', | ||
| value: 'https://rocket.chat', | ||
| }, | ||
| label: [ | ||
| { | ||
| type: 'PLAIN_TEXT', | ||
| value: 'rocket.chat', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const urls = extractUrlsFromMessageAST(md as any); | ||
| expect(urls).to.deep.equal(['https://rocket.chat']); | ||
| }); | ||
|
|
||
| it('should convert // prefix to https://', () => { | ||
| const md = [ | ||
| { | ||
| type: 'PARAGRAPH', | ||
| value: [ | ||
| { | ||
| type: 'LINK', | ||
| value: { | ||
| src: { | ||
| type: 'PLAIN_TEXT', | ||
| value: '//github.com/RocketChat/Rocket.Chat', | ||
| }, | ||
| label: [ | ||
| { | ||
| type: 'PLAIN_TEXT', | ||
| value: 'github.com/RocketChat/Rocket.Chat', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const urls = extractUrlsFromMessageAST(md as any); | ||
| expect(urls).to.deep.equal(['https://github.com/RocketChat/Rocket.Chat']); | ||
| }); | ||
|
|
||
| it('should handle multiple links', () => { | ||
| const md = [ | ||
| { | ||
| type: 'PARAGRAPH', | ||
| value: [ | ||
| { | ||
| type: 'LINK', | ||
| value: { | ||
| src: { | ||
| type: 'PLAIN_TEXT', | ||
| value: 'https://rocket.chat', | ||
| }, | ||
| label: [ | ||
| { | ||
| type: 'PLAIN_TEXT', | ||
| value: 'rocket.chat', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| { | ||
| type: 'PLAIN_TEXT', | ||
| value: ' and ', | ||
| }, | ||
| { | ||
| type: 'LINK', | ||
| value: { | ||
| src: { | ||
| type: 'PLAIN_TEXT', | ||
| value: '//github.com/RocketChat', | ||
| }, | ||
| label: [ | ||
| { | ||
| type: 'PLAIN_TEXT', | ||
| value: 'github.com/RocketChat', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const urls = extractUrlsFromMessageAST(md as any); | ||
| expect(urls).to.deep.equal(['https://rocket.chat', 'https://github.com/RocketChat']); | ||
| }); | ||
|
|
||
| it('should return empty array for undefined or non-array input', () => { | ||
| expect(extractUrlsFromMessageAST(undefined)).to.deep.equal([]); | ||
| expect(extractUrlsFromMessageAST(null as any)).to.deep.equal([]); | ||
| expect(extractUrlsFromMessageAST({} as any)).to.deep.equal([]); | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
apps/meteor/app/lib/server/functions/extractUrlsFromMessageAST.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { Root } from '@rocket.chat/message-parser'; | ||
|
|
||
| /** | ||
| * Extracts all URLs from parsed message AST (message-parser output) | ||
| * Looks for LINK nodes and extracts the src URL | ||
| */ | ||
| export const extractUrlsFromMessageAST = (md?: Root | Root[number] | Root[number]['value']): string[] => { | ||
| if (!md || !Array.isArray(md)) { | ||
| return []; | ||
| } | ||
|
|
||
| const urls: string[] = []; | ||
|
|
||
| const walk = (node: any): void => { | ||
| if (Array.isArray(node)) { | ||
| node.forEach(walk); | ||
| return; | ||
| } | ||
| if (typeof node !== 'object' || node === null) { | ||
| return; | ||
| } | ||
| if (node.type === 'LINK' && node.value?.src?.value) { | ||
| urls.push(node.value.src.value); | ||
| } | ||
| if (node.value !== undefined) { | ||
| walk(node.value); | ||
| } | ||
| }; | ||
|
|
||
| walk(md); | ||
|
|
||
| return urls; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 26 additions & 18 deletions
44
apps/meteor/app/lib/server/functions/parseUrlsInMessage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,37 @@ | ||
| import type { IMessage, AtLeast } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { extractUrlsFromMessageAST } from './extractUrlsFromMessageAST'; | ||
| import { getMessageUrlRegex } from '../../../../lib/getMessageUrlRegex'; | ||
| import { Markdown } from '../../../markdown/server'; | ||
| import { settings } from '../../../settings/server'; | ||
|
|
||
| // TODO move this function to message service to be used like a "beforeSaveMessage" hook | ||
| export const parseUrlsInMessage = (message: AtLeast<IMessage, 'msg'> & { parseUrls?: boolean }, previewUrls?: string[]) => { | ||
| if (message.parseUrls === false) { | ||
| return message; | ||
| } | ||
| const prepareUrl = (url: string, previewUrls: string[] | undefined) => ({ | ||
| url, | ||
| meta: {}, | ||
| ...(previewUrls && !previewUrls.includes(url) && !url.includes(settings.get('Site_Url')) && { ignoreParse: true }), | ||
| }); | ||
|
|
||
| message.html = message.msg; | ||
| message = Markdown.code(message); | ||
| const prepareUrls = (urls: string[], previewUrls?: string[]) => [...new Set(urls)].map((url) => prepareUrl(url, previewUrls)); | ||
|
|
||
| const urls = message.html?.match(getMessageUrlRegex()) || []; | ||
| if (urls) { | ||
| message.urls = [...new Set(urls)].map((url) => ({ | ||
| url, | ||
| meta: {}, | ||
| ...(previewUrls && !previewUrls.includes(url) && !url.includes(settings.get('Site_Url')) && { ignoreParse: true }), | ||
| })); | ||
| export const parseUrlsInMessage = ( | ||
| message: AtLeast<IMessage, 'msg' | 'md'> & { | ||
| parseUrls?: boolean; | ||
| }, | ||
| previewUrls?: string[], | ||
| ) => { | ||
| // Also extract URLs from message blocks if they exist | ||
| if (message.md) { | ||
| const astUrls = extractUrlsFromMessageAST(message.md); | ||
| return prepareUrls(astUrls, previewUrls); | ||
| } | ||
|
|
||
| message = Markdown.mountTokensBack(message, false); | ||
| message.msg = message.html || message.msg; | ||
| delete message.html; | ||
| delete message.tokens; | ||
| // TODO: remove this after make the parser official | ||
| // Parse the message to extract URLs from links without schema | ||
| // The message parser converts links like "github.com" to proper links with "//" prefix | ||
| const result = Markdown.code({ | ||
| html: message.msg, | ||
| msg: message.msg, | ||
| }); | ||
| const htmlUrls = result.html?.match(getMessageUrlRegex()) || []; | ||
| return prepareUrls(htmlUrls, previewUrls); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.