Translate Markdown to Document Object Model (DOM)

To programmatically read, manipulate, and modify the content and formatting of a document, you need to translate it to the Aspose.Words Document Object Model (DOM).

In contrast to Word documents, Markdown does not conform to the DOM described in the “Aspose.Words Document Object Model (DOM)" article. However, Aspose.Words provides its own mechanism for translating Markdown documents to DOM and back, so that we can successfully work with their elements such as text formatting, tables, headers, and others.

This article explains how the various markdown features can be translated into Aspose.Words DOM and back to Markdown format.

Complexity of Translation Markdown – DOM – Markdown

The main difficulty of this mechanism is not only to translate Markdown to DOM, but also to do the reverse transformation – to save the document back to Markdown format with minimal loss. There are elements, such as multilevel quotes, for which the reverse transformation is not trivial.

Our translation engine allows users not only to work with complex elements in an existing Markdown document, but also to create their own document in Markdown format with the original structure from scratch. To create various elements, you need to use styles with specific names according to certain rules described later in this article. Such styles can be created programmatically.

Common Translation Principles

We use Font formatting for inline blocks. When there is no direct correspondence for a Markdown feature in Aspose.Words DOM, we use a character style with a name that starts from some special words.

For container blocks, we use style inheritance to denote nested Markdown features. In this case, even when there are no nested features, we also use paragraph styles with a name that starts from some special words.

Bulleted and ordered lists are container blocks in Markdown as well. Their nesting is represented in DOM the same way as for all other container blocks using style inheritance. However, additionally, lists in DOM have corresponded number formatting in either list style or paragraph formatting.

Inline Blocks

We use Font formatting when translating Bold, Italic or Strikethrough inline markdown features.

Markdown feature Aspose.Words
Bold
**bold text**
Font.Bold = true
Italic
*italic text*
Font.Italic = true
Strikethrough
~Strikethrough text~
Font.StrikeThrough = true
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// Make the text Strikethrough.
builder.Font.Strikethrough = true;
builder.Writeln("This text will be Strikethrough");

We use a character style with a name that starts from the word InlineCode, followed by an optional dot (.) and a number of backticks (`) for the InlineCode feature. If a number of backticks is missed, then one backtick will be used by default.

Markdown feature Aspose.Words
InlineCode
**inline code**
Font.StyleName = “InlineCode[.][N]”
Autolink
<scheme://domain.com>
<email@domain.com>
The FieldHyperlink class.
Link
[link text](url)
[link text](<url> "title")
[link text](url 'title')
[link text](url (title))
The FieldHyperlink class.
Image
![](url)
![alt text](<url> “title”)
![alt text](url ‘title’)
![alt text](url (title))
The Shape class.

Container Blocks

A document is a sequence of container blocks such as headings, paragraphs, lists, quotes, and others. Container blocks can be divided into 2 classes: Leaf blocks and Complex Containers. Leaf blocks can only contain inline content. Complex containers, in turn, can contain other container blocks, including Leaf blocks.

Leaf Blocks

The table below demonstrates examples of using Markdown Leaf blocks in Aspose.Words:

Markdown feature Aspose.Words
HorizontalRule
-----
This is a simple paragraph with a corresponding HorizontalRule shape:
DocumentBuilder.InsertHorizontalRule()
ATX Heading
# H1, ## H2, ### H3…
ParagraphFormat.StyleName = “Heading N”, where (1<= N <= 9).
This is translated into a built-in style and should be exactly of the specified pattern (no suffixes or prefixes are allowed).
Otherwise, it will be just a regular paragraph with a corresponding style.
Setext Heading
=== (if Heading level 1),
--- (if Heading level 2)
ParagraphFormat.StyleName = “SetextHeading[some suffix]”, based on “Heading N” style.
If (N >= 2), then “Heading 2” will be used, otherwise “Heading 1”.
Any suffix is allowed, but Aspose.Words importer uses numbers “1” and “2” respectively.
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

builder.ParagraphFormat.StyleName = "Heading 1";
builder.Writeln("This is an H1 tag");

// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.Font.Bold = false;
builder.Font.Italic = false;

Style setexHeading1 = builder.Document.Styles.Add(StyleType.Paragraph, "SetextHeading1");
builder.ParagraphFormat.Style = setexHeading1;
builder.Document.Styles["SetextHeading1"].BaseStyleName = "Heading 1";
builder.Writeln("Setext Heading level 1");

builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("This is an H3 tag");

// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.Font.Bold = false;
builder.Font.Italic = false;

Style setexHeading2 = builder.Document.Styles.Add(StyleType.Paragraph, "SetextHeading2");
builder.ParagraphFormat.Style = setexHeading2;
builder.Document.Styles["SetextHeading2"].BaseStyleName = "Heading 3";

// Setex heading level will be reset to 2 if the base paragraph has a Heading level greater than 2.
builder.Writeln("Setext Heading level 2");
Indented Code ParagraphFormat.StyleName = “IndentedCode[some suffix]”
Fenced Code
``` c#
if ()
then
else
```
ParagraphFormat.StyleName = “FencedCode[.][info string]”
The [.] and [info string] are optional.

Complex Containers

The table below demonstrates examples of using Markdown Complex Containers in Aspose.Words:

Markdown feature Aspose.Words
Quote
> quote,
>> nested quote
ParagraphFormat.StyleName = “Quote[some suffix]”
The suffix in style name is optional, but Aspose.Words importer uses the ordered numbers 1, 2, 3, …. in case of nested quotes.
The nesting is defined via the inherited styles.
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// By default a document stores blockquote style for the first level.
builder.ParagraphFormat.StyleName = "Quote";
builder.Writeln("Blockquote");

// Create styles for nested levels through style inheritance.
Style quoteLevel2 = builder.Document.Styles.Add(StyleType.Paragraph, "Quote1");
builder.ParagraphFormat.Style = quoteLevel2;
builder.Document.Styles["Quote1"].BaseStyleName = "Quote";
builder.Writeln("1. Nested blockquote");
BulletedList
- Item 1
- Item 2
- Item 2a
- Item 2b
Bulleted lists are represented using paragraph numbering:
ListFormat.ApplyBulletDefault()
There can be 3 types of bulleted lists. They are only diff in a numbering format of the very first level. These are: ‘-’, ‘+’ or ‘*’ respectively.
OrderedList
1. Item 1
2. Item 2
1) Item 2a
2) Item 2b
Ordered lists are represented using paragraph numbering:
ListFormat.ApplyNumberDefault()
There can be 2 number format markers: ‘.’ and ‘)’. The default marker is ‘.’.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.List.ListLevels[0].NumberFormat = $"{(char) 0}.";
builder.ListFormat.List.ListLevels[1].NumberFormat = $"{(char) 1}.";

builder.Writeln("Item 1");
builder.Writeln("Item 2");

builder.ListFormat.ListIndent();

builder.Writeln("Item 2a");
builder.Writeln("Item 2b");

Tables

Aspose.Words also enables to translate tables into DOM, as shown below:

Markdown feature Aspose.Words
Table
a|b
-|-
c|d
Table, Row and Cell classes.
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// Add the first row.
builder.InsertCell();
builder.Writeln("a");
builder.InsertCell();
builder.Writeln("b");

// Add the second row.
builder.InsertCell();
builder.Writeln("c");
builder.InsertCell();
builder.Writeln("d");

See Also