Page 1 of 1
How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Mon Nov 18, 2024 2:34 pm
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).
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Tue Nov 19, 2024 12:34 pm
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).
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Tue Nov 19, 2024 1:38 pm
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
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 7:01 am
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:
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.
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 8:31 am
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)
the result I get from GetAllText() is
instead of
And I need to get the plain text, so I can pass it down to my RichViewEdit, which I open in a Modal Window
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 11:39 am
by Sergey Tkachenko
Do you need to get content of TRichView not as a plain text, but as Markdown?
Use
SaveMarkdownToStream.
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 12:29 pm
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
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 2:33 pm
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.
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 2:56 pm
by at0306
Removing RichViewEdit1.Clear before RichViewEdit1.SaveMarkdownToStream didn't changed anything, I still get an empty document. Why is that?
Re: How can I load Plain Text (HTML or Markdown) from a RichView
Posted: Wed Nov 20, 2024 5:33 pm
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;