How to Create Markdown Header 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 Header in C#

Using the C# library, you can programmatically edit MD files – change the document structure and content. Creating a Markdown header element in C# has a feature. The feature is that the header needs to be assembled from several nodes, such as text nodes, spaces between text nodes, and an empty line after the header. Let’s look at creating a new Markdown document from scratch and adding headers to it.

C# code to edit MD files

     // Create the MarkdownSyntaxTree
    var markdown = new MarkdownSyntaxTree(new Configuration());

    // Get a SyntaxFactory to create new elements
    var mdf = markdown.SyntaxFactory;

    // Create headings of the required level
    var heading1 = mdf.AtxHeading("Heading 1 level text", 1);
    var heading2 = mdf.AtxHeading("Heading 2 level text", 2);
    var heading3 = mdf.AtxHeading("Heading 3 level text", 3);

    // Create and add newLineTrivia element for an empty line after headings 
    var newLineTrivia = mdf.NewLineTrivia();
    heading1.GetTrailingTrivia().Add(newLineTrivia);
    heading2.GetTrailingTrivia().Add(newLineTrivia);
    heading3.GetTrailingTrivia().Add(newLineTrivia);

    // Add the headers to the Markdown syntax tree
    markdown.AppendChild(heading1);
    markdown.AppendChild(heading2);
    markdown.AppendChild(heading3);

    // Prepare a path for MD file saving 
    string savePath = Path.Combine(OutputDir, "output-headers.md");

    // Save MD file
    markdown.Save(savePath);



Steps to Create Markdown Header in C#

Let’s consider the simple steps to create a Markdown header:

  1. Create a new header using AtxHeading(string, int) constructor of the MarkdownSyntaxFactory class. It creates AtxHeadingSyntaxNode with text content and heading level.
  2. Create a newLineTrivia element for an empty line after header. Use the NewLineTrivia() constructor.
  3. Call the Add() method of the SyntaxNodeCollection class to add the newLineTrivia node into header element.
  4. After all header nodes have been collected into one element, add it to the document syntax tree.

Edit Markdown Header in C#

The following C# code example shows how to edit Markdown header in an existing MD document:

C# code to add Markdown Header

    // Specify the path to the source MD file
    var input = "C:/temp/document.md";

    // Create a MarkdownParser object
    var parser = new MarkdownParser();

    // Parse the document and get a Markdown 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("Added 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, "edit-header.md");

    // Save MD 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.