TypeScript types and utility functions for handling Web Annotations.

web-annotation-utils/lib/ wa-attribute-utils.d.ts
55 lines
2.3 KiB

  1. import type { BodyObject, WebAnnotation } from './WebAnnotation.js';
  2. import { OnlyOne } from './multiplicity-utils.js';
  3. /**
  4. * Turn a partial annotation into a ‘well-formed’ WebAnnotation.
  5. *
  6. * It sets the following properties, if absent in the given stub:
  7. * - `@context` as required
  8. * - `type` as required, to `'Annotation'`
  9. * - `created` as recommended (to the current time)
  10. * - `target` to `'about:invalid'`
  11. *
  12. * @returns A shallow clone of the given annotation stub, with the missing
  13. * properties added.
  14. */
  15. export declare function completeAnnotationStub(annotationStub: Partial<WebAnnotation>): WebAnnotation;
  16. /**
  17. * Get the name of the creator. If there are multiple, returns the first.
  18. * Assumes the creator is a nested Agent object: if the creator a string
  19. * (presumably the URL of an Agent node), `undefined` is returned.
  20. */
  21. export declare function getSingleCreatorName(annotationOrBody: WebAnnotation | BodyObject): string | undefined;
  22. /**
  23. * Check whether the annotation likely targets the given URL.
  24. *
  25. * The word “likely” is used because, in its comparison, this ignores the URL
  26. * scheme, fragment and query parameters.
  27. *
  28. * Note that, strictly speaking, a URL should be treated as an opaque string.
  29. * In practice, it may however be useful to consider URLs as ‘likely equivalent’
  30. * in order to apply annotations targeting one URL to the document with the
  31. * very similar URL. Apply with caution: Especially a different query may,
  32. * depending on the website at hand, result in very different documents.
  33. */
  34. export declare function targetsUrl(target: WebAnnotation['target'], url: string): boolean;
  35. /**
  36. * Get the URLs of the resources that the annotation targets, for all its
  37. * targets.
  38. */
  39. export declare function getTargetUrls(target: WebAnnotation['target']): string[];
  40. /**
  41. * Get the URL of the resource that the annotation targets, for a single
  42. * target.
  43. */
  44. export declare function getTargetUrl(target: OnlyOne<WebAnnotation['target']>): string;
  45. /**
  46. * Get the exact quotes that the annotation targets using a TextQuoteSelector,
  47. * if any.
  48. */
  49. export declare function getTargetQuotes(target: WebAnnotation['target']): string[];
  50. /**
  51. * Get the exact quote that a single target of an annotation targets using a
  52. * TextQuoteSelector, if any.
  53. */
  54. export declare function getTargetQuote(target: OnlyOne<WebAnnotation['target']>): string | undefined;