Skip to content

Commit

Permalink
πŸ’ Fix bug when empty array considered truthy (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rnbsov authored Mar 15, 2024
2 parents 77f444f + e75b7e6 commit 135b2de
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/utils/getUrlAndLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ export function getUrlAndLabels(ctx: Filter<MyContext, 'message:entities:url'>)
// parse url from the message
if (startsWithUrl(message)) {
;({ url, labels } = parseUrls(message)[0])
} else if (ctx.entities('text_link')) {
} else if (ctx.entities('text_link').length > 0) {
// handle case when user sends a message with text formatted link
const linkEntity = ctx.entities('text_link').find((entity) => entity.type === 'text_link')
const linkEntity = ctx.entities('text_link')[0]
if (linkEntity && linkEntity.url) {
url = linkEntity.url
}
labels = []
} else {
// retrieve the first url from the message
const urlMatch = message.match(/(?:https?:\/\/|www\.)\S+?(?=\s|$)/);
url = urlMatch ? urlMatch[0] : '';
labels = [];
// retrieve the first url from the message/post
const urlEntity = ctx.entities('url')[0]
if (urlEntity && urlEntity.text) {
url = urlEntity.text
}
labels = []
}

// add default label
Expand Down

0 comments on commit 135b2de

Please sign in to comment.