ochre-sdk 1.0.39 → 1.0.41

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.
@@ -15,7 +15,11 @@ const HEADING_LEVEL_TOKEN = "heading-level";
15
15
  const RAW_MDX_BLOCK_DELIMITER = "``md``";
16
16
  const RAW_MDX_BLOCK_PLACEHOLDER_PREFIX = "\0raw-mdx-block:";
17
17
  const RAW_MDX_BLOCK_PLACEHOLDER_SUFFIX = "\0";
18
+ const RICH_LINE_BREAK = "<br />\n";
18
19
  const MDX_QUOTED_ATTRIBUTE_ESCAPE_REGEX = /[\n\r"]/;
20
+ const MDX_NEWLINE_RUN_REGEX = /\n{3,}/g;
21
+ const MDX_SIMPLE_INLINE_TAG_REGEX = /<\/?(?:em|strong|u)>/g;
22
+ const MDX_MARKDOWN_LIST_LINE_REGEX = /^\s*(?:[*+-]|\d+[).])\s+/;
19
23
  const MDX_RENDER_ELEMENTS = {
20
24
  bold: "strong",
21
25
  italic: "em",
@@ -24,6 +28,47 @@ const MDX_RENDER_ELEMENTS = {
24
28
  function hasNewlineWhitespace(value) {
25
29
  return value?.split(" ").includes("newline") === true;
26
30
  }
31
+ function hasNoRichTextPayload(item) {
32
+ return (item.payload == null || item.payload === "") && item.rend == null && item.links == null && item.properties == null && item.annotation == null && item.string == null;
33
+ }
34
+ function isRawMDXBlockDelimiter(item, index) {
35
+ return item.payload === RAW_MDX_BLOCK_DELIMITER && (index === 0 || hasNewlineWhitespace(item.whitespace)) && item.rend == null && item.links == null && item.properties == null && item.annotation == null && item.string == null;
36
+ }
37
+ function normalizeMDXNewlineRuns(value) {
38
+ return value.replaceAll(MDX_NEWLINE_RUN_REGEX, "\n\n");
39
+ }
40
+ function isMarkdownListLine(value) {
41
+ return MDX_MARKDOWN_LIST_LINE_REGEX.test(value);
42
+ }
43
+ function isStandaloneFormattedLine(value) {
44
+ const trimmedValue = value.trim();
45
+ if (trimmedValue === "" || isMarkdownListLine(trimmedValue)) return false;
46
+ const unwrappedValue = trimmedValue.replaceAll(MDX_SIMPLE_INLINE_TAG_REGEX, "").trim();
47
+ return unwrappedValue !== trimmedValue && unwrappedValue !== "";
48
+ }
49
+ function pushBlankLine(lines) {
50
+ if (lines.length > 0 && lines.at(-1) !== "") lines.push("");
51
+ }
52
+ function getLastNonBlankLine(lines) {
53
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
54
+ const line = lines[index];
55
+ if (line != null && line.trim() !== "") return line;
56
+ }
57
+ return null;
58
+ }
59
+ function normalizeRawMDXBlock(value) {
60
+ const sourceLines = normalizeMDXNewlineRuns(value).split("\n");
61
+ const normalizedLines = [];
62
+ for (const [index, line] of sourceLines.entries()) {
63
+ const nextLine = sourceLines[index + 1];
64
+ const shouldStartOwnParagraph = isStandaloneFormattedLine(line);
65
+ const lastNonBlankLine = getLastNonBlankLine(normalizedLines);
66
+ if (shouldStartOwnParagraph || isMarkdownListLine(line) && (lastNonBlankLine == null || !isMarkdownListLine(lastNonBlankLine))) pushBlankLine(normalizedLines);
67
+ normalizedLines.push(line);
68
+ if (shouldStartOwnParagraph && nextLine != null && nextLine.trim() !== "" && !isMarkdownListLine(nextLine)) pushBlankLine(normalizedLines);
69
+ }
70
+ return normalizeMDXNewlineRuns(normalizedLines.join("\n"));
71
+ }
27
72
  function isXMLRichTextLink(value) {
28
73
  return typeof value === "object" && value != null;
29
74
  }
@@ -72,7 +117,8 @@ function applyMDXRenderElements(contentString, options) {
72
117
  function applyNewlineWhitespace(contentString, whitespace, rendering) {
73
118
  if (whitespace == null) return contentString;
74
119
  if (!hasNewlineWhitespace(whitespace)) return contentString;
75
- if (rendering === "rich") return contentString.trim() === "***" ? `${contentString}\n` : `<br />\n${contentString}`;
120
+ if (contentString === "") return rendering === "rich" ? RICH_LINE_BREAK : "\n";
121
+ if (rendering === "rich") return contentString.trim() === "***" ? `${contentString}\n` : `${RICH_LINE_BREAK}${contentString}`;
76
122
  return `\n${contentString}`;
77
123
  }
78
124
  /**
@@ -260,7 +306,7 @@ function parseNestedStringItems(items, contentItem, options) {
260
306
  let result = "";
261
307
  let rawMDXBlockStartIndex = null;
262
308
  for (const [index, item] of items.entries()) {
263
- if (item.payload === RAW_MDX_BLOCK_DELIMITER && (index === 0 || hasNewlineWhitespace(item.whitespace)) && item.rend == null && item.links == null && item.properties == null && item.annotation == null && item.string == null) {
309
+ if (isRawMDXBlockDelimiter(item, index)) {
264
310
  if (rawMDXBlockStartIndex == null) {
265
311
  rawMDXBlockStartIndex = index;
266
312
  continue;
@@ -276,6 +322,7 @@ function parseNestedStringItems(items, contentItem, options) {
276
322
  });
277
323
  }
278
324
  if (rawMDXBlock !== "" && !rawMDXBlock.endsWith("\n") && hasNewlineWhitespace(item.whitespace)) rawMDXBlock += "\n";
325
+ rawMDXBlock = normalizeRawMDXBlock(rawMDXBlock);
279
326
  if (options.rendering === "rich" && options.rawMDXBlocks != null) {
280
327
  const placeholder = `${RAW_MDX_BLOCK_PLACEHOLDER_PREFIX}${options.rawMDXBlocks.length}${RAW_MDX_BLOCK_PLACEHOLDER_SUFFIX}`;
281
328
  options.rawMDXBlocks.push(rawMDXBlock);
@@ -285,7 +332,13 @@ function parseNestedStringItems(items, contentItem, options) {
285
332
  continue;
286
333
  }
287
334
  if (rawMDXBlockStartIndex != null) continue;
288
- result += parseXMLStringItem(item, contentItem, options);
335
+ const nextItem = items[index + 1];
336
+ if (options.rendering === "rich" && hasNoRichTextPayload(item) && hasNewlineWhitespace(item.whitespace) && nextItem != null && isRawMDXBlockDelimiter(nextItem, index + 1)) {
337
+ result += "\n";
338
+ continue;
339
+ }
340
+ const parsedItem = parseXMLStringItem(item, contentItem, options);
341
+ result += options.rendering === "rich" && result.endsWith("\n") && parsedItem.startsWith(RICH_LINE_BREAK) ? parsedItem.slice(6) : parsedItem;
289
342
  }
290
343
  if (rawMDXBlockStartIndex != null) for (let index = rawMDXBlockStartIndex; index < items.length; index += 1) {
291
344
  const item = items[index];
@@ -464,6 +517,7 @@ function parseXMLContentItem(contentItem, options) {
464
517
  rawMDXBlocks
465
518
  }));
466
519
  for (const [index, rawMDXBlock] of rawMDXBlocks.entries()) serializedRichText = serializedRichText.replaceAll(`${RAW_MDX_BLOCK_PLACEHOLDER_PREFIX}${index}${RAW_MDX_BLOCK_PLACEHOLDER_SUFFIX}`, rawMDXBlock);
520
+ serializedRichText = normalizeMDXNewlineRuns(serializedRichText);
467
521
  return {
468
522
  text: parseNestedStringItems(contentItem.string, contentItem, {
469
523
  ...options,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
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",