TarekkMA / markdown_quill

Convert quill (Delta) fromat to and from markdown
MIT License
18 stars 41 forks source link

Line breaks are not being converted #20

Open yassinsameh opened 5 months ago

yassinsameh commented 5 months ago

With the following example json: "Example text/n/nTable of Contents", Expected:

Example text

Table of contents

Result:

Example text Table of contents

markdown_quill: ^3.1.0 flutter_quill: ^9.3.1

Code:

    final mdDocument = md.Document();
    final mdToDelta = MarkdownToDelta(
      markdownDocument: mdDocument,
    );
    final deltaDocument = mdToDelta.convert(json);
    return quill.Document.fromDelta(cleanedDelta);
TarekkMA commented 5 months ago

Hi @yassinsameh, thank you for opening an issue. This is to be expected when dealing with Markdown, you can also test it on https://dart-lang.github.io/markdown/ and confirm it.

if you want to add a hard line break that's not ignored you can add <br> which will be converted to a new line

Please let me know if you need any extra help regarding this issue.

yassinsameh commented 5 months ago

Hi @TarekkMA , sorry i don't get your point with the converter, it's converting to html while i'm converting to deltas. For other reference: Using the flutter_markdown package , with the same String, the output is formatted correctly with line breaks, could you please take a look.

I believe the solution is possibly in the EmptyBlockSyntax class parse method.

To give more context to the use case, i'm converting an llm output to deltas, so it wouldn't be very possible to add line breaks manually really, think it would be very hacky with weird edge cases.

TarekkMA commented 5 months ago

If the issue is line breaks are not converted, this is an expected behavior. 1 line break is ignored, but 2 or more line breaks converts only to one line break.

Vanilla Dart Markdown test (https://dart-lang.github.io/markdown/) Screenshot 2024-03-25 at 11 36 05 AM

flutter_markdown test (https://tarekkma.github.io/markdown_quill_playground/) Screenshot 2024-03-25 at 11 36 20 AM

Github markdown test (https://gist.github.com/) Screenshot 2024-03-25 at 11 36 48 AM Screenshot 2024-03-25 at 11 36 52 AM


If you want to maintain line breaks just replace \n with <br> it should do it.

yassinsameh commented 5 months ago

Thank you for taking the time to look at this. That behavior of >= 2 line breaks will generate only one empty line is fine. The problem i outlined above is during the conversion 2 line breaks generate 0 empty lines.

So case 1: "test/nbar". Correct & current output:

test bar

Case 2: "test/n/nbar". Incorrect Current output:

test bar

Correct expected output:

test

bar

Case 3 you mentioned above: "test/n/n/n/nbar": Correct expected output:

test

bar

TarekkMA commented 5 months ago

Thank you for the clear examples, will try to figure out what's the issue.

yassinsameh commented 5 months ago

Hi @TarekkMA, managed to take a look? I believe the solution is possibly in the EmptyBlockSyntax class parse method.

Gursewak-Uppal commented 4 months ago

I did it something like this

markdownToQuillDelta(String markdownText) {
  final mdDocument = md.Document();
  final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument);
  final delta = mdToDelta.convert(markdownText.replaceAll("\n\n\n\n", '''
\n
\u200B 
\n
'''));
  return delta;
}
sundar-karpura commented 3 months ago

@TarekkMA - did you get a chance to look into this. This change would be of great help.