Commit a46fe918 authored by Robert Knight's avatar Robert Knight

Typecheck `DocumentMetadata` class

Typecheck the class that reads metadata from HTML documents and attaches
it to annotations as the `annotation.document` property.

This resulted in a few minor corrections to the `DocumentMetadata` type
in `src/types/annotator.js`.
parent e9dd0764
......@@ -10,9 +10,18 @@
** https://github.com/openannotation/annotator/blob/master/LICENSE
*/
/**
* nb. The `DocumentMetadata` type is renamed to avoid a conflict with the
* `DocumentMetadata` class below.
*
* @typedef {import('../../types/annotator').DocumentMetadata} Metadata
*/
import baseURI from 'document-base-uri';
// @ts-expect-error - '../plugin' needs to be converted to JS.
import Plugin from '../plugin';
import { normalizeURI } from '../util/url';
/**
......@@ -26,14 +35,16 @@ export default class DocumentMeta extends Plugin {
this.events = {
beforeAnnotationCreated: 'beforeAnnotationCreated',
};
/** @type {Metadata} */
this.metadata = { title: document.title, link: [] };
this.baseURI = options.baseURI || baseURI;
this.document = options.document || document;
this.normalizeURI = options.normalizeURI || normalizeURI;
}
pluginInit() {
// Test seams.
this.baseURI = this.options.baseURI || baseURI;
this.document = this.options.document || document;
this.normalizeURI = this.options.normalizeURI || normalizeURI;
this.getDocumentMetadata();
}
......@@ -79,7 +90,7 @@ export default class DocumentMeta extends Plugin {
* Return metadata for the current page.
*/
getDocumentMetadata() {
this.metadata = {};
this.metadata = { title: document.title, link: [] };
// first look for some common metadata types
// TODO: look for microdata/rdfa?
......@@ -122,7 +133,17 @@ export default class DocumentMeta extends Plugin {
this.metadata.eprints = this._getMetaTags('eprints', 'name', '.');
}
/**
* Return an array of all the `content` values of `<meta>` tags on the page
* where the attribute named `attribute` begins with `<prefix><delimiter>`.
*
* @param {string} prefix
* @param {string} attribute
* @param {string} delimiter
* @return {Object.<string,string[]>}
*/
_getMetaTags(prefix, attribute, delimiter) {
/** @type {Object.<string,string[]>} */
const tags = {};
for (let meta of Array.from(this.document.querySelectorAll('meta'))) {
const name = meta.getAttribute(attribute);
......
......@@ -34,7 +34,6 @@
// Files in `src/annotator` that still have errors to be resolved.
"annotator/pdf-sidebar.js",
"annotator/plugin/document.js",
// Enable this once the rest of `src/sidebar` is checked.
"sidebar/index.js",
......
......@@ -17,8 +17,8 @@
* @typedef DocumentMetadata
* @prop {string} title
* @prop {Object[]} link
* @prop {string} link.rel
* @prop {string} link.type
* @prop {string} [link.rel]
* @prop {string} [link.type]
* @prop {string} link.href
* // html pages
* @prop {Object.<string, string[]>} [dc]
......@@ -27,6 +27,7 @@
* @prop {Object.<string, string[]>} [highwire]
* @prop {Object.<string, string[]>} [prism]
* @prop {Object.<string, string[]>} [twitter]
* @prop {string} [favicon]
* // pdf files
* @prop {string} [documentFingerprint]
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment