mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 15:06:41 +07:00
added links trimmer
This commit is contained in:
parent
31af4e11b1
commit
2d2c959925
3 changed files with 29 additions and 14 deletions
10
src/utils/transformMDLinks.ts
Normal file
10
src/utils/transformMDLinks.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
const urlRegex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/g;
|
||||
|
||||
const trimTo = (val: string, maxLength: number) =>
|
||||
val.length > maxLength ? val.substring(0, maxLength - 1).concat("…") : val;
|
||||
|
||||
/** Formatting all links in markdown output, trimming them to reasonable length */
|
||||
export default (value: string) =>
|
||||
value.replace(urlRegex, (val) => {
|
||||
return `[${trimTo(val, 20)}](${val})`;
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue