ochre-sdk 1.3.33 → 1.3.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -241,6 +241,8 @@ for (const match of result.ocrMatches ?? []) {
241
241
 
242
242
  Because a word node holds a single OCR word, matching runs against each word, which defaults to case-insensitive `includes`, and the search value is split on whitespace and matched as a phrase. A match is a run of consecutive words in which the first word matches the first term, the second word matches the second term, and so on. With `includes`, each word only has to contain its term, so searching for `"whitman madeline"` finds `Whitman, Madeline` despite the comma, while `exact` requires each word to equal its term. Searching for `"Chicago University"` does not match `University of Chicago`, and a single-term search matches every word that matches the term, each as its own match. Matches come back in document order, and an item with no OCR layer, or with no matches, returns an empty array, though requesting an item that does not exist is an error.
243
243
 
244
+ Every match has an `id` that is unique among the matches of the requested item and stays the same for the same match on every fetch, as long as the page's OCR layer is unchanged. It is built from where the match sits in the document, not from its geometry or text, which means you can use it as a key for a list of results or to remember a selected match; treat it as opaque and don't parse it.
245
+
244
246
  `resourceUuid` names the Resource that owns the page's OCR layer, which differs from the requested item when the OCR lives on a child Resource, and `pageNumber` gives the 1-based position of that Resource among its sibling Resources. You can use `resourceUuid` to navigate to the page and `pageNumber` to label it.
245
247
 
246
248
  Pass `contextWordCount` to get that many words on either side of each match in `context.before` and `context.after`. The context crosses lines and blocks but never leaves the page, and `context.isTruncatedBefore` and `context.isTruncatedAfter` tell you whether the page has more words beyond it, so you know when to draw an ellipsis. The context is counted in whole words, and if you need snippets of an even length, you can trim it to a character budget in your UI. Without `contextWordCount`, `context` is null and the search skips the extra work.
package/dist/errors.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { isObject, readEntries } from "./reflection.mjs";
2
1
  import * as v from "valibot";
2
+ import { isObject, readEntries } from "./reflection.mjs";
3
3
  // src/errors.ts
4
4
  /**
5
5
  The shape a valibot issue has to have for this module to render it
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { getErrorOutput } from "../errors.mjs";
2
3
  import { requestOchre } from "./request.mjs";
3
4
  import { parseGallery } from "../parsers/index.mjs";
@@ -5,7 +6,6 @@ import { parseRequestedLanguages, resolveContentLanguages } from "../parsers/lan
5
6
  import { gallerySchema } from "../schemas.mjs";
6
7
  import { XMLGalleryData } from "../xml/schemas.mjs";
7
8
  import { OCHRE_RESPONSE_PADDING, compileOchreQuery, getContentLanguage, stringLiteral } from "../xquery.mjs";
8
- import * as v from "valibot";
9
9
  // src/fetchers/gallery.ts
10
10
  function buildXQuery(parameters) {
11
11
  const { uuid, filter, page, perPage, contentLanguage } = parameters;
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { OCHRE_COLLECTION_CATEGORIES } from "../categories.mjs";
2
3
  import { getErrorOutput } from "../errors.mjs";
3
4
  import { requestOchre } from "./request.mjs";
@@ -6,7 +7,6 @@ import { parseRequestedLanguages, resolveContentLanguages } from "../parsers/lan
6
7
  import { uuidSchema } from "../schemas.mjs";
7
8
  import { XMLItemLinksData } from "../xml/schemas.mjs";
8
9
  import { OCHRE_RESPONSE_PADDING, compileOchreQuery, getContentLanguage, stringLiteral } from "../xquery.mjs";
9
- import * as v from "valibot";
10
10
  // src/fetchers/item-children.ts
11
11
  function buildXQuery(uuid, category, contentLanguage) {
12
12
  const categories = category == null ? OCHRE_COLLECTION_CATEGORIES : typeof category === "string" ? [category] : category;
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { ITEM_CATEGORY_ALIASES, OCHRE_COLLECTION_CATEGORIES } from "../categories.mjs";
2
3
  import { getErrorOutput } from "../errors.mjs";
3
4
  import { requestOchre } from "./request.mjs";
@@ -6,7 +7,6 @@ import { parseRequestedLanguages, resolveContentLanguages } from "../parsers/lan
6
7
  import { uuidSchema } from "../schemas.mjs";
7
8
  import { XMLItemLinksData } from "../xml/schemas.mjs";
8
9
  import { OCHRE_RESPONSE_PADDING, compileOchreQuery, getContentLanguage, stringLiteral } from "../xquery.mjs";
9
- import * as v from "valibot";
10
10
  // src/fetchers/item-links.ts
11
11
  /**
12
12
  Build an XQuery string to fetch linked items from the OCHRE API.
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { normalizeItemCategory } from "../categories.mjs";
2
3
  import { getErrorOutput } from "../errors.mjs";
3
4
  import { requestOchre } from "./request.mjs";
@@ -9,7 +10,6 @@ import { compileContainerItemsQuery } from "../query.mjs";
9
10
  import { itemLocationsParametersSchema } from "../schemas.mjs";
10
11
  import { XMLItemLocationsData } from "../xml/schemas.mjs";
11
12
  import { getContentLanguage, stringLiteral } from "../xquery.mjs";
12
- import * as v from "valibot";
13
13
  // src/fetchers/item-locations.ts
14
14
  function parseViewContexts(scopes, languages) {
15
15
  const viewContexts = /* @__PURE__ */ new Map();
@@ -1,9 +1,9 @@
1
+ import * as v from "valibot";
1
2
  import { getErrorOutput } from "../errors.mjs";
2
3
  import { requestOchre } from "./request.mjs";
3
4
  import { buildOcrLayerPath, buildOcrWordPath } from "../ocr.mjs";
4
5
  import { itemOcrDataParametersSchema } from "../schemas.mjs";
5
6
  import { OCHRE_RESPONSE_PADDING, stringLiteral } from "../xquery.mjs";
6
- import * as v from "valibot";
7
7
  // src/fetchers/item-ocr-data.ts
8
8
  const OCR_STRING_VERTEX_REGEX = /\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*\)/g;
9
9
  /**
@@ -21,6 +21,7 @@ const ocrStringSchema = v.object({
21
21
  Schema for a single OCR match in the OCHRE API response
22
22
  */
23
23
  const ocrMatchSchema = v.object({
24
+ id: v.string(),
24
25
  resourceUuid: v.optional(v.string(), ""),
25
26
  pageNumber: v.optional(v.string(), ""),
26
27
  text: v.optional(v.string(), ""),
@@ -85,6 +86,11 @@ form in `SUBS_CONTENT`. The two nodes are read as one word, so a phrase
85
86
  spanning a hyphenated word still lines up term by term, and the hyphenation
86
87
  check only runs on pages that carry a `SUBS_TYPE` at all.
87
88
 
89
+ A match's ID joins the requested UUID, the position of its layer in the item,
90
+ and the position of its first word node in that layer. A phrase starts at
91
+ most one match per word, so the ID is unique, and it reads only document
92
+ positions, so it holds for any OCR source and repeats on every fetch.
93
+
88
94
  The matches are wrapped in an `<ocrMatches>` element carrying a `found`
89
95
  flag, which tells "no such item" apart from "no matches".
90
96
  @param parameters - The parameters for the fetch
@@ -122,7 +128,7 @@ declare function local:joins-previous($words as element()*, $index as xs:integer
122
128
 
123
129
  let $ochre := doc(${stringLiteral(uuid)})/ochre
124
130
  let $ocrMatches :=
125
- for $layer in ${buildOcrLayerPath("$ochre")}
131
+ for $layer at $layerIndex in ${buildOcrLayerPath("$ochre")}
126
132
  let $words := ${buildOcrWordPath("$layer")}
127
133
  let $layerText := ${fold("string-join(($words/@CONTENT, $words/@SUBS_CONTENT), \" \")")}
128
134
  where (every $term in $terms satisfies contains($layerText, $term))
@@ -153,6 +159,7 @@ let $ocrMatches :=
153
159
  let $firstWord := $heads[$index]
154
160
  let $lastWord := if ($last lt $textCount) then $heads[$last + 1] - 1 else $wordCount
155
161
  return <ocrMatch
162
+ id="{concat(${stringLiteral(uuid)}, ":", $layerIndex, ":", $firstWord)}"
156
163
  resourceUuid="{string($owner/@uuid)}"
157
164
  pageNumber="{$pageNumber}"
158
165
  text="{string-join(subsequence($texts, $index, $termCount), " ")}"${contextAttributes}>{
@@ -231,6 +238,7 @@ async function fetchItemOcrData(uuid, value, options) {
231
238
  vertices: parseOcrStringVertices(parsedOcrString.vertices)
232
239
  }));
233
240
  ocrMatches.push({
241
+ id: parsedOcrMatch.id,
234
242
  resourceUuid: parsedOcrMatch.resourceUuid !== "" ? parsedOcrMatch.resourceUuid : null,
235
243
  pageNumber: parseOcrNumber(parsedOcrMatch.pageNumber),
236
244
  text: parsedOcrMatch.text,
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { ITEM_CATEGORIES_WITH_EMBEDDED_ITEMS, ITEM_CONTAINER_CATEGORIES, isItemCategoryWithEmbeddedItems, isItemContainerCategory } from "../categories.mjs";
2
3
  import { getErrorOutput } from "../errors.mjs";
3
4
  import { requestOchre } from "./request.mjs";
@@ -7,7 +8,6 @@ import { parseWebpageView } from "../parsers/website/index.mjs";
7
8
  import { uuidSchema } from "../schemas.mjs";
8
9
  import { XMLData } from "../xml/schemas.mjs";
9
10
  import { OCHRE_RESPONSE_PADDING, compileOchreQuery, getContentLanguage, stringLiteral } from "../xquery.mjs";
10
- import * as v from "valibot";
11
11
  // src/fetchers/item.ts
12
12
  function isItemWithEmbeddedItems(item) {
13
13
  return isItemCategoryWithEmbeddedItems(item.category);
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { getErrorOutput } from "../errors.mjs";
2
3
  import { requestOchre } from "./request.mjs";
3
4
  import { parseIdentification } from "../parsers/index.mjs";
@@ -5,7 +6,6 @@ import { parseRequestedLanguages, resolveContentLanguages } from "../parsers/lan
5
6
  import { uuidSchema } from "../schemas.mjs";
6
7
  import { XMLMapSourcesData } from "../xml/schemas.mjs";
7
8
  import { OCHRE_RESPONSE_PADDING, compileOchreQuery, getContentLanguage, stringLiteral } from "../xquery.mjs";
8
- import * as v from "valibot";
9
9
  // src/fetchers/map-layers.ts
10
10
  const REGEX_TILE_TEMPLATE_LEVEL = /\{(?:level|z)\}/i;
11
11
  const REGEX_TILE_TEMPLATE_ROW = /\{(?:row|y)\}/i;
@@ -1,5 +1,5 @@
1
- import { FetchFunction } from "../parsers/helpers.mjs";
2
1
  import * as v from "valibot";
2
+ import { FetchFunction } from "../parsers/helpers.mjs";
3
3
  export type OchreRequestOptions = {
4
4
  fetch?: FetchFunction;
5
5
  signal?: AbortSignal;
@@ -1,8 +1,8 @@
1
+ import { XMLParser } from "fast-xml-parser";
2
+ import * as v from "valibot";
1
3
  import { OCHRE_ENDPOINT, XML_PARSER_OPTIONS } from "../constants.mjs";
2
4
  import { createSchemaValidationError } from "../errors.mjs";
3
5
  import { restoreXMLMetadata } from "../xml/metadata.mjs";
4
- import * as v from "valibot";
5
- import { XMLParser } from "fast-xml-parser";
6
6
  // src/fetchers/request.ts
7
7
  const OCHRE_XQUERY_ENDPOINT = `${OCHRE_ENDPOINT}?xquery&xsl=none&lang="*"`;
8
8
  const xmlParser = new XMLParser(XML_PARSER_OPTIONS);
@@ -1,7 +1,7 @@
1
+ import * as v from "valibot";
1
2
  import { getErrorOutput } from "../../errors.mjs";
2
3
  import { fetchContainerItems } from "../container-items.mjs";
3
4
  import { setItemsParametersSchema } from "../../schemas.mjs";
4
- import * as v from "valibot";
5
5
  // src/fetchers/set/items.ts
6
6
  async function fetchSetItems(parameters, containedItemCategories, options) {
7
7
  try {
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { DEFAULT_LANGUAGES } from "../../constants.mjs";
2
3
  import { getErrorOutput } from "../../errors.mjs";
3
4
  import { requestOchre } from "../request.mjs";
@@ -6,7 +7,6 @@ import { parseXMLContent } from "../../parsers/string.mjs";
6
7
  import { compileContainerItemsQuery, getItemFilterQueries, getPropertyFacetSelectors } from "../../query.mjs";
7
8
  import { setPropertyValuesParametersSchema } from "../../schemas.mjs";
8
9
  import { stringLiteral } from "../../xquery.mjs";
9
- import * as v from "valibot";
10
10
  // src/fetchers/set/property-values.ts
11
11
  function getLabelContentLanguages(content) {
12
12
  const languages = [];
@@ -1,7 +1,7 @@
1
+ import * as v from "valibot";
1
2
  import { getErrorOutput } from "../../errors.mjs";
2
3
  import { fetchContainerItems } from "../container-items.mjs";
3
4
  import { treeItemsParametersSchema } from "../../schemas.mjs";
4
- import * as v from "valibot";
5
5
  // src/fetchers/tree/items.ts
6
6
  async function fetchTreeItems(parameters, containedItemCategories, options) {
7
7
  try {
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { CONTEXT_CATEGORY_ALIASES, ITEM_CATEGORY_BY_ALIAS, ITEM_CATEGORY_FACTS, SET_ITEM_CATEGORIES, isHeadingItemCategory, normalizeItemCategory } from "../categories.mjs";
2
3
  import { getParserOptions, multilingualFromText, parseContentLike, parseContentLikeText, parseLicense, parseRequiredContentLike, parseStringLike } from "./helpers.mjs";
3
4
  import { parseMetadataLanguages, resolveDefaultLanguage, resolveLanguages } from "./languages.mjs";
@@ -7,7 +8,6 @@ import { readPropertyValueText } from "./property-token.mjs";
7
8
  import { parseXMLString, transformPermanentIdentificationUrl } from "./string.mjs";
8
9
  import { readEntries, readProperty } from "../reflection.mjs";
9
10
  import { getXMLSourceIndex } from "../xml/metadata.mjs";
10
- import * as v from "valibot";
11
11
  // src/parsers/index.ts
12
12
  function isXMLContextGroup(context) {
13
13
  return "context" in context;
@@ -1,8 +1,8 @@
1
+ import * as v from "valibot";
1
2
  import { DEFAULT_LANGUAGES } from "../constants.mjs";
2
3
  import { parseStringLike } from "./helpers.mjs";
3
4
  import { readArrayProperty, readEntries } from "../reflection.mjs";
4
5
  import { ochreLanguageCodeSchema } from "../schemas.mjs";
5
- import * as v from "valibot";
6
6
  // src/parsers/languages.ts
7
7
  /**
8
8
  A content entry carrying a language tag
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { OCHRE_ENDPOINT, PERMANENT_IDENTIFICATION_URL_PREFIX, TEXT_ANNOTATION_UUID, getOchreItemUrl } from "../constants.mjs";
2
3
  import { normalizePropertyVariableLabel } from "../getters.mjs";
3
4
  import { serializeMDXContent, serializeMDXText } from "./mdx.mjs";
@@ -5,7 +6,6 @@ import { MultilingualString } from "./multilingual.mjs";
5
6
  import { readPropertyValueText, readPropertyValueUuid } from "./property-token.mjs";
6
7
  import { renderOptionsSchema } from "../schemas.mjs";
7
8
  import { getXMLSourceIndex } from "../xml/metadata.mjs";
8
- import * as v from "valibot";
9
9
  // src/parsers/string.ts
10
10
  const TEXT_ANNOTATION_TOKEN = "text-annotation";
11
11
  const TEXT_STYLING_TOKEN = "text-styling";
@@ -1,3 +1,4 @@
1
+ import * as v from "valibot";
1
2
  import { normalizeItemCategory } from "../../categories.mjs";
2
3
  import { getProperty } from "../../getters.mjs";
3
4
  import { parseStringContent } from "../helpers.mjs";
@@ -11,7 +12,6 @@ import { websitePresentationReader } from "./reader.mjs";
11
12
  import { parseResponsiveCssStyles } from "./styles.mjs";
12
13
  import { normalizeWebsiteResources } from "./walk.mjs";
13
14
  import { collectionVariantSchema, componentSchema } from "../../schemas.mjs";
14
- import * as v from "valibot";
15
15
  // src/parsers/website/components.ts
16
16
  /**
17
17
  Default values for a collection's display properties, shared between the
@@ -1,5 +1,5 @@
1
- import { Query } from "./types/index.mjs";
2
1
  import * as v from "valibot";
2
+ import { Query } from "./types/index.mjs";
3
3
  /**
4
4
  Validates a pseudo-UUID string
5
5
  @param value - The string to validate
package/dist/schemas.mjs CHANGED
@@ -1,6 +1,6 @@
1
+ import * as v from "valibot";
1
2
  import "./constants.mjs";
2
3
  import { getOchreLanguageCode, isOchreLanguageCode } from "./languages.mjs";
3
- import * as v from "valibot";
4
4
  // src/schemas.ts
5
5
  const PSEUDO_UUID_REGEX = /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/i;
6
6
  /**
@@ -370,12 +370,17 @@ page, and is null unless context was requested. Its `isTruncatedBefore` and
370
370
  `isTruncatedAfter` flags are true when the page has more words beyond the
371
371
  context.
372
372
 
373
+ `id` is unique among the matches of the requested item and is the same for
374
+ the same match on every fetch while its OCR layer is unchanged, so it can key
375
+ a list or a selection. It is opaque and is not meant to be parsed.
376
+
373
377
  `resourceUuid` is the Resource that owns the page's OCR layer, which differs
374
378
  from the requested item when the OCR lives on a child Resource.
375
379
  `pageNumber` is the 1-based position of that Resource among its sibling
376
380
  Resources, which is the page order of a multi-page document.
377
381
  */
378
382
  export type OcrMatch = {
383
+ id: string;
379
384
  resourceUuid: string | null;
380
385
  pageNumber: number | null;
381
386
  text: string;
@@ -1,5 +1,5 @@
1
- import { isObject, readEntries, readProperty } from "../reflection.mjs";
2
1
  import { XMLParser } from "fast-xml-parser";
2
+ import { isObject, readEntries, readProperty } from "../reflection.mjs";
3
3
  // src/xml/metadata.ts
4
4
  const XML_METADATA_SYMBOL = XMLParser.getMetaDataSymbol();
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { XMLBibliography, XMLData as XMLData$1, XMLDataItem as XMLDataItem$1, XMLItemLinksData as XMLItemLinksData$1, XMLLink as XMLLink$1, XMLPeriod, XMLPerson, XMLProperty, XMLResource, XMLSetItemsData as XMLSetItemsData$1, XMLString, XMLWebsiteResource, XMLWebsiteResourceItem } from "./types.mjs";
2
1
  import * as v from "valibot";
2
+ import { XMLBibliography, XMLData as XMLData$1, XMLDataItem as XMLDataItem$1, XMLItemLinksData as XMLItemLinksData$1, XMLLink as XMLLink$1, XMLPeriod, XMLPerson, XMLProperty, XMLResource, XMLSetItemsData as XMLSetItemsData$1, XMLString, XMLWebsiteResource, XMLWebsiteResourceItem } from "./types.mjs";
3
3
  export declare const XMLLink: v.GenericSchema<unknown, XMLLink$1>;
4
4
  export declare const XMLDataItem: v.GenericSchema<unknown, XMLDataItem$1>;
5
5
  export declare const XMLItemLinksData: v.GenericSchema<unknown, XMLItemLinksData$1>;
@@ -1,7 +1,7 @@
1
+ import * as v from "valibot";
1
2
  import { readArrayProperty } from "../reflection.mjs";
2
3
  import { isPseudoUuid } from "../schemas.mjs";
3
4
  import { parseDateTime } from "./dates.mjs";
4
- import * as v from "valibot";
5
5
  // src/xml/schemas.ts
6
6
  function getXMLStringPayload(value) {
7
7
  return typeof value === "string" ? value : value.payload ?? null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "1.3.33",
3
+ "version": "1.3.34",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Node.js library for working with OCHRE (Online Cultural and Historical Research Environment) data",
@@ -46,23 +46,23 @@
46
46
  ],
47
47
  "dependencies": {
48
48
  "@nodable/entities": "^3.1.0",
49
- "fast-equals": "^6.2.2",
50
- "fast-xml-parser": "^5.12.0",
49
+ "fast-equals": "^6.4.2",
50
+ "fast-xml-parser": "^5.12.1",
51
51
  "valibot": "^1.5.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@antfu/eslint-config": "^10.0.0",
55
- "@types/node": "^26.6.6",
55
+ "@types/node": "^26.6.11",
56
56
  "bumpp": "^12.3.0",
57
- "eslint": "^10.14.0",
58
- "eslint-plugin-erasable-syntax-only": "^0.7.2",
57
+ "eslint": "^10.16.0",
58
+ "eslint-plugin-erasable-syntax-only": "^0.7.3",
59
59
  "eslint-plugin-slop": "^0.1.3",
60
- "eslint-plugin-sonarjs": "^4.2.2",
61
- "knip": "^6.45.1",
62
- "oxfmt": "^0.78.0",
60
+ "eslint-plugin-sonarjs": "^4.2.3",
61
+ "knip": "^6.49.0",
62
+ "oxfmt": "^0.81.0",
63
63
  "tsdown": "^0.23.7",
64
64
  "typescript": "^6.0.3",
65
- "vitest": "^5.1.0"
65
+ "vitest": "^5.1.2"
66
66
  },
67
67
  "scripts": {
68
68
  "dev": "tsdown src/index.ts --watch",