|
| 1 | +import { |
| 2 | + Children, |
| 3 | + isValidElement, |
| 4 | + type ReactElement, |
| 5 | + type ReactNode, |
| 6 | +} from 'react'; |
| 7 | +import { flatMap } from 'lodash'; |
| 8 | +import type { InlinePart, InlineTextProps } from './inlinePartTypes'; |
| 9 | + |
| 10 | +type Recurse = (n: ReactNode) => InlinePart[]; |
| 11 | + |
| 12 | +/** |
| 13 | + * Public for tests; re-exported from serialize module. |
| 14 | + */ |
| 15 | +export function inlinePartsToPlainString(parts: InlinePart[]): string { |
| 16 | + return parts.map((x) => (x.kind === 'text' ? x.value : x.label)).join(''); |
| 17 | +} |
| 18 | + |
| 19 | +function readTextStyleProps(p: Record<string, unknown>): InlineTextProps { |
| 20 | + return { |
| 21 | + bold: p.bold as boolean | undefined, |
| 22 | + italic: p.italic as boolean | undefined, |
| 23 | + underline: p.underline as boolean | undefined, |
| 24 | + strikethrough: p.strikethrough as boolean | undefined, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +const strapiTextHandler = { |
| 29 | + predicate: (el: ReactElement<Record<string, unknown>>) => |
| 30 | + typeof el.props.text === 'string', |
| 31 | + toParts: (el: ReactElement<Record<string, unknown>>, _walk: Recurse) => { |
| 32 | + const p = el.props; |
| 33 | + return [ |
| 34 | + { |
| 35 | + kind: 'text' as const, |
| 36 | + value: p.text as string, |
| 37 | + textProps: readTextStyleProps(p as Record<string, unknown>), |
| 38 | + }, |
| 39 | + ]; |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +const strapiContentLinkHandler = { |
| 44 | + predicate: (el: ReactElement<Record<string, unknown>>) => { |
| 45 | + const c = el.props.content; |
| 46 | + return ( |
| 47 | + c != null && |
| 48 | + typeof c === 'object' && |
| 49 | + (c as { type?: string }).type === 'link' && |
| 50 | + typeof (c as { url?: string }).url === 'string' |
| 51 | + ); |
| 52 | + }, |
| 53 | + toParts: (el: ReactElement<Record<string, unknown>>, _walk: Recurse) => { |
| 54 | + const c = el.props.content as { |
| 55 | + url: string; |
| 56 | + children: Array<{ text?: string }>; |
| 57 | + }; |
| 58 | + const { url, children: linkChildren = [] } = c; |
| 59 | + const label = linkChildren.map((x) => x.text ?? '').join(''); |
| 60 | + return [{ kind: 'link' as const, url, label }]; |
| 61 | + }, |
| 62 | +}; |
| 63 | + |
| 64 | +const hrefLinkHandler = { |
| 65 | + predicate: (el: ReactElement<Record<string, unknown>>) => |
| 66 | + typeof el.props.href === 'string', |
| 67 | + toParts: (el: ReactElement<Record<string, unknown>>, walk: Recurse) => { |
| 68 | + const ch = el.props.children; |
| 69 | + const inner = flatMap(Children.toArray(ch as ReactNode), (n) => walk(n)); |
| 70 | + return [ |
| 71 | + { |
| 72 | + kind: 'link' as const, |
| 73 | + url: el.props.href as string, |
| 74 | + label: inlinePartsToPlainString(inner), |
| 75 | + }, |
| 76 | + ]; |
| 77 | + }, |
| 78 | +}; |
| 79 | + |
| 80 | +const childrenRecursionHandler = { |
| 81 | + predicate: (el: ReactElement<Record<string, unknown>>) => |
| 82 | + el.props.children != null, |
| 83 | + toParts: (el: ReactElement<Record<string, unknown>>, walk: Recurse) => |
| 84 | + flatMap(Children.toArray(el.props.children as ReactNode), (n) => walk(n)), |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * OCP: add new entry here for new Strapi inline node shapes; do not change core walk. |
| 89 | + */ |
| 90 | +const INLINE_NODE_HANDLERS: ReadonlyArray<{ |
| 91 | + predicate: (el: ReactElement<Record<string, unknown>>) => boolean; |
| 92 | + toParts: ( |
| 93 | + el: ReactElement<Record<string, unknown>>, |
| 94 | + walk: Recurse, |
| 95 | + ) => InlinePart[]; |
| 96 | +}> = [ |
| 97 | + strapiTextHandler, |
| 98 | + strapiContentLinkHandler, |
| 99 | + hrefLinkHandler, |
| 100 | + childrenRecursionHandler, |
| 101 | +]; |
| 102 | + |
| 103 | +export function walkInlines(node: ReactNode): InlinePart[] { |
| 104 | + if (node == null || node === false) { |
| 105 | + return []; |
| 106 | + } |
| 107 | + if (typeof node === 'string' || typeof node === 'number') { |
| 108 | + return [{ kind: 'text', value: String(node), textProps: {} }]; |
| 109 | + } |
| 110 | + if (!isValidElement(node)) { |
| 111 | + return []; |
| 112 | + } |
| 113 | + const el = node as ReactElement<Record<string, unknown>>; |
| 114 | + for (const h of INLINE_NODE_HANDLERS) { |
| 115 | + if (h.predicate(el)) { |
| 116 | + return h.toParts(el, walkInlines); |
| 117 | + } |
| 118 | + } |
| 119 | + return []; |
| 120 | +} |
0 commit comments