Skip to content

Commit

Permalink
feat(utilities): markedSetNofollowLinks with hostname check (#492)
Browse files Browse the repository at this point in the history
Needed to solve apify/apify-core#18466.
Now the `rel="noopener noreferrer nofollow" target="_blank"` is added
only if url is outside from `apify.com` domain.
This enable passing hostname to `markedSetNofollowLinks` function and
setting `rel="noopener noreferrer" target="_blank"` for apify domain but
different hostname.

Once this is merged, next step would be to pass hostname from
`safeMarkdown` in console/frontend down to `markedSetNofollowLinks`
  • Loading branch information
MFori authored Dec 4, 2024
1 parent ed0f406 commit 59d746d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/utilities/src/utilities.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,25 @@ export function normalizeUrl(url: string, keepFragment?: boolean) {
}

// Helper function for markdown rendered marked
// Renders links outside apify.com in readme with rel="noopener noreferrer nofollow" and target="_blank" attributes
export function markedSetNofollowLinks(href: string, title: string, text: string) {
// If passed referrerHostname, it renders links outside that hostname in readme with rel="noopener noreferrer" and target="_blank" attributes
// And links outside apify.com in readme with rel="noopener noreferrer nofollow" and target="_blank" attributes
export function markedSetNofollowLinks(href: string, title: string, text: string, referrerHostname?: string) {
let urlParsed: URL;
try {
urlParsed = new URL(href);
} catch (e) {
// Probably invalid url, go on
}
const isApifyLink = (urlParsed! && /(\.|^)apify\.com$/i.test(urlParsed.hostname));
return (isApifyLink)
? `<a href="${href}">${title || text}</a>`
: `<a rel="noopener noreferrer nofollow" target="_blank" href="${href}">${title || text}</a>`;
const isSameHostname = !referrerHostname || (urlParsed! && urlParsed.hostname === referrerHostname);

if (isApifyLink && isSameHostname) {
return `<a href="${href}">${title || text}</a>`;
} if (isApifyLink) {
return `<a rel="noopener noreferrer" target="_blank" href="${href}">${title || text}</a>`;
}

return `<a rel="noopener noreferrer nofollow" target="_blank" href="${href}">${title || text}</a>`;
}

// Helper function for markdown rendered marked
Expand Down
43 changes: 43 additions & 0 deletions test/utilities.client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isBadForMongo,
jsonStringifyExtended,
JsonVariable,
markedSetNofollowLinks,
normalizeUrl,
splitFullName,
traverseObject,
Expand Down Expand Up @@ -1203,4 +1204,46 @@ describe('utilities.client', () => {
expect(splitFullName('More Spaces Between')).toEqual(['More', 'Spaces Between']);
});
});

describe('#markedSetNofollowLinks', () => {
it('should return a link without rel or target attributes for Apify links on the same hostname', () => {
const result = markedSetNofollowLinks('https://console.apify.com', 'Apify console', 'Apify Link', 'console.apify.com');
expect(result).toBe('<a href="https://console.apify.com">Apify console</a>');
});

it('should return a link with rel="noopener noreferrer" and target="_blank" for Apify links on a different hostname', () => {
const result = markedSetNofollowLinks('https://www.apify.com', 'Apify', 'Apify Link', 'different-hostname.com');
expect(result).toBe('<a rel="noopener noreferrer" target="_blank" href="https://www.apify.com">Apify</a>');
});

it('should return a link with rel="noopener noreferrer nofollow" and target="_blank" for non-Apify links', () => {
const result = markedSetNofollowLinks('https://www.example.com', 'Example', 'Example Link');
expect(result).toBe('<a rel="noopener noreferrer nofollow" target="_blank" href="https://www.example.com">Example</a>');
});

it('should return a link with rel="noopener noreferrer nofollow" and target="_blank" for invalid URLs', () => {
const result = markedSetNofollowLinks('invalid-url', 'Invalid', 'Invalid Link');
expect(result).toBe('<a rel="noopener noreferrer nofollow" target="_blank" href="invalid-url">Invalid</a>');
});

it('should handle a missing title and use the text instead', () => {
const result = markedSetNofollowLinks('https://www.apify.com', '', 'Apify Link', 'www.apify.com');
expect(result).toBe('<a href="https://www.apify.com">Apify Link</a>');
});

it('should handle a missing hostname parameter', () => {
const result = markedSetNofollowLinks('https://www.apify.com', 'Apify', 'Apify Link');
expect(result).toBe('<a href="https://www.apify.com">Apify</a>');
});

it('should treat subdomains of apify.com as Apify links', () => {
const result = markedSetNofollowLinks('https://docs.apify.com', 'Docs', 'Docs Link');
expect(result).toBe('<a href="https://docs.apify.com">Docs</a>');
});

it('should apply rel="noopener noreferrer nofollow" for links with an undefined hostname and non-Apify URLs', () => {
const result = markedSetNofollowLinks('https://example.com', 'Example', 'Example Link', undefined);
expect(result).toBe('<a rel="noopener noreferrer nofollow" target="_blank" href="https://example.com">Example</a>');
});
});
});

0 comments on commit 59d746d

Please sign in to comment.