diff --git a/packages/app/src/SnortUtils/Utils.test.ts b/packages/app/src/SnortUtils/Utils.test.ts index b91d02ae..bbd17a66 100644 --- a/packages/app/src/SnortUtils/Utils.test.ts +++ b/packages/app/src/SnortUtils/Utils.test.ts @@ -1,61 +1,6 @@ -import { NostrPrefix } from "@snort/system"; -import { splitByUrl, magnetURIDecode, getRelayName } from "."; +import { magnetURIDecode, getRelayName } from "."; import { describe, expect } from "@jest/globals"; -describe("splitByUrl", () => { - it("should split a string by URLs", () => { - const inputStr = - "@npub1q6mcr8t not https://example.com- sure what your stack is, https://example.com but I made a https://example.com! simple example (https://example.com) of how https://example.com/yo-yo https://example.example.com to do this https://example.com, https://example.com?q=asdf for Next.js apps hosted on Vercel https://example.com. Scarcity in money provides the incentive to create abundance in other things as there is a mechanism to reliably store value. https://i.imgur.com/rkqhjeq.png Every form of money that could be inflated by way of force or technological advancement has been."; - const expectedOutput = [ - "@npub1q6mcr8t not ", - "https://example.com-", - " sure what your stack is, ", - "https://example.com", - " but I made a ", - "https://example.com", - "! simple example (", - "https://example.com)", - " of how ", - "https://example.com/yo-yo", - " ", - "https://example.example.com", - " to do this ", - "https://example.com", - ", ", - "https://example.com?q=asdf", - " for Next.js apps hosted on Vercel ", - "https://example.com", - ". Scarcity in money provides the incentive to create abundance in other things as there is a mechanism to reliably store value. ", - "https://i.imgur.com/rkqhjeq.png", - " Every form of money that could be inflated by way of force or technological advancement has been.", - ]; - - expect(splitByUrl(inputStr)).toEqual(expectedOutput); - }); - - it("should parse nostr links", () => { - const input = - "web+nostr:npub1v0lxxxxutpvrelsksy8cdhgfux9l6a42hsj2qzquu2zk7vc9qnkszrqj49\nnostr:note1jp6d36lmquhxqn2s5n4ce00pzu2jrpkek8udav6l0y3qcdngpnxsle6ngm\nnostr:naddr1qqv8x6r0wf6x2um594cxzarg946x7ttpwajhxmmdv5pzqx78pgq53vlnzmdr8l3u38eru0n3438lnxqz0mr39wg9e5j0dfq3qvzqqqr4gu5d05rr\nnostr is cool"; - const expected = [ - "", - "web+nostr:npub1v0lxxxxutpvrelsksy8cdhgfux9l6a42hsj2qzquu2zk7vc9qnkszrqj49", - "\n", - "nostr:note1jp6d36lmquhxqn2s5n4ce00pzu2jrpkek8udav6l0y3qcdngpnxsle6ngm", - "\n", - "nostr:naddr1qqv8x6r0wf6x2um594cxzarg946x7ttpwajhxmmdv5pzqx78pgq53vlnzmdr8l3u38eru0n3438lnxqz0mr39wg9e5j0dfq3qvzqqqr4gu5d05rr", - "\nnostr is cool", - ]; - expect(splitByUrl(input)).toEqual(expected); - }); - - it("should return an array with a single string if no URLs are found", () => { - const inputStr = "This is a regular string with no URLs"; - const expectedOutput = ["This is a regular string with no URLs"]; - - expect(splitByUrl(inputStr)).toEqual(expectedOutput); - }); -}); - describe("magnet", () => { it("should parse magnet link", () => { const book = diff --git a/packages/app/src/SnortUtils/index.ts b/packages/app/src/SnortUtils/index.ts index 771ac829..33ca7af6 100644 --- a/packages/app/src/SnortUtils/index.ts +++ b/packages/app/src/SnortUtils/index.ts @@ -326,13 +326,6 @@ export function groupByPubkey(acc: Record, user: Metadata return { ...acc, [user.pubkey]: user }; } -export function splitByUrl(str: string) { - const urlRegex = - /((?:http|ftp|https|nostr|web\+nostr|magnet):\/?\/?(?:[\w+?.\w+])+(?:[a-zA-Z0-9~!@#$%^&*()_\-=+\\/?.:;',]*)?(?:[-A-Za-z0-9+&@#/%=~()_|]))/i; - - return str.split(urlRegex); -} - export const delay = (t: number) => { return new Promise(resolve => { setTimeout(resolve, t); diff --git a/packages/system/src/utils.ts b/packages/system/src/utils.ts index 3553ece2..ec9a2b35 100644 --- a/packages/system/src/utils.ts +++ b/packages/system/src/utils.ts @@ -46,7 +46,7 @@ export function flatFilterEq(a: FlatReqFilter, b: FlatReqFilter): boolean { export function splitByUrl(str: string) { const urlRegex = - /((?:http|ftp|https|nostr|web\+nostr|magnet):\/?\/?(?:[\w+?.\w+])+(?:[a-zA-Z0-9~!@#$%^&*()_\-=+\\/?.:;',]*)?(?:[-A-Za-z0-9+&@#/%=~()_|]))/i; + /((?:http|ftp|https|nostr|web\+nostr|magnet):\/?\/?(?:[\w+?.\w+])+(?:[\p{L}\p{N}~!@#$%^&*()_\-=+\\/?.:;',]*)?(?:[-a-z0-9+&@#/%=~()_|]))/iu; return str.split(urlRegex); } diff --git a/packages/system/tests/utils.test.ts b/packages/system/tests/utils.test.ts index dc4aac99..779d83a9 100644 --- a/packages/system/tests/utils.test.ts +++ b/packages/system/tests/utils.test.ts @@ -1,5 +1,62 @@ import { NostrPrefix } from "../src/links"; import { parseNostrLink, tryParseNostrLink } from "../src/nostr-link"; +import { splitByUrl } from "../src/utils"; + +describe("splitByUrl", () => { + it("should split a string by URLs", () => { + const inputStr = + "@npub1q6mcr8t not https://example.com- sure what your stack is, https://example.com but I made a https://example.com! simple example (https://example.com) of how https://example.com/yo-yo https://example.example.com to do this https://example.com, https://example.com?q=asdf for Next.js apps hosted on Vercel https://example.com. Scarcity in money provides the incentive to create abundance in other things as there is a mechanism to reliably store value. https://i.imgur.com/rkqhjeq.png Every form of money that could be inflated by way of force or technological advancement has been. https://www.dw.com/de/amtsinhaber-mnangagwa-gewinnt-präsidentenwahl-in-simbabwe/a-66640006?maca=de-rss-de-all-1119-xml-atom and some shit."; + const expectedOutput = [ + "@npub1q6mcr8t not ", + "https://example.com-", + " sure what your stack is, ", + "https://example.com", + " but I made a ", + "https://example.com", + "! simple example (", + "https://example.com)", + " of how ", + "https://example.com/yo-yo", + " ", + "https://example.example.com", + " to do this ", + "https://example.com", + ", ", + "https://example.com?q=asdf", + " for Next.js apps hosted on Vercel ", + "https://example.com", + ". Scarcity in money provides the incentive to create abundance in other things as there is a mechanism to reliably store value. ", + "https://i.imgur.com/rkqhjeq.png", + " Every form of money that could be inflated by way of force or technological advancement has been. ", + "https://www.dw.com/de/amtsinhaber-mnangagwa-gewinnt-präsidentenwahl-in-simbabwe/a-66640006?maca=de-rss-de-all-1119-xml-atom", + " and some shit." + ]; + + expect(splitByUrl(inputStr)).toEqual(expectedOutput); + }); + + it("should parse nostr links", () => { + const input = + "web+nostr:npub1v0lxxxxutpvrelsksy8cdhgfux9l6a42hsj2qzquu2zk7vc9qnkszrqj49\nnostr:note1jp6d36lmquhxqn2s5n4ce00pzu2jrpkek8udav6l0y3qcdngpnxsle6ngm\nnostr:naddr1qqv8x6r0wf6x2um594cxzarg946x7ttpwajhxmmdv5pzqx78pgq53vlnzmdr8l3u38eru0n3438lnxqz0mr39wg9e5j0dfq3qvzqqqr4gu5d05rr\nnostr is cool"; + const expected = [ + "", + "web+nostr:npub1v0lxxxxutpvrelsksy8cdhgfux9l6a42hsj2qzquu2zk7vc9qnkszrqj49", + "\n", + "nostr:note1jp6d36lmquhxqn2s5n4ce00pzu2jrpkek8udav6l0y3qcdngpnxsle6ngm", + "\n", + "nostr:naddr1qqv8x6r0wf6x2um594cxzarg946x7ttpwajhxmmdv5pzqx78pgq53vlnzmdr8l3u38eru0n3438lnxqz0mr39wg9e5j0dfq3qvzqqqr4gu5d05rr", + "\nnostr is cool", + ]; + expect(splitByUrl(input)).toEqual(expected); + }); + + it("should return an array with a single string if no URLs are found", () => { + const inputStr = "This is a regular string with no URLs"; + const expectedOutput = ["This is a regular string with no URLs"]; + + expect(splitByUrl(inputStr)).toEqual(expectedOutput); + }); +}); describe("tryParseNostrLink", () => { it("is a valid nostr link", () => {