How can I load Plain Text (HTML or Markdown) from a RichView
How can I load Plain Text (HTML or Markdown) from a RichView
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).
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).
-
- 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
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).
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
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
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 (38.51 KiB) Viewed 900 times
-
- 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
I cannot reproduce the problem with LoadFromStream not loading plain text.
I used this code:
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.
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;
Code: Select all
## Header
Text
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
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:
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
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);
Code: Select all
## Header
---
Code: Select all
Header
Code: Select all
## Header
---
-
- 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
Do you need to get content of TRichView not as a plain text, but as Markdown?
Use SaveMarkdownToStream.
Use SaveMarkdownToStream.
Re: How can I load Plain Text (HTML or Markdown) from a RichView
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
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:
and It gets rendered in a RichView.
And when I click a Button I need that HTML as a string like:
for example
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;
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 when I click a Button I need that HTML as a string like:
Code: Select all
var text: string := GetRVHTMLOrMarkdown(RichView1)
-
- 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
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.
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
Removing RichViewEdit1.Clear before RichViewEdit1.SaveMarkdownToStream didn't changed anything, I still get an empty document. Why is that?
-
- 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
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:
The default HTML saving encoding is UTF-8 as well, so
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;
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;