Skip to content
Snippets Groups Projects

Pass a Markdown serializer to the Content Editor

Merged Enrique Alcántara requested to merge content-editor-markdown-serializer into master
5 files
+ 98
22
Compare changes
  • Side-by-side
  • Inline
Files
5
import { isFunction } from 'lodash';
import { Editor } from 'tiptap';
import { Bold, Code } from 'tiptap-extensions';
import createMarkdownSerializer from './markdown_serializer';
const createEditor = ({ content } = {}) => {
return new Editor({
const createEditor = async ({ content, renderMarkdown, serializer: customSerializer } = {}) => {
if (!customSerializer && !isFunction(renderMarkdown)) {
throw new Error('You have to provide a renderMarkdown function or a custom serializer');
}
const editor = new Editor({
extensions: [new Bold(), new Code()],
content,
});
const serializer = customSerializer || createMarkdownSerializer({ render: renderMarkdown });
editor.setSerializedContent = async (serializedContent) => {
editor.setContent(
await serializer.deserialize({ schema: editor.schema, content: serializedContent }),
);
};
editor.getSerializedContent = () => {
return serializer.serialize({ schema: editor.schema, content: editor.getJSON() });
};
await editor.setSerializedContent(content);
return editor;
};
export default createEditor;
Loading