Merged in fix/rte-nested-links (pull request #785)

fix(SW-714): rendering of RTE nested links/references
This commit is contained in:
Michael Zetterberg
2024-10-29 12:21:43 +00:00

View File

@@ -42,6 +42,17 @@ import type {
import { RTEMarkType } from "@/types/transitionTypes/rte/node"
import type { RenderOptions } from "@/types/transitionTypes/rte/option"
function noNestedLinksOrReferences(node: RTENode) {
if ("type" in node) {
if (node.type === RTETypeEnum.reference) {
return node.children
} else if (node.type === RTETypeEnum.a) {
return node.children
}
}
return node
}
function extractPossibleAttributes(attrs: Attributes | undefined) {
if (!attrs) return {}
const props: Record<string, any> = {}
@@ -82,7 +93,13 @@ export const renderOptions: RenderOptions = {
variant="underscored"
color="burgundy"
>
{next(node.children, embeds, fullRenderOptions)}
{next(
// Sometimes editors happen to nest a reference inside a link and vice versa.
// In that case use the outermost link, i.e. ignore nested links.
node.children.flatMap(noNestedLinksOrReferences),
embeds,
fullRenderOptions
)}
</Link>
)
}
@@ -340,28 +357,22 @@ export const renderOptions: RenderOptions = {
) {
// If entry is not an ImageContainer, it is a page and we return it as a link
const props = extractPossibleAttributes(node.attrs)
let href = ""
if (entry?.node.__typename === ContentEnum.blocks.AccountPage) {
href = removeMultipleSlashes(
`/${entry.node.system.locale}${entry.node.url}`
)
} else {
href =
entry.node?.web?.original_url ||
removeMultipleSlashes(
`/${entry.node.system.locale}${entry.node.url}`
)
}
return (
<Link
{...props}
href={href}
href={entry.node.url}
key={node.uid}
variant="underscored"
color="burgundy"
>
{next(node.children, embeds, fullRenderOptions)}
{next(
// Sometimes editors happen to nest a reference inside a link and vice versa.
// In that case use the outermost link, i.e. ignore nested links.
node.children.flatMap(noNestedLinksOrReferences),
embeds,
fullRenderOptions
)}
</Link>
)
}