TypeScript types and utility functions for handling Web Annotations.
Browse Source

Published by publish-to-git

6e3bf2d214 (HEAD -> refs/heads/main, refs/remotes/origin/main) Initial commit
tags/latest
vagrant 2 years ago
commit
69cd848392
10 changed files with 529 additions and 0 deletions
  1. +190
    -0
      Readme.md
  2. +110
    -0
      lib/WebAnnotation.d.ts
  3. +1
    -0
      lib/WebAnnotation.js
  4. +3
    -0
      lib/index.d.ts
  5. +3
    -0
      lib/index.js
  6. +6
    -0
      lib/multiplicity-utils.d.ts
  7. +14
    -0
      lib/multiplicity-utils.js
  8. +54
    -0
      lib/wa-attribute-utils.d.ts
  9. +114
    -0
      lib/wa-attribute-utils.js
  10. +34
    -0
      package.json

+ 190
- 0
Readme.md View File

@@ -0,0 +1,190 @@
# web-annotation-utils

TypeScript types and utility functions for handling Web Annotations.

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.

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.

[Web Annotation Data Model]: https://www.w3.org/TR/annotation-model/

## Usage

Install, using npm or equivalent:

```
npm install git+https://code.treora.com/gerben/web-annotation-utils#latest
```

Usage example:

```
import type { WebAnnotation } from 'web-annotation-utils';
import { completeAnnotationStub, getTargetUrls } from 'web-annotation-utils';

const webAnnotation: WebAnnotation = completeAnnotationStub({
"bodyValue": "interesting.",
"target": [
"http://example.org/page1",
{
"source": "http://example.org/page2",
"selector": {
"type": "TextQuoteSelector",
"exact": "bla bla bla",
}
}
]
});

const targetUrls = getTargetUrls(webAnnotation.target);
// [ 'http://example.org/page1', 'http://example.org/page2' ]
```


## Types

See [WebAnnotation.ts](./WebAnnotation.ts).

<!-- TSDOC_START -->

## :toolbox: Functions

- [asArray](#gear-asarray)
- [asSingleValue](#gear-assinglevalue)
- [findTargetsInDocument](#gear-findtargetsindocument)
- [findTargetInDocument](#gear-findtargetindocument)
- [completeAnnotationStub](#gear-completeannotationstub)
- [getSingleCreatorName](#gear-getsinglecreatorname)
- [targetsUrl](#gear-targetsurl)
- [getTargetUrls](#gear-gettargeturls)
- [getTargetUrl](#gear-gettargeturl)
- [getTargetQuotes](#gear-gettargetquotes)
- [getTargetQuote](#gear-gettargetquote)

### :gear: asArray

| Function | Type |
| ---------- | ---------- |
| `asArray` | `<T>(value: ZeroOrMore<T>) => T[]` |

### :gear: asSingleValue

| Function | Type |
| ---------- | ---------- |
| `asSingleValue` | `<T>(value: ZeroOrMore<T>) => T` |

### :gear: findTargetsInDocument

Find the Elements and/or Ranges in the document the annotation targets, if
any.

This supports the following selector types:
- CssSelector
- TextQuoteSelector
- TextPositionSelector
- RangeSelector

| Function | Type |
| ---------- | ---------- |
| `findTargetsInDocument` | `(target: OneOrMore<Target>, document?: Document) => Promise<DomMatch[]>` |

### :gear: findTargetInDocument

Find the Elements and/or Ranges in the document the annotation targets, if
any, given a single target.

This supports the following selector types:
- CssSelector
- TextQuoteSelector
- TextPositionSelector
- RangeSelector

| Function | Type |
| ---------- | ---------- |
| `findTargetInDocument` | `(target: Target, document?: Document) => Promise<DomMatch[]>` |

### :gear: completeAnnotationStub

Turn a partial annotation into a ‘well-formed’ WebAnnotation.

It sets the following properties, if absent in the given stub:
- `@context` as required
- `type` as required, to `'Annotation'`
- `created` as recommended (to the current time)
- `target` to `'about:invalid'`

| Function | Type |
| ---------- | ---------- |
| `completeAnnotationStub` | `(annotationStub: Partial<WebAnnotation>) => WebAnnotation` |

### :gear: getSingleCreatorName

Get the name of the creator. If there are multiple, returns the first.
Assumes the creator is a nested Agent object: if the creator a string
(presumably the URL of an Agent node), `undefined` is returned.

| Function | Type |
| ---------- | ---------- |
| `getSingleCreatorName` | `(annotationOrBody: WebAnnotation or BodyObject) => string` |

### :gear: targetsUrl

Check whether the annotation likely targets the given URL.

The word “likely” is used because, in its comparison, this ignores the URL
scheme, fragment and query parameters.

Note that, strictly speaking, a URL should be treated as an opaque string.
In practice, it may however be useful to consider URLs as ‘likely equivalent’
in order to apply annotations targeting one URL to the document with the
very similar URL. Apply with caution: Especially a different query may,
depending on the website at hand, result in very different documents.

| Function | Type |
| ---------- | ---------- |
| `targetsUrl` | `(target: OneOrMore<Target>, url: string) => boolean` |

### :gear: getTargetUrls

Get the URLs of the resources that the annotation targets, for all its
targets.

| Function | Type |
| ---------- | ---------- |
| `getTargetUrls` | `(target: OneOrMore<Target>) => string[]` |

### :gear: getTargetUrl

Get the URL of the resource that the annotation targets, for a single
target.

| Function | Type |
| ---------- | ---------- |
| `getTargetUrl` | `(target: Target) => string` |

### :gear: getTargetQuotes

Get the exact quotes that the annotation targets using a TextQuoteSelector,
if any.

| Function | Type |
| ---------- | ---------- |
| `getTargetQuotes` | `(target: OneOrMore<Target>) => string[]` |

### :gear: getTargetQuote

Get the exact quote that a single target of an annotation targets using a
TextQuoteSelector, if any.

| Function | Type |
| ---------- | ---------- |
| `getTargetQuote` | `(target: Target) => string` |



<!-- TSDOC_END -->


## Licence

This is free and unencumbered software released into the public domain.

+ 110
- 0
lib/WebAnnotation.d.ts View File

@@ -0,0 +1,110 @@
import type { OneOrMore, OneOrMoreIncluding, ZeroOrMore } from './multiplicity-utils.js';
/**
* A Web Annotation object.
*
* This is an interpretation of the Web Annotation Data Model:
* <https://www.w3.org/TR/2017/REC-annotation-model-20170223/>
*
* TODO Deal more systemically with ‘relations’, i.e. values that could be
* either a nested object or a URI referring to such an object.
*/
export interface WebAnnotation {
'@context': OneOrMoreIncluding<string, 'http://www.w3.org/ns/anno.jsonld'>;
type: OneOrMoreIncluding<string, 'Annotation'>;
id: string;
target: OneOrMore<Target>;
creator?: OneOrMore<Agent>;
created?: UtcDateTime;
generator?: OneOrMore<Agent>;
generated?: UtcDateTime;
modified?: UtcDateTime;
motivation?: OneOrMore<Motivation>;
audience?: ZeroOrMore<Audience>;
rights?: ZeroOrMore<string>;
canonical?: string;
via?: ZeroOrMore<string>;
body?: BodyChoice | OneOrMore<Body>;
bodyValue?: string;
}
/**
* A slightly stricter type for WebAnnotation, not allowing both a body and bodyValue.
*/
export declare type WebAnnotationStrict = WebAnnotation & (WithBody | WithBodyValue | WithoutBody);
interface WithBody {
body: BodyChoice | OneOrMore<Body>;
bodyValue?: undefined;
}
interface WithBodyValue {
body?: undefined;
bodyValue: string;
}
interface WithoutBody {
body?: undefined;
bodyValue?: undefined;
}
export declare type Body = string | BodyObject;
export declare type BodyObject = {
creator?: OneOrMore<Agent>;
created?: UtcDateTime;
modified?: UtcDateTime;
purpose?: OneOrMore<Motivation>;
} & (TextualBody | SpecificResource | ExternalResource);
export declare type Target = string | SpecificResource | ExternalResource;
export declare type Agent = string | {
id?: string;
type?: OneOrMore<'Person' | 'Organization' | 'Software'>;
name?: OneOrMore<string>;
nickname?: OneOrMore<string>;
email?: OneOrMore<string>;
email_sha1?: OneOrMore<string>;
homepage?: OneOrMore<string>;
};
export declare type Audience = string | {
id?: string;
type?: string;
};
export interface BodyChoice {
type: 'Choice';
items: Body[];
}
export interface TextualBody extends Omit<ExternalResource, 'id' | 'type'> {
id?: string;
type: 'TextualBody';
value: string;
}
export interface SpecificResource {
id?: string;
type?: 'SpecificResource';
source: string;
selector?: string | OneOrMore<Selector>;
accessibility?: AccessibilityFeatures;
rights?: ZeroOrMore<string>;
canonical?: string;
via?: ZeroOrMore<string>;
}
export interface Selector {
type?: string;
refinedBy?: Selector;
}
export interface ExternalResource {
id: string;
type?: OneOrMore<'Dataset' | 'Image' | 'Video' | 'Sound' | 'Text'>;
format?: OneOrMore<string>;
language?: OneOrMore<string>;
processingLanguage?: string;
textDirection?: 'ltr' | 'rtl' | 'auto';
accessibility?: AccessibilityFeatures;
rights?: ZeroOrMore<string>;
canonical?: string;
via?: ZeroOrMore<string>;
}
export declare type Motivation = 'assessing' | 'bookmarking' | 'classifying' | 'commenting' | 'describing' | 'editing' | 'highlighting' | 'identifying' | 'linking' | 'moderating' | 'questioning' | 'replying' | 'tagging';
declare type UtcDateTime = `${string}Z`;
declare global {
interface Date {
toISOString(): UtcDateTime;
}
}
export declare type AccessibilityFeatures = ZeroOrMore<AccessibilityFeature> | 'none' | ['none'];
export declare type AccessibilityFeature = 'annotations' | 'ARIA' | 'bookmarks' | 'index' | 'printPageNumbers' | 'readingOrder' | 'structuralNavigation' | 'tableOfContents' | 'taggedPDF' | 'alternativeText' | 'audioDescription' | 'captions' | 'describedMath' | 'longDescription' | 'rubyAnnotations' | 'signLanguage' | 'transcript' | 'displayTransformability' | 'synchronizedAudioText' | 'timingControl' | 'unlocked' | 'ChemML' | 'latex' | 'MathML' | 'ttsMarkup' | 'highContrastAudio' | 'highContrastDisplay' | 'largePrint' | 'braille' | 'tactileGraphic' | 'tactileObject';
export {};

+ 1
- 0
lib/WebAnnotation.js View File

@@ -0,0 +1 @@
export {};

+ 3
- 0
lib/index.d.ts View File

@@ -0,0 +1,3 @@
export * from './WebAnnotation.js';
export * from './multiplicity-utils.js';
export * from './wa-attribute-utils.js';

+ 3
- 0
lib/index.js View File

@@ -0,0 +1,3 @@
export * from './WebAnnotation.js';
export * from './multiplicity-utils.js';
export * from './wa-attribute-utils.js';

+ 6
- 0
lib/multiplicity-utils.d.ts View File

@@ -0,0 +1,6 @@
export declare type OneOrMore<T> = T | T[];
export declare type ZeroOrMore<T> = undefined | null | T | T[];
export declare type OneOrMoreIncluding<Other extends any, RequiredValue extends any> = RequiredValue | [RequiredValue, ...Other[]] | [...Other[], RequiredValue];
export declare type OnlyOne<T> = T extends (infer X)[] ? X : T;
export declare function asArray<T>(value: ZeroOrMore<T>): T[];
export declare function asSingleValue<T>(value: ZeroOrMore<T>): T | undefined;

+ 14
- 0
lib/multiplicity-utils.js View File

@@ -0,0 +1,14 @@
export function asArray(value) {
if (Array.isArray(value))
return value;
if (value === undefined || value === null)
return [];
return [value];
}
export function asSingleValue(value) {
if (value instanceof Array)
return value[0];
if (value === undefined || value === null)
return undefined;
return value;
}

+ 54
- 0
lib/wa-attribute-utils.d.ts View File

@@ -0,0 +1,54 @@
import type { BodyObject, WebAnnotation } from './WebAnnotation.js';
import { OnlyOne } from './multiplicity-utils.js';
/**
* Turn a partial annotation into a ‘well-formed’ WebAnnotation.
*
* It sets the following properties, if absent in the given stub:
* - `@context` as required
* - `type` as required, to `'Annotation'`
* - `created` as recommended (to the current time)
* - `target` to `'about:invalid'`
*
* @returns A shallow clone of the given annotation stub, with the missing
* properties added.
*/
export declare function completeAnnotationStub(annotationStub: Partial<WebAnnotation>): WebAnnotation;
/**
* Get the name of the creator. If there are multiple, returns the first.
* Assumes the creator is a nested Agent object: if the creator a string
* (presumably the URL of an Agent node), `undefined` is returned.
*/
export declare function getSingleCreatorName(annotationOrBody: WebAnnotation | BodyObject): string | undefined;
/**
* Check whether the annotation likely targets the given URL.
*
* The word “likely” is used because, in its comparison, this ignores the URL
* scheme, fragment and query parameters.
*
* Note that, strictly speaking, a URL should be treated as an opaque string.
* In practice, it may however be useful to consider URLs as ‘likely equivalent’
* in order to apply annotations targeting one URL to the document with the
* very similar URL. Apply with caution: Especially a different query may,
* depending on the website at hand, result in very different documents.
*/
export declare function targetsUrl(target: WebAnnotation['target'], url: string): boolean;
/**
* Get the URLs of the resources that the annotation targets, for all its
* targets.
*/
export declare function getTargetUrls(target: WebAnnotation['target']): string[];
/**
* Get the URL of the resource that the annotation targets, for a single
* target.
*/
export declare function getTargetUrl(target: OnlyOne<WebAnnotation['target']>): string;
/**
* Get the exact quotes that the annotation targets using a TextQuoteSelector,
* if any.
*/
export declare function getTargetQuotes(target: WebAnnotation['target']): string[];
/**
* Get the exact quote that a single target of an annotation targets using a
* TextQuoteSelector, if any.
*/
export declare function getTargetQuote(target: OnlyOne<WebAnnotation['target']>): string | undefined;

+ 114
- 0
lib/wa-attribute-utils.js View File

@@ -0,0 +1,114 @@
import { asArray, asSingleValue } from './multiplicity-utils.js';
/**
* Turn a partial annotation into a ‘well-formed’ WebAnnotation.
*
* It sets the following properties, if absent in the given stub:
* - `@context` as required
* - `type` as required, to `'Annotation'`
* - `created` as recommended (to the current time)
* - `target` to `'about:invalid'`
*
* @returns A shallow clone of the given annotation stub, with the missing
* properties added.
*/
export function completeAnnotationStub(annotationStub) {
const webAnnotation = Object.assign({ '@context': 'http://www.w3.org/ns/anno.jsonld', type: 'Annotation', created: new Date().toISOString(), id: '', target: 'about:invalid' }, annotationStub);
return webAnnotation;
}
/**
* Get the name of the creator. If there are multiple, returns the first.
* Assumes the creator is a nested Agent object: if the creator a string
* (presumably the URL of an Agent node), `undefined` is returned.
*/
export function getSingleCreatorName(annotationOrBody) {
var _a;
const creator = asSingleValue(annotationOrBody.creator);
if (typeof creator === 'string')
return undefined;
return asSingleValue((_a = creator === null || creator === void 0 ? void 0 : creator.name) !== null && _a !== void 0 ? _a : creator === null || creator === void 0 ? void 0 : creator.nickname);
}
/**
* Check whether the annotation likely targets the given URL.
*
* The word “likely” is used because, in its comparison, this ignores the URL
* scheme, fragment and query parameters.
*
* Note that, strictly speaking, a URL should be treated as an opaque string.
* In practice, it may however be useful to consider URLs as ‘likely equivalent’
* in order to apply annotations targeting one URL to the document with the
* very similar URL. Apply with caution: Especially a different query may,
* depending on the website at hand, result in very different documents.
*/
export function targetsUrl(target, url) {
return getTargetUrls(target).some((targetUrl) => sameishUrl(targetUrl, url));
}
// Compare URLs while ignoring the scheme, fragment identifier, query parameter and trailing slash.
function sameishUrl(url1, url2) {
return normaliseUrl(url1) === normaliseUrl(url2);
}
function normaliseUrl(url) {
url = url
.split('#')[0]
.split('?')[0]
.replace(/^[a-zA-Z0-9.+-]+:\/\//, '');
if (url.endsWith('/'))
url = url.slice(0, -1);
return url;
}
/**
* Get the URLs of the resources that the annotation targets, for all its
* targets.
*/
export function getTargetUrls(target) {
return unique(asArray(target).map(getTargetUrl));
}
/**
* Get the URL of the resource that the annotation targets, for a single
* target.
*/
export function getTargetUrl(target) {
if (typeof target === 'string') {
// This string *could* be referring to a non-nested SpecificResource that
// then contains the actual target URL. But we are not able to fetch that
// now, and simply assume the string refers to the target document.
return target;
}
// Specific Resource
if ('source' in target)
return target.source;
// External Resource
return target.id;
}
/**
* Get the exact quotes that the annotation targets using a TextQuoteSelector,
* if any.
*/
export function getTargetQuotes(target) {
const quotes = unique(asArray(target).map(getTargetQuote)).filter((s) => s !== undefined);
return quotes;
}
/**
* Get the exact quote that a single target of an annotation targets using a
* TextQuoteSelector, if any.
*/
export function getTargetQuote(target) {
if (typeof target === 'string')
return undefined;
if ('selector' in target) {
// Find if target.selector is/has a TextQuoteSelector.
const selectors = asArray(target.selector);
const textQuoteSelector = selectors.find(selector => {
if (typeof selector === 'string') {
// The selector is not nested in the annotation. But we are not able to
// fetch it now, and will thus have to ignore this selector.
return false;
}
return selector.type === 'TextQuoteSelector';
});
if (textQuoteSelector)
return textQuoteSelector.exact;
}
}
function unique(a) {
return [...new Set(a)];
}

+ 34
- 0
package.json View File

@@ -0,0 +1,34 @@
{
"name": "web-annotation-utils",
"version": "1.0.0",
"description": "TypeScript types and utility functions for handling Web Annotations",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"type": "module",
"exports": {
".": "./lib/index.js"
},
"files": ["lib"],
"scripts": {
"prepublish": "tsc",
"publish": "publish-to-git --force && publish-to-git --force --tag latest",
"doc": "tsdoc --src=* --dest=Readme.md"
},
"repository": {
"type": "git",
"url": "https://code.treora.com/gerben/web-annotation-utils"
},
"keywords": [
"web-annotation"
],
"author": "Gerben",
"license": "Unlicense",
"dependencies": {
"@apache-annotator/selector": "^0.3.0-dev.23",
"typescript": "^4.8.4"
},
"devDependencies": {
"publish-to-git": "^1.1.7",
"tsdoc-markdown": "^0.0.1"
}
}

Loading…
Cancel
Save