This online demo demonstrates the import, paste and export Markdown features of RadEditor.
Import Markdown content to the editor
The makeHtml() client-side method of the Telerik.Web.UI.Editor.Markdown.Converter() object handles the Markdown import feature. Once converted the markdown content can be set in RadEditor via the set_html() method of the control, e.g.
//get a reference to RadEditor
var
editor
=
$find(
"<%=RadEditor1.ClientID%>"
)
;
//get a reference to the textbox with the Markdown content
var
textbox
=
$
get
(
"<%=MarkdownTextBox1.ClientID%>"
)
;
//create an instance of the Markdown converter
var
converter
= new
Telerik.Web.UI.Editor.Markdown.Converter()
;
//set the converted Markdown content in the editor
editor.set_html(converter.makeHtml(textbox.
value
))
;
Paste Markdown content via the Paste Markdown dialog
The inserted markdown content via the Paste Markdown dialog is converted to valid HTML content. To enable the popup button on the toolbar add the
<tool name="PasteMarkdown"/> tag in the ToolsFile.xml file or register the button inline or via the codebehind as shown in the following help article: Adding Standard Buttons.
Export RadEdtor's content to a TXT file with markdown content
The approach is very straight-forward - to export the editor content to a text file with markdown format fire the ExportToMarkdown() server-side method.
You may also need to configure the exporting settings for the editor through the RadEditor.ExportSettings section. The available properties are:
- FileName - a string specifying the name (without the extension) of the file that will be created. The file extension is automatically added based on the method that is used.
- OpenInNewWindow - open the exported Markdown in a new instead of the same page
<telerik:RadEditor ID="RadEditor1" runat="server" >
<ExportSettings FileName="Markdown" OpenInNewWindow="true"></ExportSettings>
</telerik:RadEditor>
There is also an OnExportContent server event, which can be used to export silently the markdown content to the server. You can obtain the exported content via the e.ExportOutput property:
<telerik:RadEditor ID="RadEditor1" runat="server" OnExportContent="RadEditor1_ExportContent">
<ExportSettings FileName="Markdown" OpenInNewWindow="false"></ExportSettings>
</telerik:RadEditor>
C#
string url, path = "";
protected void Page_Load(object sender, EventArgs e)
{
url = String.Format("{0}.txt", RadEditor1.ExportSettings.FileName);
path = Server.MapPath(url);
}
protected void ExportToMarkdownButton_Click(object sender, EventArgs e)
{
RadEditor1.ExportToMarkdown();
}
protected void RadEditor1_ExportContent(object sender, EditorExportingArgs e)
{
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path))
{
Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
fs.Write(info, 0, info.Length);
}
Response.Redirect("DefaultCS.aspx");
}
VB.NETPrivate url As String, path As String = ""
Protected Sub Page_Load(sender As Object, e As EventArgs)
url = [String].Format("{0}.txt", RadEditor1.ExportSettings.FileName)
path = Server.MapPath(url)
End Sub
Protected Sub ExportToMarkdownButton_Click(sender As Object, e As EventArgs)
RadEditor1.ExportToMarkdown()
End Sub
Protected Sub RadEditor1_ExportContent(sender As Object, e As EditorExportingArgs)
If File.Exists(path) Then
File.Delete(path)
End If
Using fs As FileStream = File.Create(path)
Dim info As [Byte]() = System.Text.Encoding.[Default].GetBytes(e.ExportOutput)
fs.Write(info, 0, info.Length)
End Using
Response.Redirect("DefaultCS.aspx")
End Sub
Related Resources