The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/app/lib/server/functions/extractUrlsFromMessageAST.ts

33 lines
730 B

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;
};