diff --git a/src/Utils/link-preview.ts b/src/Utils/link-preview.ts index 2bc0205..10bae83 100644 --- a/src/Utils/link-preview.ts +++ b/src/Utils/link-preview.ts @@ -18,7 +18,7 @@ export type URLGenerationOptions = { /** * Given a piece of text, checks for any URL present, generates link preview for the same and returns it * Return undefined if the fetch failed or no URL was found - * @param text the text containing URL + * @param text first matched URL in text * @returns the URL info required to generate link preview */ export const getUrlInfo = async( @@ -27,8 +27,11 @@ export const getUrlInfo = async( ): Promise => { try { const { getLinkPreview } = await import('link-preview-js') - - const info = await getLinkPreview(text, { timeout: opts.timeoutMs }) + let previewLink = text + if (!text.startsWith('https://') && !text.startsWith('http://')) { + previewLink = 'https://' + previewLink + } + const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs }) if(info && 'title' in info) { const [image] = info.images @@ -38,7 +41,7 @@ export const getUrlInfo = async( return { 'canonical-url': info.url, - 'matched-text': info.url, + 'matched-text': text, title: info.title, description: info.description, jpegThumbnail diff --git a/src/Utils/messages.ts b/src/Utils/messages.ts index baaa1b4..658722b 100644 --- a/src/Utils/messages.ts +++ b/src/Utils/messages.ts @@ -248,9 +248,10 @@ export const generateWAMessageContent = async( const extContent = { text: message.text } as WATextMessage let urlInfo = message.linkPreview - if(!urlInfo && !!options.getUrlInfo && message.text.match(URL_REGEX)) { + const matchedUrls = message.text.match(URL_REGEX) + if(!urlInfo && !!options.getUrlInfo && matchedUrls) { try { - urlInfo = await options.getUrlInfo(message.text) + urlInfo = await options.getUrlInfo(matchedUrls[0]) } catch(error) { // ignore if fails options.logger?.warn({ trace: error.stack }, 'url generation failed') }