Getting Started
Install
dotnet add package ReverseMarkdownInstall-Package ReverseMarkdownBasic usage
The examples below assume using ReverseMarkdown;.
var converter = new Converter();
string html = "This a sample <strong>paragraph</strong> from " +
"<a href=\"http://test.com\">my site</a>";
string result = converter.Convert(html);
// This a sample **paragraph** from [my site](http://test.com)Result:
This a sample **paragraph** from [my site](http://test.com)With configuration
Options are organized into groups on Config. Select an output flavor with Flavor, and set grouped options such as Links, Formatting, and Tags:
var config = new Config
{
// generate GitHub flavoured markdown (br, pre -> fenced code, task lists)
GithubFlavored = true,
// include unknown tags completely in the result (the default)
Tags = { Unknown = Config.UnknownTagsOption.PassThrough },
// ignore all comments
Formatting = { RemoveComments = true },
// collapse a link to plain text when text and href match
Links = { SmartHref = true },
};
var converter = new Converter(config);Writing to a TextWriter
Convert returns the Markdown as a string. When the output is headed for a file or any other TextWriter, pass the writer instead and the Markdown is written to it directly:
var converter = new Converter();
// Renders straight into the writer: the Markdown is never held as one string.
using var file = new StreamWriter("page.md");
converter.Convert(html, file);The output is identical to the string overload, including trimming and the configured Formatting.OutputLineEnding. The gain is peak memory, not speed: the converter still renders into an internal buffer, but it skips the full-size copies the string overload makes to produce the final string, which matters on large documents.
Only the output side streams. The HTML input is still a string, and it is parsed into an in-memory document before anything is written.
The converter writes to the writer but does not flush, close or dispose it; that stays with the caller (the using above does it for a file).
Render has the same overload for a document you have already parsed (see Transform the Markdown DOM):
var converter = new Converter();
var document = converter.Parse(html);
converter.Render(document, output);One exception: with the Slack, CommonMark and GitHub flavors, Convert(html, writer) and ConvertAsync still build the string internally before writing it out. The output is correct, but there is no memory saving.
Writing asynchronously
The overloads above write synchronously. ASP.NET Core disallows synchronous writes to the response body by default, so a writer over Response.Body throws an InvalidOperationException once its buffer fills. Use ConvertAsync there, which awaits every write and takes a CancellationToken:
var converter = new Converter();
// For a stream that disallows synchronous writes, such as an ASP.NET Core response body.
// leaveOpen: the stream belongs to the caller; disposing the writer flushes it asynchronously.
await using var writer = new StreamWriter(responseBody, leaveOpen: true);
await converter.ConvertAsync(html, writer, cancellationToken);Parsing and rendering still run synchronously before the first write; only the output is asynchronous. The output is identical to the other overloads, and RenderAsync covers a document you have already parsed.
See Configuration for the full option reference and Flavors for the available output flavors.
