How can I load Plain Text (HTML or Markdown) from a RichView

General TRichView support forum. Please post your questions here
Post Reply
at0306
Posts: 5
Joined: Mon Nov 18, 2024 2:28 pm

How can I load Plain Text (HTML or Markdown) from a RichView

Post by at0306 »

Hello,

I'm working on a project where I need to build a simple HTML and Markdown Editor with TRichView and
TRichViewEdit.

I insert the plain HTML/Markdown Text into the RichViewEdit and render it with rv.LoadFromStream into the RichView.

But I can't find a way on how to load the rendered Text into the RichViewEdit again as plain text (e.g. ## Header).
Sergey Tkachenko
Site Admin
Posts: 17553
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by Sergey Tkachenko »

If you use the same method (AllowMarkdown) with parameter AllowMarkdown = False.
Or LoadTextFromStreamW (if the stream contain text in Unicode (UTF-16) encoding).
Or LoadTextFromStream (if the stream contain text in another encoding).
at0306
Posts: 5
Joined: Mon Nov 18, 2024 2:28 pm

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by at0306 »

When I use LoadFromStream with AllowMarkdown = False I only get the text content but not the plain code.

When I have this markdown:

## Header 

I gets rendered fine, but when I use LoadFromStream with AllowMarkdown = False, the result is "Header" instead of "## Header".

It would be nice if there was a method like RichView1.GetTextPlain
Attachments
Screenshot 2024-11-19 143128.png
Screenshot 2024-11-19 143128.png (38.51 KiB) Viewed 1039 times
Sergey Tkachenko
Site Admin
Posts: 17553
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by Sergey Tkachenko »

I cannot reproduce the problem with LoadFromStream not loading plain text.

I used this code:

Code: Select all

var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create('d:\test\md.md', fmOpenRead);
  rve.Clear;
  rve.LoadFromStream(Stream, rvynaYes, False);
  rve.Format;
  Stream.Free;
end;
md.md has Unicode (UTF-16) encoding and content:

Code: Select all

## Header

Text
It is loaded as a plain text. If I change the parameter of LoadFromStream to True, the header is formatted, as expected.

As for returning text.
1. SaveTextToStream and SaveTextToStreamW methods
2. (most convenient) GetAllText function from RVGetTextW unit
3. there is also GetTextRange from RVLinear unit, but it is special: it does not save non-text items (except for tabs and tables), so the resulting string has 1:1 correspondence to the original document.
at0306
Posts: 5
Joined: Mon Nov 18, 2024 2:28 pm

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by at0306 »

Thanks for the clarification.

But I my case, I'm not loading the markdown text from a file.
My problem here is the GetAllText() Method.
When I pass my RichView into GetAllText() like this:

Code: Select all

var test: string := getalltext(richview1);
and the content of RichView1 is: (but rendered as Markdown)

Code: Select all

## Header
---
the result I get from GetAllText() is

Code: Select all

 Header
 
instead of

Code: Select all

## Header
---
And I need to get the plain text, so I can pass it down to my RichViewEdit, which I open in a Modal Window
Sergey Tkachenko
Site Admin
Posts: 17553
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by Sergey Tkachenko »

Do you need to get content of TRichView not as a plain text, but as Markdown?
Use SaveMarkdownToStream.
at0306
Posts: 5
Joined: Mon Nov 18, 2024 2:28 pm

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by at0306 »

I need to get the content of TRichView either as Markdown or as HTML

But SaveMarkdownToStream needs a path and I don't.

I do this

Code: Select all

    richview1.SelectAll;
    var test: string := richview1.GetSelTextW;

    var stream: tstringstream := tstringstream.Create(test, tencoding.Unicode);

    RichViewEdit1.Clear;
    RichViewEdit1.SaveMarkdownToStream(stream, '', true);
    RichViewEdit1.Format;
    stream.Free;
Doesn't work, it's empty.

It can't be that complex to get HTML or Markdown as plain code from TRichView :(

Example:

I have this HTML:

Code: Select all

<html><body><h1>Hello From Delphi</h1></body></html>
and It gets rendered in a RichView.

And when I click a Button I need that HTML as a string like:

Code: Select all

var text: string := GetRVHTMLOrMarkdown(RichView1)
for example
Sergey Tkachenko
Site Admin
Posts: 17553
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by Sergey Tkachenko »

1. Path is needed to save external images.
If you do not have images, you can pass an empty string.
Also, you can save images directly inside Markdown/HTML. For Markdown, include in rvmdsoInlineImages in RichView.MarkdownProperties.SaveOptions. For HTML, include rvhtmlsioInlineImages in RichView1.HTMLSaveProperties.ImageOptions.
Path is not used for images saved inline, so you can pass an empty string as well.

2. Your code saves an empty document because your clear it before saving. Remove the call of RichViewEdit1.Clear before RichViewEdit1.SaveMarkdownToStream.
at0306
Posts: 5
Joined: Mon Nov 18, 2024 2:28 pm

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by at0306 »

Removing RichViewEdit1.Clear before RichViewEdit1.SaveMarkdownToStream didn't changed anything, I still get an empty document. Why is that?
Sergey Tkachenko
Site Admin
Posts: 17553
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How can I load Plain Text (HTML or Markdown) from a RichView

Post by Sergey Tkachenko »

1. If you need to save the whole document, pass False to SelectionOnly parameter of SaveMarkdownToStream.
2. If you save to TStringStream, encoding for saving and TStringStream encoding must be the same. By default, SaveMarkdownToStream saves as UTF-8.

Returning Markdown string:

Code: Select all

function GetMarkdownString(rve: TCustomRichView): UnicodeString;
var
  Stream: TStringStream;
begin
  Stream := TStringStream.Create('', TEncoding.UTF8);
  rve.SaveMarkdownToStream(Stream, '', False);
  Result := Stream.DataString;
  Stream.Free;
end;
The default HTML saving encoding is UTF-8 as well, so

Code: Select all

function GetHTMLString(rve: TCustomRichView): UnicodeString;
var
  Stream: TStringStream;
begin
  Stream := TStringStream.Create('', TEncoding.UTF8);
  rve.SaveHTMLToStream(Stream);
  Result := Stream.DataString;
  Stream.Free;
end;
Post Reply