Open platform to collect and prioritize feedback
refactor(services): Moves url functions to url.ts Moves these functions out of markdown.ts so they can be discovered and reused more easily
widal001204278de8e85
diff --git a/public/components/common/form/LinkInsertModal.tsx b/public/components/common/form/LinkInsertModal.tsxindex b29ac7df6..5bab60103 100644--- a/public/components/common/form/LinkInsertModal.tsx+++ b/public/components/common/form/LinkInsertModal.tsx@@ -4,7 +4,7 @@ import { Form } from "./Form"import { Failure } from "@fider/services"import { Trans } from "@lingui/react/macro"import { i18n } from "@lingui/core"-import { isValidUrl, normalizeUrl } from "@fider/services/markdown"+import { isValidUrl, normalizeUrl } from "@fider/services/url"import { useAllowedSchemesRegex } from "@fider/hooks"interface LinkInsertModalProps {diff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.tsindex 41dcc5cee..9bba59524 100644--- a/public/services/markdown.spec.ts+++ b/public/services/markdown.spec.ts@@ -86,50 +86,3 @@ testCases.forEach((x) => {expect(result).toEqual(x.expectedPlainText)})})--// Test the new URL validation utilities-describe("URL validation utilities", () => {- test("should always allow HTTP/HTTPS URLs even when no allowed schemes provided", () => {- // HTTP/HTTPS URLs are always allowed, even when no allowed schemes provided- expect(markdown.isValidUrl("https://example.com")).toBe(true)- expect(markdown.isValidUrl("http://example.com")).toBe(true)- expect(markdown.isValidUrl("example.com")).toBe(true) // gets normalized to https://-- // But crypto URLs are rejected when no allowed schemes provided- expect(markdown.isValidUrl("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2")).toBe(false)- })-- test("should validate URLs when allowed schemes provided", () => {- const mockAllowedSchemes = [new RegExp("^monero:[48]", "i"), new RegExp("^bitcoin:(1|3|bc1)", "i")]-- // HTTP/HTTPS URLs should work- expect(markdown.isValidUrl("https://example.com", mockAllowedSchemes)).toBe(true)- expect(markdown.isValidUrl("http://example.com", mockAllowedSchemes)).toBe(true)- expect(markdown.isValidUrl("example.com", mockAllowedSchemes)).toBe(true) // gets normalized to https://-- // Crypto URLs should work- expect(markdown.isValidUrl("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2", mockAllowedSchemes)).toBe(true)- expect(markdown.isValidUrl("bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", mockAllowedSchemes)).toBe(true)-- // Non-allowed crypto should be rejected- expect(markdown.isValidUrl("litecoin:LTC1qy2e3r4t5y6u7i8o9p0", mockAllowedSchemes)).toBe(false)- })-- test("should reject invalid URL schemes", () => {- const mockAllowedSchemes = [new RegExp("^https?://", "i")]- expect(markdown.isValidUrl("http:invalid-address", mockAllowedSchemes)).toBe(false)- })-- test("should reject empty URLs", () => {- expect(markdown.isValidUrl("")).toBe(false)- expect(markdown.isValidUrl(" ")).toBe(false)- })-- test("should normalize URLs correctly", () => {- const regularUrl = markdown.normalizeUrl("example.com")- expect(regularUrl).toBe("https://example.com")-- const cryptoUrl = markdown.normalizeUrl("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2")- expect(cryptoUrl).toBe("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2")- })-})diff --git a/public/services/markdown.ts b/public/services/markdown.tsindex bb29f5145..bfb6bd181 100644--- a/public/services/markdown.ts+++ b/public/services/markdown.ts@@ -101,34 +101,3 @@ export const plainText = (input: string): string => {const text = sanitize(marked(input, { renderer: plainTextRenderer }).trim())return decodeHtmlEntities(text).trim()}--/**- * Normalizes a URL, adding protocol if needed for regular URLs- * @param url The URL to normalize- * @returns The normalized URL- */-export const normalizeUrl = (url: string) => {- // Parse the scheme from the URL (e.g. https, mailto, monero, bitcoin, etc.)- const trimmedUrl = url.trim()- const scheme = trimmedUrl.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/)?.[1]?.toLowerCase()- // For URLs without a scheme, add https://, otherwise return the URL as is- return scheme ? trimmedUrl : `https://${trimmedUrl}`-}--/**- * URL validation with allowed schemes- * @param url The URL to validate- * @param allowedSchemesRegex Array of RegExp patterns for allowed schemes- * @returns true if valid, false otherwise- */-export const isValidUrl = (url: string, allowedSchemesRegex?: RegExp[]): boolean => {- // Return false if the URL is empty- if (!url || !url.trim()) {- return false- }- // Normalize the URL- const normalizedUrl = normalizeUrl(url)- // Check URL against allowed schemes, always including https?://- const allowed = [new RegExp("^https?://", "i"), ...(allowedSchemesRegex || [])]- return allowed.some((pattern) => pattern.test(normalizedUrl))-}diff --git a/public/services/url.spec.ts b/public/services/url.spec.tsnew file mode 100644index 000000000..5c61fb61e--- /dev/null+++ b/public/services/url.spec.ts@@ -0,0 +1,48 @@+import * as url from "./url"++// Test the URL validation utilities+describe("URL validation utilities", () => {+ test("should always allow HTTP/HTTPS URLs even when no allowed schemes provided", () => {+ // HTTP/HTTPS URLs are always allowed, even when no allowed schemes provided+ expect(url.isValidUrl("https://example.com")).toBe(true)+ expect(url.isValidUrl("http://example.com")).toBe(true)+ expect(url.isValidUrl("example.com")).toBe(true) // gets normalized to https://++ // But crypto URLs are rejected when no allowed schemes provided+ expect(url.isValidUrl("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2")).toBe(false)+ })++ test("should validate URLs when allowed schemes provided", () => {+ const mockAllowedSchemes = [new RegExp("^monero:[48]", "i"), new RegExp("^bitcoin:(1|3|bc1)", "i")]++ // HTTP/HTTPS URLs should work+ expect(url.isValidUrl("https://example.com", mockAllowedSchemes)).toBe(true)+ expect(url.isValidUrl("http://example.com", mockAllowedSchemes)).toBe(true)+ expect(url.isValidUrl("example.com", mockAllowedSchemes)).toBe(true) // gets normalized to https://++ // Crypto URLs should work+ expect(url.isValidUrl("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2", mockAllowedSchemes)).toBe(true)+ expect(url.isValidUrl("bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", mockAllowedSchemes)).toBe(true)++ // Non-allowed crypto should be rejected+ expect(url.isValidUrl("litecoin:LTC1qy2e3r4t5y6u7i8o9p0", mockAllowedSchemes)).toBe(false)+ })++ test("should reject invalid URL schemes", () => {+ const mockAllowedSchemes = [new RegExp("^https?://", "i")]+ expect(url.isValidUrl("http:invalid-address", mockAllowedSchemes)).toBe(false)+ })++ test("should reject empty URLs", () => {+ expect(url.isValidUrl("")).toBe(false)+ expect(url.isValidUrl(" ")).toBe(false)+ })++ test("should normalize URLs correctly", () => {+ const regularUrl = url.normalizeUrl("example.com")+ expect(regularUrl).toBe("https://example.com")++ const cryptoUrl = url.normalizeUrl("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2")+ expect(cryptoUrl).toBe("monero:4AdUndXHHZ6cFhR4L5n4eL3p5N1L7cKyK4n8v7f3v2")+ })+})diff --git a/public/services/url.ts b/public/services/url.tsnew file mode 100644index 000000000..e6dfd6d9d--- /dev/null+++ b/public/services/url.ts@@ -0,0 +1,30 @@+/**+ * Normalizes a URL, adding protocol if needed for regular URLs+ * @param url The URL to normalize+ * @returns The normalized URL+ */+export const normalizeUrl = (url: string) => {+ // Parse the scheme from the URL (e.g. https, mailto, monero, bitcoin, etc.)+ const trimmedUrl = url.trim()+ const scheme = trimmedUrl.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/)?.[1]?.toLowerCase()+ // For URLs without a scheme, add https://, otherwise return the URL as is+ return scheme ? trimmedUrl : `https://${trimmedUrl}`+}++/**+ * URL validation with allowed schemes+ * @param url The URL to validate+ * @param allowedSchemesRegex Array of RegExp patterns for allowed schemes+ * @returns true if valid, false otherwise+ */+export const isValidUrl = (url: string, allowedSchemesRegex?: RegExp[]): boolean => {+ // Return false if the URL is empty+ if (!url || !url.trim()) {+ return false+ }+ // Normalize the URL+ const normalizedUrl = normalizeUrl(url)+ // Check URL against allowed schemes, always including https?://+ const allowed = [new RegExp("^https?://", "i"), ...(allowedSchemesRegex || [])]+ return allowed.some((pattern) => pattern.test(normalizedUrl))+}