🚸 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:
30
packages/lib/markdown/deserializer/deserialize.ts
Normal file
30
packages/lib/markdown/deserializer/deserialize.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { getPluginOptions, PlateEditor, Value } from '@udecode/plate-common'
|
||||
import markdown from 'remark-parse'
|
||||
import { unified } from 'unified'
|
||||
|
||||
import { DeserializeMdPlugin } from './types'
|
||||
import { remarkPlugin } from '../remark-slate/remarkPlugin'
|
||||
import { RemarkPluginOptions } from '../remark-slate/types'
|
||||
import { KEY_DESERIALIZE_MD } from '../convertMarkdownToRichText'
|
||||
|
||||
export const deserialize = <V extends Value>(
|
||||
editor: PlateEditor<V>,
|
||||
data: string
|
||||
) => {
|
||||
const { elementRules, textRules, indentList } = getPluginOptions<
|
||||
DeserializeMdPlugin,
|
||||
V
|
||||
>(editor, KEY_DESERIALIZE_MD)
|
||||
|
||||
const tree: any = unified()
|
||||
.use(markdown)
|
||||
.use(remarkPlugin, {
|
||||
editor,
|
||||
elementRules,
|
||||
textRules,
|
||||
indentList,
|
||||
} as unknown as RemarkPluginOptions<V>)
|
||||
.processSync(data)
|
||||
|
||||
return tree.result
|
||||
}
|
||||
8
packages/lib/markdown/deserializer/types.ts
Normal file
8
packages/lib/markdown/deserializer/types.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Value } from '@udecode/plate-common'
|
||||
import { RemarkElementRules, RemarkTextRules } from '../remark-slate/types'
|
||||
|
||||
export interface DeserializeMdPlugin<V extends Value = Value> {
|
||||
elementRules?: RemarkElementRules<V>
|
||||
textRules?: RemarkTextRules<V>
|
||||
indentList?: boolean
|
||||
}
|
||||
Reference in New Issue
Block a user