diff --git a/src/app/shared/clarin-shared-util.spec.ts b/src/app/shared/clarin-shared-util.spec.ts index 8404d87990a..2775e2944df 100644 --- a/src/app/shared/clarin-shared-util.spec.ts +++ b/src/app/shared/clarin-shared-util.spec.ts @@ -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 http://hdl.handle.net/11234/1-5813.'); + expect(makeLinks('see https://github.com/ufal/perin, then')).toBe( + 'see https://github.com/ufal/perin, then'); + }); + + it('keeps punctuation inside the URL', () => { + expect(makeLinks('https://example.org/a.b?x=1;y=2#c')).toBe( + 'https://example.org/a.b?x=1;y=2#c'); + }); + + 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/ and https://example.org/?a=1&. 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 www.example.org.'); + }); + }); + describe('buildAuthoritySearchFilter', () => { it('uses the authority operator and key when an authority is present', () => { expect(buildAuthoritySearchFilter('publisher', { value: 'ACME Press', authority: '02mhbdp94' })) diff --git a/src/app/shared/clarin-shared-util.ts b/src/app/shared/clarin-shared-util.ts index 1afe3c21adb..9d76bdda936 100644 --- a/src/app/shared/clarin-shared-util.ts +++ b/src/app/shared/clarin-shared-util.ts @@ -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) => `${url}`); + 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. "&") 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 `${url}${trailing}`; + }); } /**