UNPKG

@mkljczk/lexical-remark

Version:

This package contains Markdown helpers and functionality for Lexical using remark-parse.

33 lines (32 loc) 1.17 kB
import { $getRoot } from 'lexical'; import remarkStringify from 'remark-stringify'; import { unified } from 'unified'; import { exportToRemarkTree } from './handlers/index.js'; function lexicalToRemark(rootNode, options = {}) { return exportToRemarkTree(rootNode, options); } export function serializeFromRemark(tree) { const file = unified() .use(remarkStringify, { bullet: '-', fence: '`', fences: true, listItemIndent: 'one', }) .stringify(tree); return String(file).trimEnd(); } export function $convertToMarkdownViaRemark(options) { const root = $getRoot(); const remarkTree = lexicalToRemark(root, { handlers: options?.handlers }); return serializeFromRemark(remarkTree); } /** * Creates a parsing function which converts a Lexical state to a markdown string via remark * * @param handlers A set of additional handlers designed to parse Lexical nodes into remark mdast nodes * @returns A function which returns the state of the active Lexical editor as a markdown string */ export function $createRemarkExport(options) { return () => $convertToMarkdownViaRemark(options); }