Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
475 lines
11 KiB
TypeScript
475 lines
11 KiB
TypeScript
import Image from "@/components/Image"
|
|
import Link from "@/components/TempDesignSystem/Link"
|
|
|
|
import styles from "./currentRenderOptions.module.css"
|
|
|
|
import type { EmbedByUid } from "@/types/components/deprecatedjsontohtml"
|
|
import { EmbedEnum } from "@/types/requests/utils/embeds"
|
|
import type { Attributes } from "@/types/rte/attrs"
|
|
import { RTEItemTypeEnum, RTETypeEnum } from "@/types/rte/enums"
|
|
import {
|
|
type RTEDefaultNode,
|
|
RTEMarkType,
|
|
type RTENext,
|
|
type RTENode,
|
|
type RTERegularNode,
|
|
} from "@/types/rte/node"
|
|
import type { RenderOptions } from "@/types/rte/option"
|
|
|
|
function extractPossibleAttributes(attrs: Attributes | undefined) {
|
|
if (!attrs) return {}
|
|
const props: Record<string, any> = {}
|
|
if (attrs.id) {
|
|
props.id = attrs.id
|
|
}
|
|
|
|
if (attrs.class) {
|
|
props.className = attrs.class
|
|
} else if (attrs["class-name"]) {
|
|
props.className = attrs["class-name"]
|
|
} else if (attrs.classname) {
|
|
props.className = attrs.classname
|
|
} else if (attrs?.style?.["text-align"]) {
|
|
props.style = {
|
|
textAlign: attrs?.style?.["text-align"],
|
|
}
|
|
}
|
|
|
|
return props
|
|
}
|
|
|
|
export const renderOptions: RenderOptions = {
|
|
[RTETypeEnum.a]: (
|
|
node: RTERegularNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
if (node.attrs.url) {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<a
|
|
{...props}
|
|
href={node.attrs.url}
|
|
target={node.attrs.target ?? "_blank"}
|
|
key={node.uid}
|
|
>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</a>
|
|
)
|
|
}
|
|
return null
|
|
},
|
|
|
|
[RTETypeEnum.blockquote]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<blockquote key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</blockquote>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.code]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<code key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</code>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.embed]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
if (node.attrs.src) {
|
|
props.src = node.attrs.src
|
|
}
|
|
if (node.attrs.url) {
|
|
props.src = node.attrs.url
|
|
}
|
|
if (!props.src) {
|
|
return null
|
|
}
|
|
return (
|
|
<iframe key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</iframe>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.h1]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<h1 key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</h1>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.h2]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<h2 key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</h2>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.h3]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<h3 key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</h3>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.h4]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<h4 key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</h4>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.h5]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<h5 key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</h5>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.h6]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<h6 key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</h6>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.hr]: () => {
|
|
return <hr />
|
|
},
|
|
|
|
[RTETypeEnum.li]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<li key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</li>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.ol]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<ol key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</ol>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.p]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<p {...props} key={node.uid}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</p>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.reference]: (
|
|
node: RTENode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
if ("attrs" in node) {
|
|
const type = node.attrs.type
|
|
if (type === RTEItemTypeEnum.asset) {
|
|
const image = embeds?.[node?.attrs?.["asset-uid"]]
|
|
if (image?.node.__typename === EmbedEnum.SysAsset) {
|
|
const alt = image?.node?.title ?? node.attrs.alt
|
|
const alignment = node.attrs?.style?.["text-align"]
|
|
? {
|
|
alignSelf: node.attrs?.style?.["text-align"],
|
|
}
|
|
: {}
|
|
return (
|
|
<Image
|
|
key={node.uid}
|
|
alt={alt}
|
|
className={styles.image}
|
|
height={image.node.dimension.height}
|
|
src={image?.node?.url}
|
|
width={image.node.dimension.width}
|
|
style={alignment}
|
|
/>
|
|
)
|
|
}
|
|
} else {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
const href = node.attrs?.locale
|
|
? `/${node.attrs.locale}${node.attrs.href}`
|
|
: node.attrs.href
|
|
return (
|
|
<Link {...props} href={href} key={node.uid}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</Link>
|
|
)
|
|
}
|
|
}
|
|
|
|
return null
|
|
},
|
|
|
|
[RTETypeEnum.table]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<table key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</table>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.thead]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<thead key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</thead>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.tbody]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<tbody key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</tbody>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.tfoot]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<tfoot key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</tfoot>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.tr]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<tr key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</tr>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.th]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<th key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</th>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.td]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<td key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</td>
|
|
)
|
|
},
|
|
|
|
[RTETypeEnum.ul]: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
const props = extractPossibleAttributes(node.attrs)
|
|
return (
|
|
<ul key={node.uid} {...props}>
|
|
{next(node.children, embeds, fullRenderOptions)}
|
|
</ul>
|
|
)
|
|
},
|
|
|
|
/** TextNode wrappers */
|
|
[RTEMarkType.bold]: (children: React.ReactNode) => {
|
|
return <strong>{children}</strong>
|
|
},
|
|
|
|
[RTEMarkType.italic]: (children: React.ReactNode) => {
|
|
return <em>{children}</em>
|
|
},
|
|
|
|
[RTEMarkType.underline]: (children: React.ReactNode) => {
|
|
return <u>{children}</u>
|
|
},
|
|
|
|
[RTEMarkType.strikethrough]: (children: React.ReactNode) => {
|
|
return <s>{children}</s>
|
|
},
|
|
|
|
[RTEMarkType.inlineCode]: (children: React.ReactNode) => {
|
|
return <span>{children}</span>
|
|
},
|
|
|
|
[RTEMarkType.subscript]: (children: React.ReactNode) => {
|
|
return <sub>{children}</sub>
|
|
},
|
|
|
|
[RTEMarkType.superscript]: (children: React.ReactNode) => {
|
|
return <sup>{children}</sup>
|
|
},
|
|
|
|
[RTEMarkType.break]: (children: React.ReactNode) => {
|
|
return (
|
|
<>
|
|
<br />
|
|
{children}
|
|
</>
|
|
)
|
|
},
|
|
|
|
[RTEMarkType.classnameOrId]: (
|
|
children: React.ReactNode,
|
|
className?: string,
|
|
id?: string
|
|
) => {
|
|
let props = {
|
|
className,
|
|
id,
|
|
}
|
|
if (!className) {
|
|
delete props.className
|
|
}
|
|
if (!id) {
|
|
delete props.id
|
|
}
|
|
return (
|
|
<span key={id} {...props}>
|
|
{children}
|
|
</span>
|
|
)
|
|
},
|
|
|
|
/**
|
|
* Contentstack can return something called `default` as seen here in their
|
|
* own SDK (https://github.com/contentstack/contentstack-utils-javascript/blob/master/src/options/default-node-options.ts#L89)
|
|
*/
|
|
default: (
|
|
node: RTEDefaultNode,
|
|
embeds: EmbedByUid,
|
|
next: RTENext,
|
|
fullRenderOptions: RenderOptions
|
|
) => {
|
|
return next(node.children, embeds, fullRenderOptions)
|
|
},
|
|
}
|