Skip to content

Commit 07192b1

Browse files
Merged in task/dspace-cris-2023_02_x/DSC-1552 (pull request DSpace#1659)
Task/dspace cris 2023 02 x/DSC-1552
2 parents 63f5072 + 5cba3a3 commit 07192b1

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/app/core/services/internal-link.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('InternalLinkService', () => {
5858

5959
it('should return unchanged link for external link', () => {
6060
const result = service.getRelativePath('https://externalDomain/my-link');
61-
expect(result).toBe('https://externalDomain/my-link');
61+
expect(result).toBe('/https://externalDomain/my-link');
6262
});
6363

6464
it('should return unchanged link for internal link with leading "/"', () => {

src/app/core/services/internal-link.service.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,41 @@ export class InternalLinkService {
3939
* @returns The relative path for the given internal link.
4040
*/
4141
public getRelativePath(link: string): string {
42-
// Create a Domain object for the provided link
42+
// Obtaining the base URL, disregarding query parameters
43+
const baseUrl = link.split('?')[0];
4344
const currentDomain = new URL(this.currentURL).hostname;
4445

45-
if (link.startsWith(this.currentURL)) {
46-
const currentSegments = link.substring(this.currentURL.length);
46+
if (baseUrl.startsWith(this.currentURL) || baseUrl.startsWith(currentDomain)) {
47+
const base = baseUrl.startsWith(this.currentURL) ? this.currentURL : currentDomain;
48+
const currentSegments = baseUrl.substring(base.length);
4749
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
4850
}
4951

50-
if (link.startsWith(currentDomain)) {
51-
const currentSegments = link.substring(currentDomain.length);
52-
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
52+
return baseUrl.startsWith('/') ? baseUrl : `/${baseUrl}`;
53+
}
54+
55+
/**
56+
* Parse the query parameters from a given URL link.
57+
*
58+
* @param link The URL link containing query parameters.
59+
* @returns An object containing the parsed query parameters.
60+
*/
61+
public getQueryParams(link: string): Record<string, string> {
62+
const queryParams: Record<string, string> = {};
63+
64+
const queryStringStartIndex = link.indexOf('?');
65+
if (queryStringStartIndex !== -1) {
66+
const paramsString = link.substring(queryStringStartIndex + 1);
67+
const paramsArray = paramsString.split('&');
68+
69+
paramsArray.forEach(param => {
70+
const [key, value] = param.split('=');
71+
if (key && value) {
72+
queryParams[key] = decodeURIComponent(value.replace(/\+/g, ' '));
73+
}
74+
});
5375
}
5476

55-
return link;
77+
return queryParams;
5678
}
5779
}

0 commit comments

Comments
 (0)