Skip to content

Commit 185c291

Browse files
committed
feat: implement MDX serialization and integrate with multilingual and string parsers
1 parent 05d8b08 commit 185c291

3 files changed

Lines changed: 70 additions & 24 deletions

File tree

src/parsers/mdx.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const MDX_LITERAL_EXPRESSION_REGEX = /[!#*<>[\]_`{|}~]/;
2+
const MDX_LITERAL_BLOCK_REGEX =
3+
/(?:^|\n)[\t ]*(?:import|export|[+>-]|\d+[).])(?:[\t ]|$)/;
4+
5+
export function serializeMDXText(value: string): string {
6+
if (
7+
value === "" ||
8+
(!MDX_LITERAL_EXPRESSION_REGEX.test(value) &&
9+
!MDX_LITERAL_BLOCK_REGEX.test(value))
10+
) {
11+
return value;
12+
}
13+
14+
return `{${JSON.stringify(value)}}`;
15+
}

src/parsers/multilingual.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DEFAULT_LANGUAGES } from "#/constants.js";
2+
import { serializeMDXText } from "#/parsers/mdx.js";
23

34
/**
45
* One text entry for a language. When OCHRE exposes multiple entries for the
@@ -62,8 +63,11 @@ function normalizeInputText(
6263
text: MultilingualStringInput,
6364
): MultilingualStringText {
6465
return typeof text === "string"
65-
? { text, richText: text }
66-
: { text: text.text, richText: text.richText ?? text.text };
66+
? { text, richText: serializeMDXText(text) }
67+
: {
68+
text: text.text,
69+
richText: text.richText ?? serializeMDXText(text.text),
70+
};
6771
}
6872

6973
function normalizeAliases(

src/parsers/string.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
TEXT_ANNOTATION_TEXT_STYLING_VARIANT_UUID,
1717
TEXT_ANNOTATION_UUID,
1818
} from "#/constants.js";
19+
import { serializeMDXText } from "#/parsers/mdx.js";
1920
import { MultilingualString } from "#/parsers/multilingual.js";
2021
import { renderOptionsSchema, whitespaceSchema } from "#/schemas.js";
2122
import { getXMLSourceIndex } from "#/xml/metadata.js";
@@ -254,10 +255,8 @@ function parseXMLStringPayload(
254255
string: XMLString,
255256
options: { rendering: TextRendering },
256257
): string {
257-
return (string.payload ?? "")
258-
.replaceAll("<", options.rendering === "rich" ? String.raw`\<` : "<")
259-
.replaceAll("{", String.raw`\{`)
260-
.replaceAll("}", String.raw`\}`);
258+
const payload = string.payload ?? "";
259+
return options.rendering === "rich" ? serializeMDXText(payload) : payload;
261260
}
262261

263262
export function parseXMLString(string: XMLString): MultilingualStringText {
@@ -304,28 +303,40 @@ function createMDXComponent(
304303

305304
switch (variant) {
306305
case "inlineImage": {
307-
returnString = `<InlineImage uuid="${uuid}"${createMDXStringAttribute(
306+
returnString = `<InlineImage${createMDXStringAttribute(
307+
"uuid",
308+
uuid ?? "null",
309+
)}${createMDXStringAttribute(
308310
"content",
309311
content,
310312
)} height={${height ?? "null"}} width={${width ?? "null"}} />`;
311313
break;
312314
}
313315
case "internalLink": {
314-
returnString = `<InternalLink uuid="${uuid}"${createMDXStringAttribute(
316+
returnString = `<InternalLink${createMDXStringAttribute(
317+
"uuid",
318+
uuid ?? "null",
319+
)}${createMDXStringAttribute(
315320
"content",
316321
tooltipContent,
317322
)}>${text}</InternalLink>`;
318323
break;
319324
}
320325
case "externalLink": {
321-
returnString = `<ExternalLink href="${href == null ? "#" : transformPermanentIdentificationUrl(href)}"${createMDXStringAttribute(
326+
returnString = `<ExternalLink${createMDXStringAttribute(
327+
"href",
328+
href == null ? "#" : transformPermanentIdentificationUrl(href),
329+
)}${createMDXStringAttribute(
322330
"content",
323331
tooltipContent,
324332
)}>${text}</ExternalLink>`;
325333
break;
326334
}
327335
case "documentLink": {
328-
returnString = String.raw`<ExternalLink href="https:\/\/ochre.lib.uchicago.edu/ochre/v2/ochre.php?uuid=${uuid}&load"${createMDXStringAttribute(
336+
returnString = `<ExternalLink${createMDXStringAttribute(
337+
"href",
338+
`https://ochre.lib.uchicago.edu/ochre/v2/ochre.php?uuid=${uuid}&load`,
339+
)}${createMDXStringAttribute(
329340
"content",
330341
tooltipContent,
331342
)}>${text}</ExternalLink>`;
@@ -693,11 +704,13 @@ function wrapWithTextStyling(
693704
return content;
694705
}
695706

696-
return `<Annotation type="text-styling" variant="${textStyling.variant}" size="${textStyling.size}"${
697-
textStyling.headingLevel != null
698-
? ` headingLevel="${textStyling.headingLevel}"`
699-
: ""
700-
}${
707+
return `<Annotation type="text-styling"${createMDXStringAttribute(
708+
"variant",
709+
textStyling.variant,
710+
)}${createMDXStringAttribute("size", textStyling.size)}${createMDXStringAttribute(
711+
"headingLevel",
712+
textStyling.headingLevel ?? undefined,
713+
)}${
701714
textStyling.cssStyles.length > 0
702715
? ` cssStyles={{default: ${JSON.stringify(textStyling.cssStyles)}, tablet: [], mobile: []}}`
703716
: ""
@@ -718,22 +731,36 @@ function createInternalLinkComponent(properties: {
718731

719732
switch (properties.annotationMetadata.linkVariant) {
720733
case "hover-card": {
721-
return `<Annotation type="hover-card" uuid="${properties.uuid}">${innerContent}</Annotation>`;
734+
return `<Annotation type="hover-card"${createMDXStringAttribute(
735+
"uuid",
736+
properties.uuid ?? "null",
737+
)}>${innerContent}</Annotation>`;
722738
}
723739
case "item-page": {
724-
return `<InternalLink type="item" uuid="${properties.uuid}">${innerContent}</InternalLink>`;
740+
return `<InternalLink type="item"${createMDXStringAttribute(
741+
"uuid",
742+
properties.uuid ?? "null",
743+
)}>${innerContent}</InternalLink>`;
725744
}
726745
case "entry-page": {
727-
return `<InternalLink type="entry" uuid="${properties.uuid}">${innerContent}</InternalLink>`;
746+
return `<InternalLink type="entry"${createMDXStringAttribute(
747+
"uuid",
748+
properties.uuid ?? "null",
749+
)}>${innerContent}</InternalLink>`;
728750
}
729751
default: {
730-
return `<InternalLink uuid="${properties.uuid}"${
752+
return `<InternalLink${createMDXStringAttribute(
753+
"uuid",
754+
properties.uuid ?? "null",
755+
)}${
731756
properties.propertyMetadata != null
732-
? ` properties="${properties.propertyMetadata.labelUuid}"${
733-
properties.propertyMetadata.valueUuid != null
734-
? ` value="${properties.propertyMetadata.valueUuid}"`
735-
: ""
736-
}`
757+
? `${createMDXStringAttribute(
758+
"properties",
759+
properties.propertyMetadata.labelUuid,
760+
)}${createMDXStringAttribute(
761+
"value",
762+
properties.propertyMetadata.valueUuid ?? undefined,
763+
)}`
737764
: ""
738765
}${createMDXStringAttribute(
739766
"content",

0 commit comments

Comments
 (0)