Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes nodes option in serialize function #4156

Merged
merged 10 commits into from
Mar 12, 2025
5 changes: 5 additions & 0 deletions .changeset/loud-days-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-markdown": patch
---

Fixes nodes option in serialize function
2 changes: 1 addition & 1 deletion apps/www/content/docs/en/markdown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Converts the current Slate value to a Markdown string.

<API name="serialize">
<APIOptions type="SerializeMarkdownOptions">
<APIItem name="nodes" type="Descendant[]" optional>
<APIItem name="value" type="Descendant[]" optional>
The Slate nodes to serialize. If not provided, the entire editor value will be used.
</APIItem>
</APIOptions>
Expand Down
12 changes: 8 additions & 4 deletions packages/markdown/src/lib/serializer/serializeMd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { serializeMdNodes } from './serializeMdNodes';
/** Serialize the editor value to Markdown. */
export const serializeMd = (
editor: SlateEditor,
options?: Parameters<typeof serializeMdNodes>['1']
options?: { value?: Parameters<typeof serializeMdNodes>['0'] } & Parameters<
typeof serializeMdNodes
>['1']
) => {
const plugins = editor.pluginList.filter(
(p) => p.node.isElement || p.node.isLeaf
);

const nodes = plugins.reduce(
const pluginNodes = plugins.reduce(
(acc, plugin) => {
(acc as any)[plugin.key] = {
isLeaf: plugin.node.isLeaf,
Expand All @@ -31,8 +33,10 @@ export const serializeMd = (
{} as SerializeMdOptions['nodes']
);

return serializeMdNodes(editor.children, {
const nodesToSerialize = options?.value ?? editor.children;

return serializeMdNodes(nodesToSerialize, {
...options,
nodes: merge(nodes, options?.nodes),
nodes: merge(pluginNodes, options?.nodes),
});
};