-
Notifications
You must be signed in to change notification settings - Fork 1
Add helpers for AnnotationDocumentInfo #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| export type Annotation = { | ||
| uri: string; | ||
| document: { title: string }; | ||
| links: { | ||
| /** A "bouncer" URL that takes the user to see the annotation in context */ | ||
| incontext?: string; | ||
| /** URL to view the annotation by itself. */ | ||
| html?: string; | ||
| }; | ||
| }; | ||
|
|
||
| export type DocumentMetadata = { | ||
| uri: string; | ||
| domain: string; | ||
| title: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Extract document metadata from an annotation. | ||
| */ | ||
| export function documentMetadata(annotation: Annotation): DocumentMetadata { | ||
| const uri = annotation.uri; | ||
|
|
||
| let domain; | ||
| try { | ||
| domain = new URL(uri).hostname; | ||
| } catch { | ||
| // Annotation URI parsing on the backend is very liberal compared to the URL | ||
| // constructor. There is also some historic invalid data in h (eg [1]). | ||
| // Hence, we must handle URL parsing failures in the client. | ||
| // | ||
| // [1] https://github.com/hypothesis/client/issues/3666 | ||
| domain = ''; | ||
| } | ||
| if (domain === 'localhost') { | ||
| domain = ''; | ||
| } | ||
|
|
||
| let title = domain; | ||
| if (annotation.document && annotation.document.title) { | ||
| title = annotation.document.title[0]; | ||
| } | ||
|
|
||
| return { uri, domain, title }; | ||
| } | ||
|
|
||
| export type DomainAndTitle = { | ||
| domain: string; | ||
| titleText: string; | ||
| titleLink: string | null; | ||
| }; | ||
|
|
||
| /** | ||
| * Return the domain and title of an annotation for display on an annotation | ||
| * card. | ||
| */ | ||
| export function domainAndTitle(annotation: Annotation): DomainAndTitle { | ||
| return { | ||
| domain: domainTextFromAnnotation(annotation), | ||
| titleText: titleTextFromAnnotation(annotation), | ||
| titleLink: titleLinkFromAnnotation(annotation), | ||
| }; | ||
| } | ||
|
|
||
| function titleLinkFromAnnotation(annotation: Annotation): string | null { | ||
| let titleLink: string | null = annotation.uri; | ||
|
|
||
| if ( | ||
| titleLink && | ||
| !(titleLink.indexOf('http://') === 0 || titleLink.indexOf('https://') === 0) | ||
| ) { | ||
| // We only link to http(s) URLs. | ||
| titleLink = null; | ||
| } | ||
|
|
||
| if (annotation.links && annotation.links.incontext) { | ||
| titleLink = annotation.links.incontext; | ||
| } | ||
|
|
||
| return titleLink; | ||
| } | ||
| /** | ||
| * Returns the domain text from an annotation. | ||
| */ | ||
| function domainTextFromAnnotation(annotation: Annotation): string { | ||
| const document = documentMetadata(annotation); | ||
|
|
||
| let domainText = ''; | ||
| if (document.uri && document.uri.indexOf('file://') === 0 && document.title) { | ||
| const parts = document.uri.split('/'); | ||
| const filename = parts[parts.length - 1]; | ||
| if (filename) { | ||
| domainText = filename; | ||
| } | ||
| } else if (document.domain && document.domain !== document.title) { | ||
| domainText = document.domain; | ||
| } | ||
|
|
||
| return domainText; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the title text from an annotation and crops it to 30 chars | ||
| * if needed. | ||
| */ | ||
| function titleTextFromAnnotation(annotation: Annotation): string { | ||
| const document = documentMetadata(annotation); | ||
|
|
||
| let titleText = document.title; | ||
| if (titleText.length > 30) { | ||
| titleText = titleText.slice(0, 30) + '…'; | ||
| } | ||
|
|
||
| return titleText; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| export { documentMetadata } from './annotation-metadata'; | ||
| export { processAndReplaceMentionElements } from './mentions'; | ||
|
|
||
| export type { DocumentMetadata, DomainAndTitle } from './annotation-metadata'; | ||
| export type { Group, GroupType } from './groups'; | ||
| export type { Mention, MentionMode, InvalidMentionContent } from './mentions'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created this type as small as possible, just with the minimum properties that are needed by the helpers defined here.
In future we plan to extract the type definitions to a shared location.