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:

  1. Create a new empty paragraph node using Paragraph() constructor in SyntaxFactory.
  2. Use the Text() constructor to create text nodes for paragraph.
  3. Call the AppendChild() method of the MarkdownSyntaxNode class to add text nodes and whitespaces into paragraph.
  4. Use the Whitespace() constructor to create space between text nodes in paragraph.
  5. Create and add newLineTrivia element for an empty line after paragraph. Use the NewLineTrivia() constructor and the Add() method.
  6. 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.


Get Started with Aspose.HTML for .NET API

Install Aspose.HTML for .NET from NuGet with the .NET CLI:

dotnet add package Aspose.HTML

You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the installation guide for setup details, or request a temporary license to evaluate the API without evaluation limitations.

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.