lexical
lexical copied to clipboard
Feature: Get markdown content for selection
Description
I'm working with the markdown module of lexical and I realized there's no way to get the markdown string from only the selection of the text. I mean, If I use $convertToMarkdownString method, it only takes the full content of the editor and returns all the text in markdown, but in case of only being interested in the markdown string for the selection, it is not possible.
Would it be possible to change the signature/method or add a new one to allow that functionality? Or is there any possibility to do it currently with some workaround?
Thnaks!
I tried using $convertToMarkdownString and it worked fine. The second parameter of this method is the custom node that needs to be passed in.
The way that function works is a bit awkward because it expects the given node to be like the RootNode - it's really just a container for its children, and it won't be serialized itself. A workaround you could use is to copy the nodes in the selection ($generateJSONFromSelectedNodes then $generateNodesFromSerializedNodes), create an ElementNode, and append the copied nodes into that node which will be used as the argument to $convertToMarkdownString. It can only be done in an update since you're creating nodes, but the update will be cheap since you're not modifying any nodes that remain in the document.
The way that function works is a bit awkward because it expects the given node to be like the RootNode - it's really just a container for its children, and it won't be serialized itself. A workaround you could use is to copy the nodes in the selection (
$generateJSONFromSelectedNodesthen$generateNodesFromSerializedNodes), create an ElementNode, and append the copied nodes into that node which will be used as the argument to$convertToMarkdownString. It can only be done in an update since you're creating nodes, but the update will be cheap since you're not modifying any nodes that remain in the document.
Can you elaborate a bit more on it? I tried the same approach that you mention and I wasn't able to succeed on it without execution errors.
Are you using TypeScript to help guide your implementation? Not sure what problem you're having because you're not specific about the errors you're getting. Something like this (untested) will likely work:
let output: string;
editor.update(() => {
const el = $createParagraphNode();
el.splice(0, 0, $generateJSONFromSelectedNodes(editor, $getSelection()).nodes);
output = $convertToMarkdownString(transformers, el);
});
@etrepum I would like to work on this. please assign it to me
No need for assignment, if you're interested just submit a PR when you have a PR
genereateJSONFromSelectedNodes does not exist anymore, right?
https://lexical.dev/docs/api/modules/lexical_clipboard#generatejsonfromselectednodes
I am working on this issue. Would it be okay to add a state to manage whether the current editor is in full markdown mode or not?
A suggestion: Maybe add to the lexical custom context menu an option -> 'Copy as Markdown'. Irrespective if the editor is in markdown mode or not.
Hi, any updates on this issue?