How to Create Markdown Inline Code or Code Block 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 Inline Code in C#

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

C# code to add inline code to MD file

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

    // Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree
    var mdf = markdown.SyntaxFactory;

    // Create an empty codeSpan node
    var codeSpan = mdf.CodeSpan();

    // Create text content for the codeSpan
    codeSpan.AppendChild(mdf.Text("Source code text"));

    // Add codeSpan to MD document
    markdown.AppendChild(codeSpan);

    // Add a space after the code node through a special WhiteSpace node
    markdown.AppendChild(mdf.Whitespace());

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

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



Steps to Create Markdown Code in C#

We remind you that the inline code needs to be assembled from the component nodes. Consider simple steps to create Markdown inline code:

  1. Create a new empty CodeSpan node using CodeSpan() constructor in SyntaxFactory.
  2. Use the Text() constructor to create text nodes for the CodeSpan.
  3. Call the AppendChild() method of the MarkdownSyntaxNode class to add text nodes into the CodeSpan element.
  4. Add created code block to the document syntax tree using AppendChild() method.
  5. Use the Whitespace() constructor to create space after inline code.

Add Markdown Code Block in C#

The following C# code example shows how to add Markdown code block to MD document:

C# code to add Markdown code block

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

    // Create a Markdown syntax factory
    var mdf = markdown.SyntaxFactory;

    // Create a start token and an end token that will frame a code block
    var startToken = mdf.Token(SourceText.From("```\r\n"));
    var endToken = mdf.Token(SourceText.From("\r\n```"));

    // Create a fenced code element
    var fencedCodeSpan = mdf.FencedCodeBlock(startToken, null, endToken);

    // Create text content for the fenced code element
    fencedCodeSpan.AppendChild(mdf.Text("Source code text"));

    //Add the fenced code element to MD file
    markdown.AppendChild(fencedCodeSpan);
            
    // Prepare a path for MD file saving 
    string savePath = Path.Combine(OutputDir, "markdown-code-block.md");

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