Table of Contents

Class TwTextEditorExport

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

Converts the HTML a TwTextEditor produces into Markdown, Word, OpenDocument, HTML, plain text and Rich Text Format files. TwTextEditor uses it for its export menu, and it can be called on its own, for example to build a file on the server from stored HTML.

public static class TwTextEditorExport
Inheritance
TwTextEditorExport
Inherited Members

Remarks

Every converter reads the HTML with the same rules as the editor: only its tags are kept (paragraphs, three heading levels, bold, italic, underline, strikethrough, inline code, links, bulleted and numbered lists, quotes and code blocks), other tags are unwrapped, and scripts, styles and unsafe links are dropped. So HTML from anywhere can be converted safely. The converters use only the .NET libraries; no other package is needed.

Fields

DocxContentType

The media type of a Word document.

public const string DocxContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

Field Value

string

HtmlContentType

The media type of a web page.

public const string HtmlContentType = "text/html"

Field Value

string

MarkdownContentType

The media type of a Markdown file.

public const string MarkdownContentType = "text/markdown"

Field Value

string

OdtContentType

The media type of an OpenDocument text document.

public const string OdtContentType = "application/vnd.oasis.opendocument.text"

Field Value

string

PlainTextContentType

The media type of a plain text file.

public const string PlainTextContentType = "text/plain"

Field Value

string

RtfContentType

The media type of a Rich Text Format file.

public const string RtfContentType = "application/rtf"

Field Value

string

Methods

Export(string?, TextEditorExportFormat, string)

Converts HTML to a file in format, with the file name, extension and media type to save it with. Text formats are UTF-8, with a byte order mark for plain text so Windows Notepad shows any language.

public static ExportFile Export(string? html, TextEditorExportFormat format, string fileName = "document")

Parameters

html string

The editor's HTML.

format TextEditorExportFormat

The format.

fileName string

The file name without its extension, also used as the document's title. Characters a file name cannot hold are replaced. Defaults to document.

Returns

ExportFile

The file.

ExtensionOf(TextEditorExportFormat)

Gets the extension a format's files are saved with, such as .docx.

public static string ExtensionOf(TextEditorExportFormat format)

Parameters

format TextEditorExportFormat

The format.

Returns

string

The extension, with its dot.

ToDocx(string?, string?)

Converts HTML to a Word document. Headings use Word's Heading 1 to 3 styles, lists are real Word lists and links are real hyperlinks.

public static byte[] ToDocx(string? html, string? title = null)

Parameters

html string

The editor's HTML.

title string

The document's title, stored in its properties, or null for none.

Returns

byte[]

The contents of a .docx file.

ToHtmlDocument(string?, string, string)

Converts HTML to a web page that opens on its own in a browser, with simple readable styles that follow the reader's light or dark setting.

public static string ToHtmlDocument(string? html, string title = "Document", string language = "en")

Parameters

html string

The editor's HTML.

title string

The page's title.

language string

The language of the text, such as en-GB. Defaults to en.

Returns

string

The web page.

ToMarkdown(string?)

Converts HTML to Markdown, in the CommonMark syntax with GitHub's strikethrough. Underlined text is kept as <u> tags, since Markdown has no underline.

public static string ToMarkdown(string? html)

Parameters

html string

The editor's HTML.

Returns

string

The Markdown, or an empty string when there is no text.

ToOdt(string?, string?)

Converts HTML to an OpenDocument text document (ODF 1.3), the format of LibreOffice. Headings use the standard heading styles, and lists are real nested lists.

public static byte[] ToOdt(string? html, string? title = null)

Parameters

html string

The editor's HTML.

title string

The document's title, stored in its properties, or null for none.

Returns

byte[]

The contents of an .odt file.

ToPlainText(string?)

Converts HTML to plain text, with a blank line between paragraphs, a bullet or number before each list item, > before quoted lines, and each link's address after its text.

public static string ToPlainText(string? html)

Parameters

html string

The editor's HTML.

Returns

string

The text, or an empty string when there is no text.

ToRtf(string?, string?)

Converts HTML to Rich Text Format, which almost every word processor opens.

public static string ToRtf(string? html, string? title = null)

Parameters

html string

The editor's HTML.

title string

The document's title, stored in its properties, or null for none.

Returns

string

The RTF.