') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Improve upgrade of IIIF2 annotations by jbaiter · Pull Request #26 · IIIF-Commons/parser · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions __tests__/presentation-2-parser/upgrade-test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,8 @@ import withDimensions from '../../fixtures/presentation-2/iiif-fixture-manifest-
import europeana from '../../fixtures/presentation-2/europeana.json';
import { presentation2to3 } from '../../src/presentation-2';
import { Validator } from '@hyperion-framework/validator';
import annoList from '../../fixtures/presentation-2/iiif-fixture-annotation-list.json';
import choiceAnnoList from '../../fixtures/presentation-2/anno_list_choice.json';

describe('Presentation 2 to 3', () => {
const validator = new Validator();
Expand DownExpand Up@@ -289,4 +291,181 @@ describe('Presentation 2 to 3', () => {
},
]);
});

test('annotation list', () => {
const result = presentation2to3.traverseAnnotationList(annoList as any);
expect(result).toMatchInlineSnapshot(`
{
"@context": "http://iiif.io/api/presentation/3/context.json",
"id": "http://iiif.io/api/presentation/2.0/example/fixtures/list/65/list1.json",
"items": [
{
"body": {
"chars": "Top of First Page to Display",
"id": "http://example.org/cnt:ContentAsText/363",
"type": "TextualBody",
},
"id": "http://example.org/oa:Annotation/364",
"motivation": "painting",
"target": {
"selector": {
"type": "FragmentSelector",
"value": "xywh=225,70,750,150",
},
"source": {
"id": "http://iiif.io/api/presentation/2.0/example/fixtures/canvas/65/c1.json",
"type": "Canvas",
},
"type": "SpecificResource",
},
"type": "Annotation",
},
{
"body": [
{
"chars": "character",
"id": "http://example.org/oa:Tag/365",
"type": "Tag",
},
{
"chars": "<p>万</p>
<p><audio style=\\"display: none;\\" controls=\\"controls\\"></audio></p>",
"format": "text/html",
"id": "http://example.org/dctypes:Text/366",
"type": "Text",
},
],
"id": "http://example.org/oa:Annotation/367",
"motivation": [
"tagging",
"commenting",
],
"target": {
"selector": [
{
"type": "FragmentSelector",
"value": "xywh=3112,1063,518,656",
},
{
"type": "SvgSelector",
"value": "<svg xmlns='http://www.w3.org/2000/svg'><path xmlns=\\"http://www.w3.org/2000/svg\\" d=\\"M3111.52264,1062.87895h259.04821v0h259.04821v327.86107v327.86107h-259.04821h-259.04821v-327.86107z\\" data-paper-data=\\"{&quot;strokeWidth&quot;:1,&quot;rotation&quot;:0,&quot;deleteIcon&quot;:null,&quot;rotationIcon&quot;:null,&quot;group&quot;:null,&quot;editable&quot;:true,&quot;annotation&quot;:null}\\" id=\\"rectangle_023df9bc-ab63-4d9b-9426-ddbf468fa0c6\\" fill-opacity=\\"0\\" fill=\\"#00bfff\\" fill-rule=\\"nonzero\\" stroke=\\"#00bfff\\" stroke-width=\\"1\\" stroke-linecap=\\"butt\\" stroke-linejoin=\\"miter\\" stroke-miterlimit=\\"10\\" stroke-dasharray=\\"\\" stroke-dashoffset=\\"0\\" font-family=\\"none\\" font-weight=\\"none\\" font-size=\\"none\\" text-anchor=\\"none\\" style=\\"mix-blend-mode: normal\\"/></svg>",
},
],
"source": "https://purl.stanford.edu/ch264fq0568/iiif/canvas/ch264fq0568_1",
"type": "SpecificResource",
},
"type": "Annotation",
},
],
"label": {
"none": [
"Test 65 List 1",
],
},
"type": "AnnotationPage",
}
`);
});

test('annotation list with choices', () => {
const result = presentation2to3.traverseAnnotationList(choiceAnnoList as any);
expect(result).toMatchInlineSnapshot(`
{
"@context": "http://iiif.io/api/presentation/3/context.json",
"id": "https://collections.recolnat.org/annotate-server/iiif/2/annotationList/30/febbe57672bb5eaf097de6c5448c43ad8a82e012/list",
"items": [
{
"body": [
{
"chars": "<p><i>Zone of interest</i><br/><b>Place</b><br/>Cauca of Huila</p>",
"format": "text/html",
"id": "http://example.org/dctypes:Text/368",
"type": "Text",
},
{
"chars": "Colombia",
"id": "http://example.org/oa:Tag/369",
"type": "Tag",
},
{
"chars": "Cauca",
"id": "http://example.org/oa:Tag/370",
"type": "Tag",
},
],
"id": "https://collections.recolnat.org/annotate-server/iiif/2/annotationList/30/febbe57672bb5eaf097de6c5448c43ad8a82e012/list/1",
"label": {
"none": [
"label-Place",
],
},
"metadata": [
{
"label": {
"none": [
"Note",
],
},
"value": {
"none": [
"Une note complémentaire à propos de cette annotation",
],
},
},
],
"motivation": [
"commenting",
"tagging",
],
"target": {
"selector": [
{
"type": "FragmentSelector",
"value": "xywh=2458.3294117573255,4064.0775928284547,148.5859420675797,52.27032787325061",
},
{
"type": "SvgSelector",
"value": "<svg xmlns='http://www.w3.org/2000/svg'><path .../></svg>",
},
],
"source": "https://collections.recolnat.org/annotate-server/iiif/2/canvases/canvas/1",
"type": "SpecificResource",
},
"type": "Annotation",
},
{
"body": {
"chars": "<p><i>Surface tool</i><br/><b>Feuille</b><br/>70.97 mm²</p>",
"format": "text/html",
"id": "http://example.org/dctypes:Text/371",
"type": "Text",
},
"id": "https://collections.recolnat.org/annotate-server/iiif/2/annotationList/30/febbe57672bb5eaf097de6c5448c43ad8a82e012/list/3",
"label": {
"none": [
"label-Feuille",
],
},
"motivation": "commenting",
"target": {
"selector": [
{
"type": "FragmentSelector",
"value": "xywh=1437.1021112372669,1013.9299384835548,129.10014843443582,234.9981533683548",
},
{
"type": "SvgSelector",
"value": "<svg xmlns='http://www.w3.org/2000/svg'><path .../></svg>",
},
],
"source": "https://collections.recolnat.org/annotate-server/iiif/2/canvases/canvas/1",
"type": "SpecificResource",
},
"type": "Annotation",
},
],
"type": "AnnotationPage",
}
`);
});
});
85 changes: 85 additions & 0 deletions fixtures/presentation-2/anno_list_choice.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
{
"@context" : "http://iiif.io/api/presentation/2/context.json",
"@id" : "https://collections.recolnat.org/annotate-server/iiif/2/annotationList/30/febbe57672bb5eaf097de6c5448c43ad8a82e012/list",
"@type" : "sc:AnnotationList",
"resources" : [
{
"@id" : "https://collections.recolnat.org/annotate-server/iiif/2/annotationList/30/febbe57672bb5eaf097de6c5448c43ad8a82e012/list/1",
"@type" : "oa:Annotation",
"motivation" : ["oa:commenting", "oa:tagging"],
"resource" : [
{
"@type": "dctypes:Text",
"chars" : "<p><i>Zone of interest</i><br/><b>Place</b><br/>Cauca of Huila</p>",
"format" : "text/html"
},
{
"@type" : "oa:Tag",
"chars" : "Colombia"
},
{
"@type" : "oa:Tag",
"chars" : "Cauca"
}
],
"on" : {
"@id" : "https://collections.recolnat.org/annotate-server/iiif/2/resource/1",
"@type" : "oa:SpecificResource",
"full" : "https://collections.recolnat.org/annotate-server/iiif/2/canvases/canvas/1",
"selector" : {
"@type" : "oa:Choice",
"default" : {
"@type" : "oa:FragmentSelector",
"value" : "xywh=2458.3294117573255,4064.0775928284547,148.5859420675797,52.27032787325061"
},
"item" : {
"@type" : "oa:SvgSelector",
"value" : "<svg xmlns='http://www.w3.org/2000/svg'><path .../></svg>"
}
},
"within" : {
"@type" : "sc:Manifest",
"@id" : "https://collections.recolnat.org/annotate-server/iiif/2/30/manifest"
}
},
"label" : "label-Place",
"metadata": [
{
"label": "Note",
"value": "Une note complémentaire à propos de cette annotation"
}
]
},
{
"@id" : "https://collections.recolnat.org/annotate-server/iiif/2/annotationList/30/febbe57672bb5eaf097de6c5448c43ad8a82e012/list/3",
"@type" : "oa:Annotation",
"motivation" : "oa:commenting",
"resource" : {
"@type" : "dctypes:Text",
"chars" : "<p><i>Surface tool</i><br/><b>Feuille</b><br/>70.97 mm²</p>",
"format" : "text/html"
},
"on" : {
"@id" : "https://collections.recolnat.org/annotate-server/iiif/2/resource/3",
"@type": "oa:SpecificResource",
"full": "https://collections.recolnat.org/annotate-server/iiif/2/canvases/canvas/1",
"selector": {
"@type": "oa:Choice",
"default" : {
"@type": "oa:FragmentSelector",
"value": "xywh=1437.1021112372669,1013.9299384835548,129.10014843443582,234.9981533683548"
},
"item": {
"@type": "oa:SvgSelector",
"value": "<svg xmlns='http://www.w3.org/2000/svg'><path .../></svg>"
}
},
"within": {
"@type": "sc:Manifest",
"@id": "https://collections.recolnat.org/annotate-server/iiif/2/30/manifest"
}
},
"label": "label-Feuille"
}
]
}
32 changes: 32 additions & 0 deletions fixtures/presentation-2/iiif-fixture-annotation-list.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,6 +23,38 @@
"value": "xywh=225,70,750,150"
}
}
},
{
"@type": "oa:Annotation",
"motivation": ["oa:tagging", "oa:commenting"],
"resource": [
{
"@type": "oa:Tag",
"chars": "character"
},
{
"@type": "dctypes:Text",
"format": "text/html",
"chars": "<p>万</p>\n<p><audio style=\"display: none;\" controls=\"controls\"></audio></p>"
}
],
"on": [
{
"@type": "oa:SpecificResource",
"full": "https://purl.stanford.edu/ch264fq0568/iiif/canvas/ch264fq0568_1",
"selector": {
"@type": "oa:Choice",
"default": {
"@type": "oa:FragmentSelector",
"value": "xywh=3112,1063,518,656"
},
"item": {
"@type": "oa:SvgSelector",
"value": "<svg xmlns='http://www.w3.org/2000/svg'><path xmlns=\"http://www.w3.org/2000/svg\" d=\"M3111.52264,1062.87895h259.04821v0h259.04821v327.86107v327.86107h-259.04821h-259.04821v-327.86107z\" data-paper-data=\"{&quot;strokeWidth&quot;:1,&quot;rotation&quot;:0,&quot;deleteIcon&quot;:null,&quot;rotationIcon&quot;:null,&quot;group&quot;:null,&quot;editable&quot;:true,&quot;annotation&quot;:null}\" id=\"rectangle_023df9bc-ab63-4d9b-9426-ddbf468fa0c6\" fill-opacity=\"0\" fill=\"#00bfff\" fill-rule=\"nonzero\" stroke=\"#00bfff\" stroke-width=\"1\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-miterlimit=\"10\" stroke-dasharray=\"\" stroke-dashoffset=\"0\" font-family=\"none\" font-weight=\"none\" font-size=\"none\" text-anchor=\"none\" style=\"mix-blend-mode: normal\"/></svg>"
}
}
}
]
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,7 @@
"test": "vitest"
},
"dependencies": {
"@iiif/presentation-2": "^1.0.3",
"@iiif/presentation-2": "^1.0.4",
"@iiif/presentation-3": "^1.1.3",
"@types/geojson": "^7946.0.8"
},
Expand Down
8 changes: 7 additions & 1 deletion src/presentation-2/traverse.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -315,7 +315,13 @@ export class Traverse<

traverseAnnotationItems(annotation: Annotation): Annotation {
if (annotation.resource) {
annotation.resource = this.traverseContentResource(annotation.resource as CommonContentResource);
if (Array.isArray(annotation.resource)) {
annotation.resource = annotation.resource.map((res) =>
this.traverseContentResource(res as CommonContentResource)
);
} else {
annotation.resource = this.traverseContentResource(annotation.resource as CommonContentResource);
}
}

if (annotation.on) {
Expand Down
Loading