Markdown Issue with Last Word Styling and Trailing Space

General TRichView support forum. Please post your questions here
Post Reply
a.mancini
Posts: 17
Joined: Tue Jan 16, 2024 3:35 pm

Markdown Issue with Last Word Styling and Trailing Space

Post by a.mancini »

Hello,

I've run into a snag while working with Markdown.
It seems there's an issue when the last word of my text is styled (e.g., bold) and ends with a space.

Any tips on resolving this or best practices to follow? Just want to make sure my text formats correctly according to Markdown rules.

Thanks for the help!

Best,
Alessandro Mancini
Sergey Tkachenko
Site Admin
Posts: 17565
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Markdown Issue with Last Word Styling and Trailing Space

Post by Sergey Tkachenko »

I confirm the problem. It is in saving.

According to Markdown specification (TRichView uses CommonMark version), bold or italic fragment cannot end on a space character.
So in the code

Code: Select all

aaa **bbb **
bbb is not bold and asterisks must be displayed (check)

TRichView must modify saving procedure to avoid this problem. It already does it in the middle of paragraphs.
For example, in the text "aaa bbb ccc" bold space after "bbb" is saved as not bold:

Code: Select all

aaa **bbb** ccc
instead of incorrect

Code: Select all

aaa **bbb **ccc
We need to do the same thing at the end of paragraph.

Quick fix. Open RVMarkdownSave.pas, and change class procedure TRVMarkdownSavingData.FinalizeFormatting to

Code: Select all

class procedure TRVMarkdownSavingData.FinalizeFormatting(var CurS: TRVUnicodeString;
  var CurStyle: TFontStyles);
var
  Formatting: TRVUnicodeString;
  CT: TRVMDCharType;
begin
  if CurStyle <> [] then
  begin
    Formatting := '';
    if rvfsBold in CurStyle then
      Formatting := Formatting + '**';
    if rvfsItalic in CurStyle then
      Formatting := Formatting + '*';
    if Formatting <> '' then
    begin
      CT := GetLastCharType(CurS);
      if CT <> rvmdctWhitespace then
        CurS := CurS + Formatting
      else
        InsertBeforeTrailingWhitespaces(CurS, Formatting);
    end;
    CurStyle := CurStyle - [rvfsBold, rvfsItalic];
  end;
end;
Of course, the resulting document will be not exactly the same as the original. But this is a necessary compromise.
a.mancini
Posts: 17
Joined: Tue Jan 16, 2024 3:35 pm

Re: Markdown Issue with Last Word Styling and Trailing Space

Post by a.mancini »

the fix works well, thank you for the support
Sergey Tkachenko
Site Admin
Posts: 17565
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Markdown Issue with Last Word Styling and Trailing Space

Post by Sergey Tkachenko »

Fixed in TRichView 22.2
Post Reply