TypeScript types and utility functions for handling Web Annotations.

Readme.md 5.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # web-annotation-utils
  2. TypeScript types and utility functions for handling Web Annotations.
  3. A Web Annotation, as defined by the [Web Annotation Data Model][] is basically a JSON object with a target (what the note is ‘about’) and a body (the note ‘content’); along with optional metadata (creator, date, motivation, etc.). This module provides a TypeScript type declaration for Web Annotations.
  4. Besides the type definition, it provides convenience functions for dealing with Web Annotations, such as getting the URL(s) of pages an annotation targets, or the plain text content the annotation body. It aims to provide some basic tools to get started writing interoperable annotation tools without having to deal with the intricacies of the data model.
  5. [Web Annotation Data Model]: https://www.w3.org/TR/annotation-model/
  6. ## Usage
  7. Install, using npm or equivalent:
  8. ```
  9. npm install git+https://code.treora.com/gerben/web-annotation-utils#latest
  10. ```
  11. Usage example:
  12. ```
  13. import type { WebAnnotation } from 'web-annotation-utils';
  14. import { completeAnnotationStub, getTargetUrls } from 'web-annotation-utils';
  15. const webAnnotation: WebAnnotation = completeAnnotationStub({
  16. "bodyValue": "interesting.",
  17. "target": [
  18. "http://example.org/page1",
  19. {
  20. "source": "http://example.org/page2",
  21. "selector": {
  22. "type": "TextQuoteSelector",
  23. "exact": "bla bla bla",
  24. }
  25. }
  26. ]
  27. });
  28. const targetUrls = getTargetUrls(webAnnotation.target);
  29. // [ 'http://example.org/page1', 'http://example.org/page2' ]
  30. ```
  31. ## Types
  32. See [WebAnnotation.ts](./WebAnnotation.ts).
  33. <!-- TSDOC_START -->
  34. ## :toolbox: Functions
  35. - [asArray](#gear-asarray)
  36. - [asSingleValue](#gear-assinglevalue)
  37. - [findTargetsInDocument](#gear-findtargetsindocument)
  38. - [findTargetInDocument](#gear-findtargetindocument)
  39. - [completeAnnotationStub](#gear-completeannotationstub)
  40. - [getSingleCreatorName](#gear-getsinglecreatorname)
  41. - [targetsUrl](#gear-targetsurl)
  42. - [getTargetUrls](#gear-gettargeturls)
  43. - [getTargetUrl](#gear-gettargeturl)
  44. - [getTargetQuotes](#gear-gettargetquotes)
  45. - [getTargetQuote](#gear-gettargetquote)
  46. ### :gear: asArray
  47. | Function | Type |
  48. | ---------- | ---------- |
  49. | `asArray` | `<T>(value: ZeroOrMore<T>) => T[]` |
  50. ### :gear: asSingleValue
  51. | Function | Type |
  52. | ---------- | ---------- |
  53. | `asSingleValue` | `<T>(value: ZeroOrMore<T>) => T` |
  54. ### :gear: findTargetsInDocument
  55. Find the Elements and/or Ranges in the document the annotation targets, if
  56. any.
  57. This supports the following selector types:
  58. - CssSelector
  59. - TextQuoteSelector
  60. - TextPositionSelector
  61. - RangeSelector
  62. | Function | Type |
  63. | ---------- | ---------- |
  64. | `findTargetsInDocument` | `(target: OneOrMore<Target>, document?: Document) => Promise<DomMatch[]>` |
  65. ### :gear: findTargetInDocument
  66. Find the Elements and/or Ranges in the document the annotation targets, if
  67. any, given a single target.
  68. This supports the following selector types:
  69. - CssSelector
  70. - TextQuoteSelector
  71. - TextPositionSelector
  72. - RangeSelector
  73. | Function | Type |
  74. | ---------- | ---------- |
  75. | `findTargetInDocument` | `(target: Target, document?: Document) => Promise<DomMatch[]>` |
  76. ### :gear: completeAnnotationStub
  77. Turn a partial annotation into a ‘well-formed’ WebAnnotation.
  78. It sets the following properties, if absent in the given stub:
  79. - `@context` as required
  80. - `type` as required, to `'Annotation'`
  81. - `created` as recommended (to the current time)
  82. - `target` to `'about:invalid'`
  83. | Function | Type |
  84. | ---------- | ---------- |
  85. | `completeAnnotationStub` | `(annotationStub: Partial<WebAnnotation>) => WebAnnotation` |
  86. ### :gear: getSingleCreatorName
  87. Get the name of the creator. If there are multiple, returns the first.
  88. Assumes the creator is a nested Agent object: if the creator a string
  89. (presumably the URL of an Agent node), `undefined` is returned.
  90. | Function | Type |
  91. | ---------- | ---------- |
  92. | `getSingleCreatorName` | `(annotationOrBody: WebAnnotation or BodyObject) => string` |
  93. ### :gear: targetsUrl
  94. Check whether the annotation likely targets the given URL.
  95. The word “likely” is used because, in its comparison, this ignores the URL
  96. scheme, fragment and query parameters.
  97. Note that, strictly speaking, a URL should be treated as an opaque string.
  98. In practice, it may however be useful to consider URLs as ‘likely equivalent’
  99. in order to apply annotations targeting one URL to the document with the
  100. very similar URL. Apply with caution: Especially a different query may,
  101. depending on the website at hand, result in very different documents.
  102. | Function | Type |
  103. | ---------- | ---------- |
  104. | `targetsUrl` | `(target: OneOrMore<Target>, url: string) => boolean` |
  105. ### :gear: getTargetUrls
  106. Get the URLs of the resources that the annotation targets, for all its
  107. targets.
  108. | Function | Type |
  109. | ---------- | ---------- |
  110. | `getTargetUrls` | `(target: OneOrMore<Target>) => string[]` |
  111. ### :gear: getTargetUrl
  112. Get the URL of the resource that the annotation targets, for a single
  113. target.
  114. | Function | Type |
  115. | ---------- | ---------- |
  116. | `getTargetUrl` | `(target: Target) => string` |
  117. ### :gear: getTargetQuotes
  118. Get the exact quotes that the annotation targets using a TextQuoteSelector,
  119. if any.
  120. | Function | Type |
  121. | ---------- | ---------- |
  122. | `getTargetQuotes` | `(target: OneOrMore<Target>) => string[]` |
  123. ### :gear: getTargetQuote
  124. Get the exact quote that a single target of an annotation targets using a
  125. TextQuoteSelector, if any.
  126. | Function | Type |
  127. | ---------- | ---------- |
  128. | `getTargetQuote` | `(target: Target) => string` |
  129. <!-- TSDOC_END -->
  130. ## Licence
  131. This is free and unencumbered software released into the public domain.