How to Create Markdown Paragraph in C#
Aspose.HTML for .NET
library is a stand-alone solution that allows you to parse and manipulate Markdown files without using other software. You can create a Markdown document from scratch, open from a local file and manipulate by inserting new elements, removing, or editing the content of existing nodes.
Use the
Aspose.Html.Toolkit.Markdown.Syntax
and
Aspose.Html.Toolkit.Markdown.Parser
namespaces to provide full Markdown parsing and MD syntax tree management based on the GitHub Flavored Markdown (GFM) specification.
How to Add Markdown Paragraph in C#
Using the C# library, you can programmatically edit MD files – change the document structure and content. Creating an MD paragraph element in C# has a feature. The feature is that the paragraph needs to be assembled from several nodes, such as text nodes, spaces between text nodes, and an empty line after the paragraph. Let’s look at creating a new Markdown document from scratch and adding two paragraphs to it.
C# code to edit MD files
// Create the MarkdownSyntaxTree
var md = new MarkdownSyntaxTree(new Configuration());
// Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree
var mdf = md.SyntaxFactory;
// Create an empty paragraph1
var paragraph1 = mdf.Paragraph();
// Add texts to the paragraph1
paragraph1.AppendChild(mdf.Text("The first sentence of the first paragraph."));
// Add a space after the first sentence in the paragraph through a special WhiteSpace node
paragraph1.AppendChild(mdf.Whitespace());
paragraph1.AppendChild(mdf.Text("The second sentence of the first paragraph."));
// Create and add newLineTrivia element for an empty line after paragraph1
var newLineTrivia = mdf.NewLineTrivia();
paragraph1.GetTrailingTrivia().Add(newLineTrivia);
// Add the filled paragraph1 into MD document
md.AppendChild(paragraph1);
// Create an empty paragraph2
var paragraph2 = mdf.Paragraph();
// Add texts to the paragraph2
paragraph2.AppendChild(mdf.Text("The first sentence of the second paragraph."));
// Add a space after the first sentence in the paragraph through a special WhiteSpace node
paragraph2.AppendChild(mdf.Whitespace());
paragraph2.AppendChild(mdf.Text("The second sentence of the second paragraph."));
// Add newLineTrivia element for an empty line after paragraph2
paragraph2.GetTrailingTrivia().Add(newLineTrivia);
// Add the filled paragraph2 into MD document as a last child
md.AppendChild(paragraph2);
// Prepare a path for MD file saving
string savePath = Path.Combine(OutputDir, "markdown-paragraph.md");
// Save MD file
md.Save(savePath);
Steps to Create Markdown Paragraph in C#
We remind you that the paragraph needs to be assembled from the component nodes. Consider simple steps to create Markdown paragraph:
- Create a new empty paragraph node using Paragraph() constructor in SyntaxFactory.
- Use the Text() constructor to create text nodes for paragraph.
- Call the AppendChild() method of the MarkdownSyntaxNode class to add text nodes and whitespaces into paragraph.
- Use the Whitespace() constructor to create space between text nodes in paragraph.
- Create and add newLineTrivia element for an empty line after paragraph. Use the NewLineTrivia() constructor and the Add() method.
- After all paragraph nodes have been collected into one element, add it to the document syntax tree.
Add Markdown Paragraph in C#
The following C# code example shows how to add paragraph in an existing MD document:
C# code to add paragraph
// Specify the path to the source MD file
var input = "C:/temp/input.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 empty 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("Markdown language is simple to learn. It has minimal extra characters, so you can quickly write and make fewer errors.");
// Add text into paragraph
paragraphSyntaxNode.AppendChild(textSyntaxNode);
// Add paragraph to MD document 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.md");
// Save Markdown file
syntaxTree.Save(savePath);
Documentation
To learn more about Aspose.HTML API, please visit our
documentation
guide. Markdown is a simple markup language that allows you to format plain text. The
Markdown Syntax
documentation article provides information on the main Markdown elements, details and examples of the Markdown syntax.
FAQ
The Aspose.HTML for .NET API is a stand-alone solution for working with Markdown documents that does not depend on other software. Install our C# library, add the library reference to your C# project, and programmatically work with Markdown documents.
Markdown is very popular among writers, developers, and content creators due to its versatility. Its advantages include easy-to-learn and use syntax, portability, flexibility, readability, and usability. Markdown is simple and intuitive and can be converted to HTML, PDF, or other formats.
You can work with Markdown documents on any operating system, whether you’re using Windows, Mac OS, Linux, Android, or iOS.
We support several file formats that you can create, open, parse, edit, save or convert in C#. These are HTML, XHTML, MHTML and Markdown formats.
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.
System Requirements
Our APIs are supported on all major platforms and Operating Systems. Before executing the code, please ensure that you have the following prerequisites on your system.
- Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms.
- Development environment like Microsoft Visual Studio.
- Aspose.Html for .NET DLL referenced in your project – Install from NuGet using the Download button above.