MarkdownWriter Class
- Namespace
- Kampute.DocToolkit.IO.Writers
- Assembly
- Kampute.DocToolkit.dll
Definition
Represents a text writer that escapes Markdown special characters and provides methods to write Markdown elements.
public class MarkdownWriter : MarkupWriter- Inheritance
Remarks
This class helps in writing Markdown content safely by escaping special characters where necessary. It extends the MarkupWriter class and provides additional methods tailored for Markdown formatting.
Examples
The following example demonstrates the key features of MarkdownWriter:
using var writer = new MarkdownDocumentWriter(Console.Out, leaveOpen: true);
// Write a heading
writer.WriteHeading(1, "Title");
// Write a paragraph
writer.WriteParagraph(w =>
{
w.Write("This is a paragraph with ");
w.WriteStrong("bold");
w.Write(" text.");
});
// Add a blockquote
writer.WriteBlockquote("This is a blockquote.");
// Add a code block
writer.WriteCodeBlock(@"var greeting = ""Hello, World!"";", "csharp");
// Add a link
writer.WriteLink(new Uri("https://example.com"), "Visit Example");This produces the following Markdown:# Title
This is a paragraph with **bold text**
> This is a blockquote.
```csharp
var greeting = "Hello, World!";
```
[Visit Example](https://example.com/)Thread Safety
Public static members of the type are guaranteed to be thread-safe. However, public instance members are not thread-safe.
Constructors
| MarkdownWriter(TextWriter, bool) | Initializes a new instance of the MarkdownWriter class. |
Properties
| IndentationLevel | Gets the current indentation level. |
Methods
| EnsureEmptyLine() | Writes an empty line if the write does not already end with one. |
| EnsureNewLine() | Writes a newline if the writer does not already end with one. |
| IsAtStartOfLine(bool) | Determines whether the writer is at the start of a line. |
| Write(char) | Writes a character after escaping Markdown special characters. |
| WriteBlockquote(Action<MarkupWriter>) | Writes a blockquote using the provided delegate to write the content. |
| WriteCodeBlock(string, string) | Writes a block of code with optional language specifier. |
| WriteEmphasis(Action<MarkupWriter>) | Writes an emphasis using the provided delegate to write the content. |
| WriteHeading(int, Action<MarkupWriter>) | Writes a heading using the provided delegate to write the content. |
| WriteHorizontalRule() | Writes a horizontal rule. |
| WriteImage(Uri, string) | Writes an image using the specified URL and an optional title. |
| WriteInlineCode(string) | Writes a code span using the specified text. |
| WriteLink(Uri, Action<MarkupWriter>) | Writes a hyperlink. |
| WriteList(int, Action<MarkupWriter, int>, bool) | Writes a list using a delegate to write the list items. |
| WriteParagraph(Action<MarkupWriter>) | Writes a paragraph using the provided delegate to write the content. |
| WriteSafe(char) | Writes a character without escaping Markdown special characters. |
| WriteStrong(Action<MarkupWriter>) | Writes a strong emphasis using the provided delegate to write the content. |
| WriteTable(IReadOnlyList<string>, int, Action<MarkupWriter, int, int>) | Writes a table using a delegate to write the content of each cell. |
