kadikraman / draftjs-md-converter

Converts content from Draft.js blocks to Markdown and vice versa.
https://kadikraman.github.io/draftjs-md-converter/
MIT License
145 stars 37 forks source link

Custom dictionary value are not converted (MD to DraftJS) #65

Open caribouflex opened 3 years ago

caribouflex commented 3 years ago

Hi, I'm using the custom dictionary to handle the strikethroughoption. The conversion from DraftJS to markdown works fine. However converting from markdown to DraftJS does not work, and the inlineStyleRanges raw value doesn't contains the strikethrough style.

Here is the dictionary:

export const customMarkdownDictionary = {
  STRIKETHROUGH: '~~',
  CODE: '`',
};

Here is how I convert :

// To raw data
const rawData = mdToDraftjs(value as string);

// To markdown
const markdownContent = draftjsToMd(
    rawDraftContent,
    customMarkdownDictionary,
);

Thanks for the help ✌️

michaelg9 commented 3 years ago

if you check the code, the second parameter to mdToDraftjs is a nested object and not an object from string to string, as the declaration type incorrectly states. You need to pass something like this:

{
  inlineStyles: {
    Strong: {
      type: 'BOLD',
      symbol: '**',
    },
    Emphasis: {
      type: 'ITALIC',
      symbol: '*',
    },
    Delete: {
      type: 'UNDERLINE',
      symbol: '__',
    },
  },
};
jpmsegurado commented 3 years ago

@michaelg9 how do you know these keys like Strong, Emphasis and Delete. I'm trying to implement underline and strikethrough.

I was able to work it out the strikethrough with Delete but what would be the one for underline?

michaelg9 commented 3 years ago

I just looked at the implementation code @jpmsegurado, this is not documented. In my case I replaced Delete with underline since I didn't need strikethrough. Tbh I didn't find any library that would be quite flexible with adding / replacing these styles to your own liking. If you do I'd be interested to know!

Sholanke commented 2 years ago

@jpmsegurado here's a list of the accepted ASTNodeTypes

Document = "Document", DocumentExit = "Document:exit", Paragraph = "Paragraph", ParagraphExit = "Paragraph:exit", BlockQuote = "BlockQuote", BlockQuoteExit = "BlockQuote:exit", ListItem = "ListItem", ListItemExit = "ListItem:exit", List = "List", ListExit = "List:exit", Header = "Header", HeaderExit = "Header:exit", CodeBlock = "CodeBlock", CodeBlockExit = "CodeBlock:exit", HtmlBlock = "HtmlBlock", HtmlBlockExit = "HtmlBlock:exit", HorizontalRule = "HorizontalRule", HorizontalRuleExit = "HorizontalRule:exit", Comment = "Comment", CommentExit = "Comment:exit", ReferenceDef = "ReferenceDef", ReferenceDefExit = "ReferenceDef:exit", Str = "Str", StrExit = "Str:exit", Break = "Break", BreakExit = "Break:exit", Emphasis = "Emphasis", EmphasisExit = "Emphasis:exit", Strong = "Strong", StrongExit = "Strong:exit", Html = "Html", HtmlExit = "Html:exit", Link = "Link", LinkExit = "Link:exit", Image = "Image", ImageExit = "Image:exit", Code = "Code", CodeExit = "Code:exit", Delete = "Delete", DeleteExit = "Delete:exit"