|
| 1 | +export type Annotation = { |
| 2 | + uri: string; |
| 3 | + document: { title: string }; |
| 4 | + links: { |
| 5 | + /** A "bouncer" URL that takes the user to see the annotation in context */ |
| 6 | + incontext?: string; |
| 7 | + /** URL to view the annotation by itself. */ |
| 8 | + html?: string; |
| 9 | + }; |
| 10 | +}; |
| 11 | + |
| 12 | +export type DocumentMetadata = { |
| 13 | + uri: string; |
| 14 | + domain: string; |
| 15 | + title: string; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Extract document metadata from an annotation. |
| 20 | + */ |
| 21 | +export function documentMetadata(annotation: Annotation): DocumentMetadata { |
| 22 | + const uri = annotation.uri; |
| 23 | + |
| 24 | + let domain; |
| 25 | + try { |
| 26 | + domain = new URL(uri).hostname; |
| 27 | + } catch { |
| 28 | + // Annotation URI parsing on the backend is very liberal compared to the URL |
| 29 | + // constructor. There is also some historic invalid data in h (eg [1]). |
| 30 | + // Hence, we must handle URL parsing failures in the client. |
| 31 | + // |
| 32 | + // [1] https://github.com/hypothesis/client/issues/3666 |
| 33 | + domain = ''; |
| 34 | + } |
| 35 | + if (domain === 'localhost') { |
| 36 | + domain = ''; |
| 37 | + } |
| 38 | + |
| 39 | + let title = domain; |
| 40 | + if (annotation.document && annotation.document.title) { |
| 41 | + title = annotation.document.title[0]; |
| 42 | + } |
| 43 | + |
| 44 | + return { uri, domain, title }; |
| 45 | +} |
| 46 | + |
| 47 | +export type DomainAndTitle = { |
| 48 | + domain: string; |
| 49 | + titleText: string; |
| 50 | + titleLink: string | null; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Return the domain and title of an annotation for display on an annotation |
| 55 | + * card. |
| 56 | + */ |
| 57 | +export function domainAndTitle(annotation: Annotation): DomainAndTitle { |
| 58 | + return { |
| 59 | + domain: domainTextFromAnnotation(annotation), |
| 60 | + titleText: titleTextFromAnnotation(annotation), |
| 61 | + titleLink: titleLinkFromAnnotation(annotation), |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +function titleLinkFromAnnotation(annotation: Annotation): string | null { |
| 66 | + let titleLink: string | null = annotation.uri; |
| 67 | + |
| 68 | + if ( |
| 69 | + titleLink && |
| 70 | + !(titleLink.indexOf('http://') === 0 || titleLink.indexOf('https://') === 0) |
| 71 | + ) { |
| 72 | + // We only link to http(s) URLs. |
| 73 | + titleLink = null; |
| 74 | + } |
| 75 | + |
| 76 | + if (annotation.links && annotation.links.incontext) { |
| 77 | + titleLink = annotation.links.incontext; |
| 78 | + } |
| 79 | + |
| 80 | + return titleLink; |
| 81 | +} |
| 82 | +/** |
| 83 | + * Returns the domain text from an annotation. |
| 84 | + */ |
| 85 | +function domainTextFromAnnotation(annotation: Annotation): string { |
| 86 | + const document = documentMetadata(annotation); |
| 87 | + |
| 88 | + let domainText = ''; |
| 89 | + if (document.uri && document.uri.indexOf('file://') === 0 && document.title) { |
| 90 | + const parts = document.uri.split('/'); |
| 91 | + const filename = parts[parts.length - 1]; |
| 92 | + if (filename) { |
| 93 | + domainText = filename; |
| 94 | + } |
| 95 | + } else if (document.domain && document.domain !== document.title) { |
| 96 | + domainText = document.domain; |
| 97 | + } |
| 98 | + |
| 99 | + return domainText; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Returns the title text from an annotation and crops it to 30 chars |
| 104 | + * if needed. |
| 105 | + */ |
| 106 | +function titleTextFromAnnotation(annotation: Annotation): string { |
| 107 | + const document = documentMetadata(annotation); |
| 108 | + |
| 109 | + let titleText = document.title; |
| 110 | + if (titleText.length > 30) { |
| 111 | + titleText = titleText.slice(0, 30) + '…'; |
| 112 | + } |
| 113 | + |
| 114 | + return titleText; |
| 115 | +} |
0 commit comments