How to Edit Markdown in C#
Aspose.HTML for .NET
library is a stand-alone solution that allows you to edit Markdown files without using other software. You can modify a Markdown document by inserting new elements, removing, or editing the content of existing nodes.
The
Aspose.Html.Toolkit.Markdown.Syntax
namespace contains classes and methods to manipulate a Markdown syntax tree based on GitHub Flavored Markdown (GFM) Specification.
The
Aspose.Html.Toolkit.Markdown.Parser
namespace contains classes and methods to provide full Markdown parsing and rendering.
The
Aspose.Html.Toolkit.Markdown.Extensions
namespace contains classes and methods for manipulating the Markdown syntax tree based on specifications that are not part of the GFM specification.
Any document editing you want to perform involves loading an MD document, editing and saving it in the supported format. It can be different scenarios, but it can be made with a few required steps:
- Open or create Markdown.
- Parse Markdown to get a syntax tree. Edit the Markdown syntax tree using the classes and methods in the above namespaces.
- Save MD or convert MD to the required file format.
How to Add Element to MD File in C#
Using the C# library, you can programmatically edit MD files – change the document structure and content. The following C# code example shows how to add a new paragraph to an MD document:
C# code to edit MD files
// Specify the path to the source MD file
var input = @"C:\temp\document.md";
// Create a MarkdownParser object
var parser = new MarkdownParser();
// Parse the MD document and get a syntax tree
var syntaxTree = parser.ParseFile(input);
// Get a SyntaxFactory to create new elements
var syntaxFactory = syntaxTree.SyntaxFactory;
// Create a new paragraph
var paragraphSyntaxNode = syntaxFactory.Paragraph();
// Create and add newLineTrivia element for an empty line after paragraph
var newLineTrivia = syntaxFactory.NewLineTrivia();
paragraphSyntaxNode.GetTrailingTrivia().Add(newLineTrivia);
// Create text content for the paragraph
var textSyntaxNode = syntaxFactory.Text("New paragraph text.");
paragraphSyntaxNode.AppendChild(textSyntaxNode);
// Add the paragraph before the first element of the syntax tree
syntaxTree.InsertBefore(paragraphSyntaxNode, syntaxTree.FirstChild);
// Prepare a path for MD file saving
string savePath = Path.Combine(OutputDir, "output-add-paragraph.md");
// Save MD file
syntaxTree.Save(savePath);
Steps to Create and Add Element to MD Document in C#
Consider simple steps to edit existing MD document. In the document will add a new text paragraph:
- Specify the path to the source MD file and use the MarkdownParser() constructor to initialize a new instance of the MarkdownParser class.
- Call the ParseFile() method to parse Markdown and get a syntax tree.
- Use the SyntaxFactory property to get a syntax factory to create new elements.
- Create a new paragraph using the Paragraph() constructor. Create and add text content for a new paragraph.
- Use InsertBefore() method to add the paragraph before the first element of the syntax tree.
- Save the edited MD file with Save() method.
How to Edit Markdown Header in C#
Let’s look at how to edit the first element in the MD file ( document.md ). The first element in the file is a header, level 3. The edited file you find following the link – output-edit-header.md
C# code to edit MD files
// Specify the path to the source MD file
var input = @"C:\temp\document.md";
// Create a MarkdownParser object
var parser = new MarkdownParser();
// Parse the MD document and get a syntax tree
var syntaxTree = parser.ParseFile(input);
// The first element of this document is AtxHeading
var heading = (AtxHeadingSyntaxNode)syntaxTree.FirstChild;
// Accumulate the text content from its elements
var sb = new StringBuilder();
while (heading.FirstChild != null)
{
sb.Append(heading.FirstChild);
// Remove accumulated first element from the tree
heading.RemoveChild(heading.FirstChild);
}
// Get a SyntaxFactory to create new elements
var syntaxFactory = syntaxTree.SyntaxFactory;
// Сreate a text node consisting of new and old text and add it as a child element of AtxHeading
var textSyntaxNode = syntaxFactory.Text("Some new text! " + sb);
// Add the new element to the document tree
heading.AppendChild(textSyntaxNode);
// Prepare a path for MD file saving
string savePath = Path.Combine(OutputDir, "output-edit-header.md");
// Save MD file
syntaxTree.Save(savePath);
Documentation
To learn more about Aspose.HTML API, please visit our
documentation
guide. You can download the examples from the
GitHub
repository. They are open source and can be freely used in your own applications.
The
Markdown Syntax
documentation article provides information on the main Markdown elements, details and examples of the Markdown syntax.
Markdown is a simple markup language that allows you to format plain text. MD files use Markdown language that was proposed and developed by John Gruber. John Gruber designed Markdown’s formatting syntax with the goal of making it as readable as possible. Markdown is often used as a format for documentation and readme files since it allows writing in an easy-to-read and easy-to-write style. In addition, MD files can be converted to HTML, PDF, XPS or images to take advantage of other formats for specific tasks.
FAQ
The Aspose.HTML for .NET API is a standalone MD editing solution that does not depend on other software. Install our C# library, add the library reference to your C# project, and programmatically edit and manage MD documents.
Please visit our documentation to learn more about using the Aspose.HTML for .NET API to edit MD. If you have questions about functionality, found issues, or need a new feature, please start a discussion in our free support forum .
You can edit MD documents on any operating system, whether you’re using Windows, Mac OS, Linux, Android, or iOS.
We support a few file formats that you can edit in C#, including HTML, MHTML and Markdown.
Get Started with Aspose.HTML for .NET API
You can use several ways to install the Aspose.HTML for .NET library on your system:
- Install a NuGet Package using the NuGet Package Manager GUI.
- Install a NuGet Package using the Package Manager Console. You may use the following command
PM> Install-Package Aspose.Html
. - Install Aspose.HTML for .NET through MSI.
This library supports parsing of HTML5, CSS3, SVG, and HTML Canvas to construct a Document Object Model (DOM) based on the WHATWG DOM Standard. Aspose.HTML for .NET is written completely in C# and can be used to build any type of 32-bit or 64-bit .NET application including ASP.NET, WCF, WinForms & .NET Core. Before running the .NET conversion example code, make sure that you have OS like Microsoft Windows or a compatible with .NET Framework or .NET Standard, and the development environment like Microsoft Visual Studio. For more details about C# library installation and system requirements, please refer to Aspose.HTML Documentation.