🚸 Rewrite the markdown deserializer to improve br… (#1198)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Updated markdown handling and serialization libraries for improved performance and accuracy in text formatting. - **New Features** - Enhanced rich text and markdown conversion capabilities, providing users with more reliable and seamless text formatting options. - **Documentation** - Added detailed documentation for markdown to rich text conversion and vice versa, ensuring easier understanding and implementation for developers. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
36
packages/lib/markdown/remark-slate/remarkTransformText.ts
Normal file
36
packages/lib/markdown/remark-slate/remarkTransformText.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { TText, Value } from '@udecode/plate-common'
|
||||
|
||||
import { remarkDefaultTextRules } from './remarkDefaultTextRules'
|
||||
import { MdastNode, RemarkPluginOptions } from './types'
|
||||
|
||||
export const remarkTransformText = <V extends Value>(
|
||||
node: MdastNode,
|
||||
options: RemarkPluginOptions<V>,
|
||||
inheritedMarkProps: { [key: string]: boolean } = {}
|
||||
): TText | TText[] => {
|
||||
const { editor, textRules } = options
|
||||
|
||||
const { type, value, children } = node
|
||||
const textRule = (textRules as any)[type!] || remarkDefaultTextRules.text
|
||||
|
||||
const { mark, transform = (text: string) => text } = textRule
|
||||
|
||||
const markProps = mark
|
||||
? {
|
||||
...inheritedMarkProps,
|
||||
[mark({ editor })]: true,
|
||||
}
|
||||
: inheritedMarkProps
|
||||
|
||||
const childTextNodes =
|
||||
children?.flatMap((child) =>
|
||||
remarkTransformText(child, options, markProps)
|
||||
) || []
|
||||
|
||||
const currentTextNodes =
|
||||
value || childTextNodes.length === 0
|
||||
? [{ text: transform(value || ''), ...markProps } as TText]
|
||||
: []
|
||||
|
||||
return [...currentTextNodes, ...childTextNodes]
|
||||
}
|
||||
Reference in New Issue
Block a user