How to Create Markdown Bold 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.
The MarkdownSyntaxFactory class contains methods to create various Markdown elements. For bold text making, we will use the Emphasis() method that takes as a parameter the Emphasis enumeration value Emphasis.Strong. You can use bold for single words, multiple words, sentences, paragraphs, and so on, depending on the formatting style of the document.


Markdown Bold Text in C#

Using the C# library, you can programmatically edit MD files – change the document structure and content. Let’s look at creating a new Markdown document from scratch and adding bold text to it.

C# code to create bold in Markdown

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

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

    // Create bold object add text for it
    var bold = mdf.Emphasis(Emphasis.Strong);
    bold.AppendChild(mdf.Text("Bold in Markdown"));

    // Add bold text to the Markdown syntax tree
    markdown.AppendChild(bold);

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

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



Steps to Create Markdown Bold in C#

Let’ discuss C# example based on bold in Markdown. We consider a few simple steps to create Markdown bold text:

  1. Create the MarkdownSyntaxTree using MarkdownSyntaxTree() constructer.
  2. Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree.
  3. Create bold object add text for it using Emphasis() constructor and the Text() constructor.
  4. Call the AppendChild() method of the MarkdownSyntaxNode class to add bold node into the the document syntax tree.

Markdown Bold for just One Word in a Sentence

We can use bold for single words, multiple words, sentences, paragraphs, and so on, depending on the formatting style of the document. The following C# code example shows how to add bold Markdown for just one word in a sentence.

C# code to add Markdown bold

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

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

    // Create an empty paragraph
    var paragraph = mdf.Paragraph();

    // Add texts and whitespace to the paragraph
    paragraph.AppendChild(mdf.Text("The Markdown bold is used to"));
    paragraph.AppendChild(mdf.Whitespace());

    // Create bold object add text for it
    var bold = mdf.Emphasis(Emphasis.Strong);
    bold.AppendChild(mdf.Text("bold"));

    // Add bold text and whitespace after it to the paragraph
    paragraph.AppendChild(bold);
    paragraph.AppendChild(mdf.Whitespace());

    // Complete the text of sentence and add to the paragraph
    paragraph.AppendChild(mdf.Text("the text, and it is similar to the bold in Microsoft Word and other text formatting documents."));

    // Add paragraph into MD document
    md.AppendChild(paragraph);

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

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

The rendered result must be: The bold command is used to bold the text, and it is similar to the bold in Microsoft word and other text formatting documents.


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.