IIIF Tools (src/tools/iiif.js)
Helper functions for converting IIIF manifests into MEI XML documents, validating manifests, and extracting pages.
addPage(canvas, canvases, dimension, n, file, meiSurfaceTemplate, hasItems)
Adds a page (<surface>) to the MEI file from a IIIF canvas.
Parameters:
canvas {Object}— IIIF canvas objectcanvases {Array}— Array of all canvases (for dimension lookup)dimension {Array}—[width, height]for the imagen {number}— Page number (1-based)file {Document}— The MEI XML document to modifymeiSurfaceTemplate {Document}— MEI surface template XMLhasItems {boolean}—trueif IIIF Presentation 3 (items),falseif Presentation 2 (images)
Behavior:
- Extracts the label and dimensions of the canvas.
- Determines the
info.jsonURI based on IIIF version. - Generates unique IDs for
<surface>and<graphic>. - Clones the surface template, sets attributes, and appends it to the MEI
<facsimile>.
iiifManifest2mei(json, url, parser, state)
Converts a IIIF manifest JSON object into an MEI XML document.
Parameters:
json {Object}— IIIF manifest JSONurl {string}— Manifest URLparser {DOMParser}— XML parser instancestate {Object}— Application state (for page dimensions)
Behavior:
- Loads MEI file and surface templates asynchronously.
- Sets up MEI metadata (title, source, date, etc.) from the IIIF manifest.
- Attempts to extract shelfmark and composer from IIIF metadata.
- Iterates over canvases (Presentation 2) or items (Presentation 3) and adds each as a surface/page using
addPage.
Returns:
Promise<Document>— Resolves to the generated MEI XML document.
checkIiifManifest(json)
Checks if a given JSON object is a valid IIIF manifest (Presentation 2 or 3).
Parameters:
json {Object}— IIIF manifest JSON
Returns:
{boolean}—trueif the manifest is valid, otherwisefalse.
Validation logic:
- Verifies IIIF context (
@context) - Checks manifest type (
sc:Manifest) - Ensures an ID (
@id) is present - Confirms presence of
sequences(IIIF v2) oritems(IIIF v3)
getPageArray(mei)
Extracts an array of page objects from an MEI XML document.
Parameters:
mei {Document}— MEI XML document
Returns:
{Array}— Array of page objects with:uri— Image target URIid— MEI surface IDn— Page numberwidth,height— DimensionshasSvg— Whether the surface has an embedded SVGhasZones— Whether the surface contains zones
Example Usage
import { iiifManifest2mei, checkIiifManifest, getPageArray } from '@/tools/iiif.js'
// Validate a manifest
const isValid = checkIiifManifest(manifestJson)
// Convert IIIF manifest to MEI
const meiDoc = await iiifManifest2mei(manifestJson, manifestUrl, parser, state)
// Extract page data
const pages = getPageArray(meiDoc)