Skip to content

Commit 8fb6e8e

Browse files
committed
Added isEqual() to uri helper
- Added a method that compares two URLs or strings for equality, normalizing them by removing trailing slashes
1 parent 5b3faae commit 8fb6e8e

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/helpers/uri.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ export function toURL(value: unknown): URL | undefined {
2121
return undefined;
2222
}
2323
}
24+
25+
/**
26+
* Compares two URLs or strings for equality, normalizing them by removing trailing slashes
27+
* and slashes before query parameters.
28+
*/
29+
export function isEqual(a: URL | string, b: URL | string): boolean {
30+
if (a instanceof URL) return isEqual(a.href, b);
31+
if (b instanceof URL) return isEqual(a, b.href);
32+
33+
return a.replace(/\/+$/, '') === b.replace(/\/+$/, '');
34+
}

src/helpers/uri.unit.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { isUri, toURL } from './uri';
3+
import { isEqual, isUri, toURL } from './uri';
44

55
describe('isUri', () => {
66
it('should return a boolean indicating if the provided string is a valid URI', () => {
@@ -28,3 +28,50 @@ describe('toURL', () => {
2828
expect(toURL('://example.com/user/foo')).toBeUndefined();
2929
});
3030
});
31+
32+
describe('isEqual', () => {
33+
it('should compare string URLs correctly', () => {
34+
expect(isEqual('https://example.com', 'https://example.com/')).toBe(
35+
true,
36+
);
37+
expect(isEqual('https://example.com', 'https://example.com')).toBe(
38+
true,
39+
);
40+
expect(isEqual('https://example.com/', 'https://example.com/')).toBe(
41+
true,
42+
);
43+
expect(isEqual('https://example.com', 'https://example.org')).toBe(
44+
false,
45+
);
46+
});
47+
48+
it('should compare URL objects correctly', () => {
49+
const url1 = new URL('https://example.com');
50+
const url2 = new URL('https://example.com/');
51+
const url3 = new URL('https://example.org');
52+
53+
expect(isEqual(url1, url1)).toBe(true);
54+
expect(isEqual(url1, url2)).toBe(true);
55+
expect(isEqual(url2, url3)).toBe(false);
56+
expect(isEqual(url1, url3)).toBe(false);
57+
});
58+
59+
it('should compare mixed URL objects and strings correctly', () => {
60+
const url = new URL('https://example.com');
61+
62+
expect(isEqual(url, 'https://example.com/')).toBe(true);
63+
expect(isEqual(url, 'https://example.org')).toBe(false);
64+
});
65+
66+
it('should handle URLs with paths', () => {
67+
expect(
68+
isEqual('https://example.com/path', 'https://example.com/path/'),
69+
).toBe(true);
70+
expect(
71+
isEqual(
72+
'https://example.com/path',
73+
'https://example.com/other-path',
74+
),
75+
).toBe(false);
76+
});
77+
});

src/post/content.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { htmlToText } from 'html-to-text';
22
import linkifyHtml from 'linkify-html';
33
import { parse } from 'node-html-parser';
44
import { HANDLE_REGEX } from '../constants';
5+
import { isEqual } from '../helpers/uri';
56
import type { Mention } from './post.entity';
67

78
/**
@@ -221,15 +222,14 @@ export class ContentPreparer {
221222
const links = html.querySelectorAll('a');
222223

223224
for (const mention of mentions) {
224-
const apId = mention.account.apId
225-
.toString()
226-
.replace(/\/+$/, '');
227-
const url = mention.account.url.toString().replace(/\/+$/, '');
228-
229225
// Find all links that matches the mentioned account
230226
for (const link of links) {
231-
const href = link.getAttribute('href')?.replace(/\/+$/, '');
232-
if (href === apId || href === url) {
227+
const href = link.getAttribute('href');
228+
if (
229+
href &&
230+
(isEqual(mention.account.apId, href) ||
231+
isEqual(mention.account.url, href))
232+
) {
233233
// Update the link attributes
234234
link.setAttribute(
235235
'data-profile',

0 commit comments

Comments
 (0)