Skip to content

Commit 23ef326

Browse files
vNovskiFrancescoMolinaro
authored andcommitted
[DSC-1484] feature: added internal link service
1 parent 094464b commit 23ef326

4 files changed

Lines changed: 10 additions & 102 deletions

File tree

src/app/core/cache/builders/link.service.spec.ts

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -251,55 +251,4 @@ describe('LinkService', () => {
251251
});
252252
});
253253

254-
describe('User Functions', () => {
255-
fdescribe('isLinkInternal', () => {
256-
it('should return true for internal link starting with "/"', () => {
257-
result = service.isLinkInternal('/my-link', 'https://currentdomain');
258-
expect(result).toBe(true);
259-
});
260-
261-
it('should return true for internal link starting with currentURL', () => {
262-
result = service.isLinkInternal('https://currentdomain/my-link', 'https://currentdomain');
263-
expect(result).toBe(true);
264-
});
265-
266-
it('should return true for internal link starting with "currentdomain"', () => {
267-
result = service.isLinkInternal('currentdomain/my-link', 'https://currentdomain');
268-
expect(result).toBe(true);
269-
});
270-
271-
it('should return false for external link', () => {
272-
result = service.isLinkInternal('https://externaldomain/my-link', 'https://currentdomain');
273-
expect(result).toBe(false);
274-
});
275-
276-
it('should return true for internal link link without leading "/"', () => {
277-
result = service.isLinkInternal('my-link', 'https://currentdomain');
278-
expect(result).toBe(true);
279-
});
280-
});
281-
282-
fdescribe('transformInternalLink', () => {
283-
it('should transform internal link by removing currentURL', () => {
284-
result = service.transformInternalLink('https://currentdomain/my-link', 'https://currentdomain');
285-
expect(result).toBe('/my-link');
286-
});
287-
288-
it('should transform internal link by adding leading "/" if missing', () => {
289-
result = service.transformInternalLink('currentdomain/my-link', 'https://currentdomain');
290-
expect(result).toBe('/my-link');
291-
});
292-
293-
it('should return unchanged link for external link', () => {
294-
result = service.transformInternalLink('https://externalDomain/my-link', 'https://currentdomain');
295-
expect(result).toBe('https://externalDomain/my-link');
296-
});
297-
298-
it('should return unchanged link for internal link with leading "/"', () => {
299-
result = service.transformInternalLink('/my-link', 'https://currentdomain');
300-
expect(result).toBe('/my-link');
301-
});
302-
});
303-
});
304-
305254
});

src/app/core/cache/builders/link.service.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -123,47 +123,4 @@ export class LinkService {
123123
return result;
124124
}
125125

126-
/**
127-
* Check if the provided link is internal, i.e., it starts with a '/' or matches the current URL.
128-
*
129-
* @param link The link to be checked.
130-
* @param currentURL The current URL to compare against.
131-
* @returns A boolean indicating whether the link is internal.
132-
*/
133-
public isLinkInternal(link: string, currentURL: string): boolean {
134-
// Create a Domain object for the provided link
135-
const currentDomain = new URL(currentURL).hostname;
136-
137-
return link.startsWith('/')
138-
|| link.startsWith(currentURL)
139-
|| link.startsWith(currentDomain)
140-
|| link === currentDomain
141-
|| !link.includes('://');
142-
}
143-
144-
/**
145-
* Transform an internal link based on the current URL.
146-
*
147-
* @param link The link to be transformed.
148-
* @param currentURL The current URL used for transformation.
149-
* @returns The transformed internal link.
150-
*/
151-
public transformInternalLink(link: string, currentURL: string): string {
152-
// Create a Domain object for the provided link
153-
const currentDomain = new URL(currentURL).hostname;
154-
155-
if (link.startsWith(currentURL)) {
156-
const currentSegments = link.substring(currentURL.length);
157-
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
158-
}
159-
160-
if (link.startsWith(currentDomain)) {
161-
const currentSegments = link.substring(currentDomain.length);
162-
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
163-
}
164-
165-
return link;
166-
167-
}
168-
169126
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ describe('InternalLinkService', () => {
4747

4848
describe('transformInternalLink', () => {
4949
it('should transform internal link by removing currentURL', () => {
50-
const result = service.getRelativePath('https://currentdomain/my-link');
50+
const result = service.transformInternalLink('https://currentdomain/my-link');
5151
expect(result).toBe('/my-link');
5252
});
5353

5454
it('should transform internal link by adding leading "/" if missing', () => {
55-
const result = service.getRelativePath('currentdomain/my-link');
55+
const result = service.transformInternalLink('currentdomain/my-link');
5656
expect(result).toBe('/my-link');
5757
});
5858

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

6464
it('should return unchanged link for internal link with leading "/"', () => {
65-
const result = service.getRelativePath('/my-link');
65+
const result = service.transformInternalLink('/my-link');
6666
expect(result).toBe('/my-link');
6767
});
6868
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class InternalLinkService {
1919
* Check if the provided link is internal, i.e., it starts with a '/' or matches the current URL.
2020
*
2121
* @param link The link to be checked.
22+
* @param currentURL The current URL to compare against.
2223
* @returns A boolean indicating whether the link is internal.
2324
*/
2425
public isLinkInternal(link: string): boolean {
@@ -33,12 +34,13 @@ export class InternalLinkService {
3334
}
3435

3536
/**
36-
* Get the relative path for an internal link based on the current URL.
37+
* Transform an internal link based on the current URL.
3738
*
38-
* @param link The internal link to be transformed.
39-
* @returns The relative path for the given internal link.
39+
* @param link The link to be transformed.
40+
* @param currentURL The current URL used for transformation.
41+
* @returns The transformed internal link.
4042
*/
41-
public getRelativePath(link: string): string {
43+
public transformInternalLink(link: string): string {
4244
// Create a Domain object for the provided link
4345
const currentDomain = new URL(this.currentURL).hostname;
4446

0 commit comments

Comments
 (0)