Merged in fix/rte-nested-links (pull request #785)
fix(SW-714): rendering of RTE nested links/references
This commit is contained in:
@@ -42,6 +42,17 @@ import type {
|
|||||||
import { RTEMarkType } from "@/types/transitionTypes/rte/node"
|
import { RTEMarkType } from "@/types/transitionTypes/rte/node"
|
||||||
import type { RenderOptions } from "@/types/transitionTypes/rte/option"
|
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) {
|
function extractPossibleAttributes(attrs: Attributes | undefined) {
|
||||||
if (!attrs) return {}
|
if (!attrs) return {}
|
||||||
const props: Record<string, any> = {}
|
const props: Record<string, any> = {}
|
||||||
@@ -82,7 +93,13 @@ export const renderOptions: RenderOptions = {
|
|||||||
variant="underscored"
|
variant="underscored"
|
||||||
color="burgundy"
|
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>
|
</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
|
// If entry is not an ImageContainer, it is a page and we return it as a link
|
||||||
const props = extractPossibleAttributes(node.attrs)
|
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 (
|
return (
|
||||||
<Link
|
<Link
|
||||||
{...props}
|
{...props}
|
||||||
href={href}
|
href={entry.node.url}
|
||||||
key={node.uid}
|
key={node.uid}
|
||||||
variant="underscored"
|
variant="underscored"
|
||||||
color="burgundy"
|
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>
|
</Link>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user