Skip to content
Open
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
32 changes: 31 additions & 1 deletion src/app/shared/clarin-shared-util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { buildAuthoritySearchFilter, convertMetadataFieldIntoSearchType } from './clarin-shared-util';
import { buildAuthoritySearchFilter, convertMetadataFieldIntoSearchType, makeLinks } from './clarin-shared-util';

describe('clarin-shared-util', () => {
describe('makeLinks', () => {
it('does not include sentence-ending punctuation in the link', () => {
expect(makeLinks('Consolidated 2.0 http://hdl.handle.net/11234/1-5813.')).toBe(
'Consolidated 2.0 <a href="http://hdl.handle.net/11234/1-5813" target="_blank">http://hdl.handle.net/11234/1-5813</a>.');
expect(makeLinks('see https://github.com/ufal/perin, then')).toBe(
'see <a href="https://github.com/ufal/perin" target="_blank">https://github.com/ufal/perin</a>, then');
});

it('keeps punctuation inside the URL', () => {
expect(makeLinks('https://example.org/a.b?x=1;y=2#c')).toBe(
'<a href="https://example.org/a.b?x=1;y=2#c" target="_blank">https://example.org/a.b?x=1;y=2#c</a>');
});

it('keeps the semicolon of a trailing HTML character reference in the link', () => {
const container = document.createElement('div');
container.innerHTML = makeLinks('see https://example.org/a&sol; and https://example.org/?a=1&amp;. Done');
const anchors = container.querySelectorAll('a');
expect(anchors[0].getAttribute('href')).toBe('https://example.org/a/');
expect(anchors[0].textContent).toBe('https://example.org/a/');
expect(anchors[1].getAttribute('href')).toBe('https://example.org/?a=1&');
expect(anchors[1].textContent).toBe('https://example.org/?a=1&');
expect(container.textContent).toBe('see https://example.org/a/ and https://example.org/?a=1&. Done');
});

it('links www. addresses as absolute URLs', () => {
expect(makeLinks('Visit www.example.org.')).toBe(
'Visit <a href="http://www.example.org" target="_blank">www.example.org</a>.');
});
});

describe('buildAuthoritySearchFilter', () => {
it('uses the authority operator and key when an authority is present', () => {
expect(buildAuthoritySearchFilter('publisher', { value: 'ACME Press', authority: '02mhbdp94' }))
Expand Down
13 changes: 12 additions & 1 deletion src/app/shared/clarin-shared-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ export function loadItemAuthors(item, itemAuthors, baseUrl, fields) {
export function makeLinks(text: string): string {
// Use a regular expression to find URLs and convert them into clickable links
const regex = /(?:https?|ftp):\/\/[^\s)]+|www\.[^\s)]+/g;
return text?.replace(regex, (url) => `<a href="${url}" target="_blank">${url}</a>`);
return text?.replace(regex, (match) => {
// Punctuation ending a sentence (e.g. "see http://hdl.handle.net/11234/1-5813.") is not part of the URL
let url = match.replace(/[.,;:!?'"]+$/, '');
let trailing = match.slice(url.length);
// The result is rendered through innerHTML, so a ';' closing a character reference (e.g. "&amp;") belongs to the URL
if (trailing.startsWith(';') && /&(?:#\d+|#x[\da-f]+|[a-z][a-z\d]*)$/i.test(url)) {
url += ';';
trailing = trailing.slice(1);
}
const href = url.startsWith('www.') ? `http://${url}` : url;
return `<a href="${href}" target="_blank">${url}</a>${trailing}`;
});
}

/**
Expand Down
Loading