Insert as Markdown ?

General TRichView support forum. Please post your questions here
Post Reply
Fab85
Posts: 41
Joined: Tue Apr 09, 2024 6:02 am

Insert as Markdown ?

Post by Fab85 »

Hello,

A InsertText method exists.
There is also a InsertMarkdownFromStreamEd and a PasteMarkdown.

But what I need is to insert a text containing markdown (without using/overriding clipboard with PasteMarkdown) :
A function like InsertMarkdownText ?

I need this to insert a AI response markdown text by code...
Sergey Tkachenko
Site Admin
Posts: 18240
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert as Markdown ?

Post by Sergey Tkachenko »

You can save a string to stream and call InsertMarkdownFromStreamEd.
For simplicity, you can use the RVU_UnicodeStringToUTF8Stream function from RVUni.pas (fmxRVUni.pas in FireMonkey vesion):

Code: Select all

uses
  RVUni;

procedure InsertMarkdown(const S: UnicodeString; rve: TCustomRichViewEdit);
var
  Stream: TStream;
begin
  Stream := RVU_UnicodeStringToUTF8Stream(S);
  Stream.Position := 0;
  rv.InsertMarkdownFromStreamEd(Stream);
  Stream.Free;
end;
Fab85
Posts: 41
Joined: Tue Apr 09, 2024 6:02 am

Re: Insert as Markdown ?

Post by Fab85 »

Hello,

Thank you for your answer.

However, I have a small issue when inserting Markdown content into an existing RTF document, especially when the Markdown contains formatting such as bold text, headings, or numbered lists :
The font size does not seem to be consistent: some parts of the inserted text appear larger or smaller than others.

Currently, to fix this, I have to select the inserted Markdown text manually and force the font size, for example to 14 pt.

Do you know if there is a way to ensure that the inserted Markdown uses a consistent font size automatically?

Thank you for your help.
Sergey Tkachenko
Site Admin
Posts: 18240
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert as Markdown ?

Post by Sergey Tkachenko »

Does your editor use style templates? (UseStyleTemplates property = True).
If yes, properties of certain style templates are applied on loading, such as 'Strong', 'Emphasis', 'HTML Code'. If these style templates have defined font size, it is applied to loaded text.
Fab85
Posts: 41
Joined: Tue Apr 09, 2024 6:02 am

Re: Insert as Markdown ?

Post by Fab85 »

Does your editor use style templates? (UseStyleTemplates property = True).
Thank you for answer.

Does do this (disable UsestyleTemplate while inserting) is a good idea :

Code: Select all

Myrve.UseStyleTemplates:=false;
InsertMarkdown(MDClipboardText,Myrve);
Myrve.UseStyleTemplates:=true;
Sergey Tkachenko
Site Admin
Posts: 18240
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert as Markdown ?

Post by Sergey Tkachenko »

Without StyleTemplates, Markdown will be loaded using default character attributes.
For example, *text* will be loaded as an italic text, instead of using properties of 'Emphasis' StyleTemplate.

But I think, if the style like 'Emphasis' has a font size specified, this means that the user wants to emphasize text by changing its size.
Post Reply