summaryrefslogtreecommitdiff
path: root/src/components/rich-text
diff options
context:
space:
mode:
authorBertrand Yuan <bert.yuan@outlook.com>2025-12-16 00:25:04 +0800
committerGitHub <noreply@github.com>2025-12-16 00:25:04 +0800
commit39c83fbb69ef06d2d56790d75abc254ba7e34394 (patch)
treedd006593448c3500bdcb414af3b4656f7a7683d4 /src/components/rich-text
parent48b07bc308a35734a6a7a305c8fdccbfa47de7d8 (diff)
parent785371bb3eccca455e5ce5fccbe9b6e3752a03f6 (diff)
Merge pull request #1 from bertyuan/feat-introduce-payloadv1.0
Feat: introduce payload
Diffstat (limited to 'src/components/rich-text')
-rw-r--r--src/components/rich-text/index.tsx42
-rw-r--r--src/components/rich-text/node-format.ts11
-rw-r--r--src/components/rich-text/serialize.tsx201
3 files changed, 254 insertions, 0 deletions
diff --git a/src/components/rich-text/index.tsx b/src/components/rich-text/index.tsx
new file mode 100644
index 0000000..556942c
--- /dev/null
+++ b/src/components/rich-text/index.tsx
@@ -0,0 +1,42 @@
+import { cn } from '@/lib/utils'
+import React from 'react'
+import { serializeLexical } from './serialize'
+
+type Props = {
+ className?: string
+ content: Record<string, unknown>
+ enableProse?: boolean
+}
+
+export function RichText({
+ className,
+ content,
+ enableProse = true,
+}: Props) {
+ if (!content) {
+ return null
+ }
+
+ return (
+ <div
+ className={cn(
+ {
+ 'prose min-w-0 dark:prose-invert': enableProse,
+ },
+ className
+ )}
+ >
+ {content &&
+ !Array.isArray(content) &&
+ typeof content === 'object' &&
+ 'root' in content &&
+ serializeLexical({
+ nodes: (content.root as { children: unknown[] })?.children as Parameters<
+ typeof serializeLexical
+ >[0]['nodes'],
+ })}
+ </div>
+ )
+}
+
+export default RichText
diff --git a/src/components/rich-text/node-format.ts b/src/components/rich-text/node-format.ts
new file mode 100644
index 0000000..84e7d7e
--- /dev/null
+++ b/src/components/rich-text/node-format.ts
@@ -0,0 +1,11 @@
+// From Lexical: https://github.com/facebook/lexical/blob/c2ceee223f46543d12c574e62155e619f9a18a5d/packages/lexical/src/LexicalConstants.ts
+
+// Text node formatting
+export const IS_BOLD = 1
+export const IS_ITALIC = 1 << 1
+export const IS_STRIKETHROUGH = 1 << 2
+export const IS_UNDERLINE = 1 << 3
+export const IS_CODE = 1 << 4
+export const IS_SUBSCRIPT = 1 << 5
+export const IS_SUPERSCRIPT = 1 << 6
+export const IS_HIGHLIGHT = 1 << 7
diff --git a/src/components/rich-text/serialize.tsx b/src/components/rich-text/serialize.tsx
new file mode 100644
index 0000000..d75c40f
--- /dev/null
+++ b/src/components/rich-text/serialize.tsx
@@ -0,0 +1,201 @@
+import React, { Fragment, type JSX } from 'react'
+import Link from 'next/link'
+import {
+ IS_BOLD,
+ IS_CODE,
+ IS_ITALIC,
+ IS_STRIKETHROUGH,
+ IS_SUBSCRIPT,
+ IS_SUPERSCRIPT,
+ IS_UNDERLINE,
+} from './node-format'
+
+// Lexical 节点类型
+interface LexicalNode {
+ type: string
+ format?: number
+ text?: string
+ tag?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'ul' | 'ol'
+ listType?: 'bullet' | 'number' | 'check'
+ checked?: boolean
+ value?: number
+ children?: LexicalNode[]
+ fields?: {
+ linkType?: 'internal' | 'custom'
+ url?: string
+ newTab?: boolean
+ doc?: {
+ value?: {
+ slug?: string
+ }
+ relationTo?: string
+ }
+ }
+ language?: string
+ version?: number
+}
+
+type Props = {
+ nodes: LexicalNode[]
+}
+
+export function serializeLexical({ nodes }: Props): JSX.Element {
+ return (
+ <Fragment>
+ {nodes?.map((node, index): JSX.Element | null => {
+ if (node == null) {
+ return null
+ }
+
+ if (node.type === 'text') {
+ let text = <React.Fragment key={index}>{node.text}</React.Fragment>
+ const format = node.format || 0
+
+ if (format & IS_BOLD) {
+ text = <strong key={index}>{text}</strong>
+ }
+ if (format & IS_ITALIC) {
+ text = <em key={index}>{text}</em>
+ }
+ if (format & IS_STRIKETHROUGH) {
+ text = (
+ <span key={index} style={{ textDecoration: 'line-through' }}>
+ {text}
+ </span>
+ )
+ }
+ if (format & IS_UNDERLINE) {
+ text = (
+ <span key={index} style={{ textDecoration: 'underline' }}>
+ {text}
+ </span>
+ )
+ }
+ if (format & IS_CODE) {
+ text = <code key={index}>{node.text}</code>
+ }
+ if (format & IS_SUBSCRIPT) {
+ text = <sub key={index}>{text}</sub>
+ }
+ if (format & IS_SUPERSCRIPT) {
+ text = <sup key={index}>{text}</sup>
+ }
+
+ return text
+ }
+
+ // 处理子节点
+ const serializedChildrenFn = (node: LexicalNode): JSX.Element | null => {
+ if (node.children == null) {
+ return null
+ } else {
+ // 处理 checkbox list
+ if (node?.type === 'list' && node?.listType === 'check') {
+ for (const item of node.children) {
+ if ('checked' in item) {
+ if (!item?.checked) {
+ item.checked = false
+ }
+ }
+ }
+ }
+ return serializeLexical({ nodes: node.children })
+ }
+ }
+
+ const serializedChildren = 'children' in node ? serializedChildrenFn(node) : ''
+
+ switch (node.type) {
+ case 'linebreak': {
+ return <br key={index} />
+ }
+ case 'paragraph': {
+ return <p key={index}>{serializedChildren}</p>
+ }
+ case 'heading': {
+ const Tag = node?.tag || 'h2'
+ return <Tag key={index}>{serializedChildren}</Tag>
+ }
+ case 'list': {
+ const Tag = node?.tag || 'ul'
+ return <Tag key={index}>{serializedChildren}</Tag>
+ }
+ case 'listitem': {
+ if (node?.checked != null) {
+ return (
+ <li
+ aria-checked={node.checked ? 'true' : 'false'}
+ key={index}
+ role="checkbox"
+ tabIndex={-1}
+ value={node?.value}
+ >
+ <input
+ type="checkbox"
+ checked={node.checked}
+ readOnly
+ className="mr-2"
+ />
+ {serializedChildren}
+ </li>
+ )
+ } else {
+ return (
+ <li key={index} value={node?.value}>
+ {serializedChildren}
+ </li>
+ )
+ }
+ }
+ case 'quote': {
+ return <blockquote key={index}>{serializedChildren}</blockquote>
+ }
+ case 'link': {
+ const fields = node.fields
+
+ if (fields?.linkType === 'internal' && fields?.doc?.value?.slug) {
+ const href =
+ fields.doc.relationTo === 'posts'
+ ? `/posts/${fields.doc.value.slug}`
+ : `/${fields.doc.value.slug}`
+
+ return (
+ <Link key={index} href={href}>
+ {serializedChildren}
+ </Link>
+ )
+ }
+
+ return (
+ <a
+ key={index}
+ href={fields?.url || '#'}
+ target={fields?.newTab ? '_blank' : undefined}
+ rel={fields?.newTab ? 'noopener noreferrer' : undefined}
+ >
+ {serializedChildren}
+ </a>
+ )
+ }
+ case 'code': {
+ // 代码块
+ return (
+ <pre key={index} className="overflow-x-auto">
+ <code>{serializedChildren}</code>
+ </pre>
+ )
+ }
+ case 'horizontalrule': {
+ return <hr key={index} />
+ }
+ default:
+ // 如果有子节点,递归渲染
+ if (node.children) {
+ return <Fragment key={index}>{serializedChildren}</Fragment>
+ }
+ return null
+ }
+ })}
+ </Fragment>
+ )
+}