Ankeeta Sahoo
Ankeeta Sahoo

Reputation: 11

convert lexical editor state to markdown

I am trying to download lexical editor contents in the form of a file. So i am trying to extract its equivalent markdown .

import { createHeadlessEditor } from '@lexical/headless'; 
import { $convertToMarkdownString, TRANSFORMERS } from '@lexical/markdown';  

// function to extract markdown 
export const getTextEditorMarkdown = async (stringifiedEditorState: string) => {
    try {
        const editor = createHeadlessEditor({
            nodes: [],
            onError: () => {}
        });
        editor.setEditorState(editor.parseEditorState(JSON.parse(stringifiedEditorState)));

        await editor.update(() => {
            const markdown = $convertToMarkdownString(TRANSFORMERS);
            console.info('markdown', markdown);
        });
    } catch (e: any) {
        // error handling
    }
};
const stringifiedEditorState = "{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"ju","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}"

// extracting markdown
getTextEditorMarkdown(stringifiedEditorState);

I am trying to extract markdown from lexical editor state stringified json, by creating a function.

I am getting this output enter image description here
Either markdown or its equivalent HTML, anything works for me.

Upvotes: 0

Views: 870

Answers (0)

Related Questions