Skip to content

Added isEqual() to uri helper #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/helpers/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ export function toURL(value: unknown): URL | undefined {
return undefined;
}
}

/**
* Compares two URLs or strings for equality, normalizing them by removing trailing slashes
*/
export function isEqual(a: URL | string, b: URL | string): boolean {
if (a instanceof URL) return isEqual(a.href, b);
if (b instanceof URL) return isEqual(a, b.href);

return a.replace(/\/+$/, '') === b.replace(/\/+$/, '');
}
49 changes: 48 additions & 1 deletion src/helpers/uri.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { isUri, toURL } from './uri';
import { isEqual, isUri, toURL } from './uri';

describe('isUri', () => {
it('should return a boolean indicating if the provided string is a valid URI', () => {
Expand Down Expand Up @@ -28,3 +28,50 @@ describe('toURL', () => {
expect(toURL('://example.com/user/foo')).toBeUndefined();
});
});

describe('isEqual', () => {
it('should compare string URLs correctly', () => {
expect(isEqual('https://example.com', 'https://example.com/')).toBe(
true,
);
expect(isEqual('https://example.com', 'https://example.com')).toBe(
true,
);
expect(isEqual('https://example.com/', 'https://example.com/')).toBe(
true,
);
expect(isEqual('https://example.com', 'https://example.org')).toBe(
false,
);
});

it('should compare URL objects correctly', () => {
const url1 = new URL('https://example.com');
const url2 = new URL('https://example.com/');
const url3 = new URL('https://example.org');

expect(isEqual(url1, url1)).toBe(true);
expect(isEqual(url1, url2)).toBe(true);
expect(isEqual(url2, url3)).toBe(false);
expect(isEqual(url1, url3)).toBe(false);
});

it('should compare mixed URL objects and strings correctly', () => {
const url = new URL('https://example.com');

expect(isEqual(url, 'https://example.com/')).toBe(true);
expect(isEqual(url, 'https://example.org')).toBe(false);
});

it('should handle URLs with paths', () => {
expect(
isEqual('https://example.com/path', 'https://example.com/path/'),
).toBe(true);
expect(
isEqual(
'https://example.com/path',
'https://example.com/other-path',
),
).toBe(false);
});
});
14 changes: 7 additions & 7 deletions src/post/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { htmlToText } from 'html-to-text';
import linkifyHtml from 'linkify-html';
import { parse } from 'node-html-parser';
import { HANDLE_REGEX } from '../constants';
import { isEqual } from '../helpers/uri';
import type { Mention } from './post.entity';

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

for (const mention of mentions) {
const apId = mention.account.apId
.toString()
.replace(/\/+$/, '');
const url = mention.account.url.toString().replace(/\/+$/, '');

// Find all links that matches the mentioned account
for (const link of links) {
const href = link.getAttribute('href')?.replace(/\/+$/, '');
if (href === apId || href === url) {
const href = link.getAttribute('href');
if (
href &&
(isEqual(mention.account.apId, href) ||
isEqual(mention.account.url, href))
) {
// Update the link attributes
link.setAttribute(
'data-profile',
Expand Down