mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
fix url extraction
This commit is contained in:
parent
96fea09b3f
commit
6b0b0b9b57
2 changed files with 22 additions and 1 deletions
21
src/utils/__tests__/extractURLs.test.ts
Normal file
21
src/utils/__tests__/extractURLs.test.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { extractURLs } from "../extract";
|
||||
|
||||
describe("extractURLs", () => {
|
||||
it("extracts simple urls", () => {
|
||||
const result = extractURLs(
|
||||
"Trying out links https://map.vault48.org/test 123"
|
||||
);
|
||||
|
||||
expect(result.length).toBe(1);
|
||||
expect(result[0].href).toBe("https://map.vault48.org/test");
|
||||
});
|
||||
|
||||
it("works with that weird new VK urls", () => {
|
||||
const result = extractURLs(
|
||||
"Trying out links: [#alias|map.vault48.org/test|https://map.vault48.org/test]"
|
||||
);
|
||||
|
||||
expect(result.length).toBe(1);
|
||||
expect(result[0].href).toBe("https://map.vault48.org/test");
|
||||
});
|
||||
});
|
|
@ -1,6 +1,6 @@
|
|||
import { URL } from "url";
|
||||
|
||||
const urlRe = /(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,})/gim;
|
||||
const urlRe = /(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,})/gim;
|
||||
|
||||
export const extractURLs = (text: string): URL[] => {
|
||||
const matches = text.match(urlRe) || [];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue