Skip to content

Instantly share code, notes, and snippets.

@webel
Created December 3, 2021 12:37
Show Gist options
  • Select an option

  • Save webel/545f229fe79c2176dbaed9023de46e12 to your computer and use it in GitHub Desktop.

Select an option

Save webel/545f229fe79c2176dbaed9023de46e12 to your computer and use it in GitHub Desktop.
/**
* Slate's schema has changed vastly under 2 years. The text editor is still
* a better candidate than the other OSS editors out there, so we must live
* with the major changes.
*
* Migrate a schema from the old version 0.33 to current version 0.6x
* Inspiration taken wholly from
* https://github.com/react-page/react-page/blob/b6c83a8650cfe9089e0c3eaf471ab58a0f7db761/packages/plugins/content/slate/src/migrations/v004.ts
*/
const migrateTextNode = oldNode => {
const leaves = oldNode.leaves.map(leaf => ({
text: leaf.text,
...leaf.marks?.reduce(
(acc, mark) => ({
...acc,
[mark.type]: true,
}),
{}
),
}));
return leaves;
};
const migrateElementNode = node => {
return {
data: node.data ?? {},
type: node.type,
children: node.nodes?.map(migrateNode).flat() ?? [],
};
};
const migrateNode = oldNode => {
if (oldNode.object === 'text') {
return migrateTextNode(oldNode);
} else {
return migrateElementNode(oldNode);
}
};
export const migrateSchema = oldSchema => {
return oldSchema.document.nodes.map(migrateNode);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment