ochre-sdk 1.0.14 → 1.0.16

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.
@@ -1,7 +1,7 @@
1
1
  import { TEXT_ANNOTATION_UUID } from "../constants.mjs";
2
2
  import { serializeMDXText } from "./mdx.mjs";
3
3
  import { MultilingualString } from "./multilingual.mjs";
4
- import { renderOptionsSchema, whitespaceSchema } from "../schemas.mjs";
4
+ import { renderOptionsSchema } from "../schemas.mjs";
5
5
  import { getXMLSourceIndex } from "../xml/metadata.mjs";
6
6
  import * as v from "valibot";
7
7
  //#region src/parsers/string.ts
@@ -63,49 +63,26 @@ function applyMDXRenderElements(contentString, options) {
63
63
  }
64
64
  return result;
65
65
  }
66
- /**
67
- * Applies whitespace options to a string (newline)
68
- *
69
- * @param contentString - The string content to modify
70
- * @param whitespace - Space-separated string of whitespace options
71
- * @param rendering - Which text rendering to produce
72
- * @returns String with whitespace modifications applied
73
- *
74
- * @internal
75
- */
76
- function parseWhitespace(contentString, whitespace, rendering) {
77
- let returnString = contentString;
78
- const { success, output } = v.safeParse(whitespaceSchema, whitespace);
79
- if (!success) return contentString;
80
- for (const option of output) switch (option) {
81
- case "newline":
82
- if (rendering === "rich") returnString = returnString.trim() === "***" ? `${returnString}\n` : `<br />\n${returnString}`;
83
- else returnString = `\n${returnString}`;
84
- break;
85
- case "trailing":
86
- returnString = `${returnString} `;
87
- break;
88
- case "leading":
89
- returnString = ` ${returnString}`;
90
- break;
91
- }
92
- return returnString.replaceAll("&#39;", "'");
66
+ function applyNewlineWhitespace(contentString, whitespace, rendering) {
67
+ if (whitespace == null) return contentString;
68
+ if (!whitespace.split(" ").includes("newline")) return contentString;
69
+ if (rendering === "rich") return contentString.trim() === "***" ? `${contentString}\n` : `<br />\n${contentString}`;
70
+ return `\n${contentString}`;
93
71
  }
94
72
  /**
95
- * Parses XML string into a formatted string with whitespace and rendering options
73
+ * Parses XML string into a formatted string with rendering options
96
74
  *
97
75
  * @param string - XML string to parse
98
76
  * @param options - Options for parsing
99
77
  * @param options.rendering - Which text rendering to produce
100
- * @returns Formatted string with whitespace and rendering options
78
+ * @returns Formatted string with rendering options
101
79
  *
102
80
  * @internal
103
81
  */
104
82
  function parseXMLStringVariant(string, options) {
105
83
  let returnString = parseXMLStringPayload(string, options);
106
84
  if (string.rend != null) returnString = parseRenderOptions(returnString, string.rend, options.rendering);
107
- if (string.whitespace != null) returnString = parseWhitespace(returnString, string.whitespace, options.rendering);
108
- return returnString;
85
+ return applyNewlineWhitespace(returnString, string.whitespace, options.rendering);
109
86
  }
110
87
  function parseXMLStringPayload(string, options) {
111
88
  const payload = string.payload ?? "";
@@ -162,9 +139,6 @@ function createMDXStringAttribute(name, value) {
162
139
  if (value == null || value === "") return "";
163
140
  return ` ${name}=${MDX_QUOTED_ATTRIBUTE_ESCAPE_REGEX.test(value) ? `{${JSON.stringify(value)}}` : `"${value}"`}`;
164
141
  }
165
- function applyWhitespaceToResult(result, whitespace, rendering) {
166
- return whitespace == null ? result : parseWhitespace(result, whitespace, rendering);
167
- }
168
142
  function getPropertyValueUuid(property) {
169
143
  const value = property?.value?.[0];
170
144
  return value?.uuid == null || value.uuid === "" ? null : value.uuid;
@@ -264,17 +238,17 @@ function hasRichTextEnvelope(item) {
264
238
  return item.properties?.property[0] != null || getXMLRichTextLinks(item).length > 0;
265
239
  }
266
240
  function parseXMLStringItem(item, contentItem, options) {
267
- if (!(item.payload != null || item.string != null) && getXMLRichTextLinks(item).length === 0) return item.whitespace == null ? "" : parseWhitespace("", item.whitespace, options.rendering);
241
+ if (!(item.payload != null || item.string != null) && getXMLRichTextLinks(item).length === 0) return applyNewlineWhitespace("", item.whitespace, options.rendering);
268
242
  if (hasRichTextEnvelope(item)) {
269
243
  let linkString = item.payload != null ? parseXMLStringPayload(item, { rendering: options.rendering }) : parseNestedStringItems(item.string ?? [], contentItem, { ...options });
270
244
  if (item.rend != null) linkString = parseRenderOptions(linkString, item.rend, options.rendering);
271
- if (options.rendering === "plain") return applyWhitespaceToResult(linkString, item.whitespace, options.rendering);
245
+ if (options.rendering === "plain") return applyNewlineWhitespace(linkString, item.whitespace, options.rendering);
272
246
  return renderRichTextItem(item, linkString, contentItem, options);
273
247
  }
274
248
  if (item.payload != null) return parseXMLStringVariant(item, { rendering: options.rendering });
275
249
  let result = parseNestedStringItems(item.string ?? [], contentItem, options);
276
250
  if (item.rend != null) result = parseRenderOptions(result, item.rend, options.rendering);
277
- return applyWhitespaceToResult(result, item.whitespace, options.rendering);
251
+ return applyNewlineWhitespace(result, item.whitespace, options.rendering);
278
252
  }
279
253
  function parseNestedStringItems(items, contentItem, options) {
280
254
  let result = "";
@@ -324,7 +298,7 @@ function renderRichTextItem(item, linkString, contentItem, options) {
324
298
  const { languages, rendering } = options;
325
299
  const annotationMetadata = extractAnnotationMetadata(item, { language: contentItem.lang });
326
300
  const links = getXMLRichTextLinks(item);
327
- if (links.length === 0) return applyWhitespaceToResult(wrapWithTextStyling(linkString, annotationMetadata.textStyling), item.whitespace, rendering);
301
+ if (links.length === 0) return applyNewlineWhitespace(wrapWithTextStyling(linkString, annotationMetadata.textStyling), item.whitespace, rendering);
328
302
  let result = "";
329
303
  for (const link of links) {
330
304
  const linkContent = link.identification != null ? "content" in link.identification.label ? parseXMLContent(link.identification.label, { languages }) : MultilingualString.create(contentItem.lang, parseXMLString(link.identification.label), languages) : MultilingualString.create(contentItem.lang, "", languages);
@@ -341,7 +315,7 @@ function renderRichTextItem(item, linkString, contentItem, options) {
341
315
  content: contentText,
342
316
  text: linkString
343
317
  });
344
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
318
+ result += component;
345
319
  } else if (link.publicationDateTime != null) {
346
320
  const component = createInternalLinkComponent({
347
321
  uuid: getLinkStringProperty(link, "uuid"),
@@ -349,14 +323,14 @@ function renderRichTextItem(item, linkString, contentItem, options) {
349
323
  content: contentText,
350
324
  annotationMetadata
351
325
  });
352
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
326
+ result += component;
353
327
  } else {
354
328
  const component = createMDXComponent("tooltipSpan", {
355
329
  uuid: getLinkStringProperty(link, "uuid"),
356
330
  text: linkString,
357
331
  content: contentText
358
332
  });
359
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
333
+ result += component;
360
334
  }
361
335
  break;
362
336
  case "internalDocument": {
@@ -367,7 +341,7 @@ function renderRichTextItem(item, linkString, contentItem, options) {
367
341
  annotationMetadata,
368
342
  propertyMetadata: getFirstPropertyMetadata(item)
369
343
  });
370
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
344
+ result += component;
371
345
  break;
372
346
  }
373
347
  case "externalDocument": {
@@ -380,7 +354,7 @@ function renderRichTextItem(item, linkString, contentItem, options) {
380
354
  text: linkString,
381
355
  content: contentText
382
356
  });
383
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
357
+ result += component;
384
358
  break;
385
359
  }
386
360
  case "webpage": {
@@ -390,7 +364,7 @@ function renderRichTextItem(item, linkString, contentItem, options) {
390
364
  text: linkString,
391
365
  content: contentText
392
366
  });
393
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
367
+ result += component;
394
368
  break;
395
369
  }
396
370
  }
@@ -401,17 +375,17 @@ function renderRichTextItem(item, linkString, contentItem, options) {
401
375
  content: contentText,
402
376
  annotationMetadata
403
377
  });
404
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
378
+ result += component;
405
379
  } else {
406
380
  const component = createMDXComponent("tooltipSpan", {
407
381
  uuid: getLinkStringProperty(link, "uuid"),
408
382
  text: linkString,
409
383
  content: contentText
410
384
  });
411
- result += applyWhitespaceToResult(component, item.whitespace, rendering);
385
+ result += component;
412
386
  }
413
387
  }
414
- return result;
388
+ return applyNewlineWhitespace(result, item.whitespace, rendering);
415
389
  }
416
390
  /**
417
391
  * Parses rich text content into a formatted string with links and annotations
@@ -32,11 +32,6 @@ declare const gallerySchema: v.ObjectSchema<{
32
32
  * @internal
33
33
  */
34
34
  declare const renderOptionsSchema: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.TransformAction<string, string[]>, v.ArraySchema<v.PicklistSchema<["bold", "italic", "underline"], undefined>, undefined>]>;
35
- /**
36
- * Schema for validating and parsing whitespace options
37
- * @internal
38
- */
39
- declare const whitespaceSchema: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.TransformAction<string, string[]>, v.ArraySchema<v.PicklistSchema<["newline", "trailing", "leading"], undefined>, undefined>]>;
40
35
  /**
41
36
  * Schema for validating the parameters for the Set property values fetching function
42
37
  * @internal
@@ -81,4 +76,4 @@ declare const setItemsParamsSchema: v.ObjectSchema<{
81
76
  readonly pageSize: v.OptionalSchema<v.GenericSchema<unknown, number>, 48>;
82
77
  }, undefined>;
83
78
  //#endregion
84
- export { componentSchema, gallerySchema, iso639_3Schema, renderOptionsSchema, setItemsParamsSchema, setPropertyValuesParamsSchema, uuidSchema, whitespaceSchema };
79
+ export { componentSchema, gallerySchema, iso639_3Schema, renderOptionsSchema, setItemsParamsSchema, setPropertyValuesParamsSchema, uuidSchema };
package/dist/schemas.mjs CHANGED
@@ -63,15 +63,6 @@ const renderOptionsSchema = v.pipe(v.string(), v.transform((str) => str.split("
63
63
  "underline"
64
64
  ])));
65
65
  /**
66
- * Schema for validating and parsing whitespace options
67
- * @internal
68
- */
69
- const whitespaceSchema = v.pipe(v.string(), v.transform((str) => str.split(" ")), v.array(v.picklist([
70
- "newline",
71
- "trailing",
72
- "leading"
73
- ])));
74
- /**
75
66
  * Schema for validating date data types
76
67
  * @internal
77
68
  */
@@ -229,4 +220,4 @@ const setItemsParamsSchema = v.object({
229
220
  pageSize: v.optional(positiveNumber("Page size must be positive"), 48)
230
221
  });
231
222
  //#endregion
232
- export { componentSchema, gallerySchema, iso639_3Schema, renderOptionsSchema, setItemsParamsSchema, setPropertyValuesParamsSchema, uuidSchema, whitespaceSchema };
223
+ export { componentSchema, gallerySchema, iso639_3Schema, renderOptionsSchema, setItemsParamsSchema, setPropertyValuesParamsSchema, uuidSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
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",
@@ -64,8 +64,8 @@
64
64
  "scripts": {
65
65
  "dev": "tsdown src/index.ts --watch",
66
66
  "build": "tsdown",
67
- "lint": "knip && eslint .",
68
- "lint:fix": "knip fix && eslint . --fix",
67
+ "lint": "knip; eslint .",
68
+ "lint:fix": "knip --fix; eslint . --fix",
69
69
  "format": "prettier --check .",
70
70
  "format:fix": "prettier --write --list-different .",
71
71
  "check-types": "tsc --noEmit",