Core Rules

These rules are enabled by default in Parser() and implement the CommonMark specification. They cover the standard markdown syntax that most users expect.

using CommonMark
parser = Parser()

Block Rules

Block rules handle document structure: headings, paragraphs, lists, code blocks, and other elements that occupy their own lines.

ATX Headings

Headings prefixed with # characters. Supports levels 1-6.

ast = parser("# Heading 1\n## Heading 2\n### Heading 3")
html(ast)
"<h1>Heading 1</h1>\n<h2>Heading 2</h2>\n<h3>Heading 3</h3>\n"

Setext Headings

Headings underlined with = or -. Only supports levels 1 and 2.

ast = parser("Heading 1\n=========\n\nHeading 2\n---------")
html(ast)
"<h1>Heading 1</h1>\n<h2>Heading 2</h2>\n"

Block Quotes

Quoted text prefixed with >. Can be nested and contain other block elements.

ast = parser("> This is a block quote.\n> It can span multiple lines.")
html(ast)
"<blockquote>\n<p>This is a block quote.\nIt can span multiple lines.</p>\n</blockquote>\n"

Lists

Unordered lists use -, +, or * as markers:

ast = parser("- Item one\n- Item two\n- Item three")
html(ast)
"<ul>\n<li>Item one</li>\n<li>Item two</li>\n<li>Item three</li>\n</ul>\n"

Ordered lists use numbers followed by . or ):

ast = parser("1. First item\n2. Second item\n3. Third item")
html(ast)
"<ol>\n<li>First item</li>\n<li>Second item</li>\n<li>Third item</li>\n</ol>\n"

Lists can be nested by indenting items, and can contain multiple paragraphs or other block elements.

Fenced Code Blocks

Code blocks delimited by triple backticks or tildes. An optional info string specifies the language for syntax highlighting.

ast = parser("""
```julia
println("Hello, World!")
```
""")
html(ast)
"<pre><code class=\"language-julia\">println(&quot;Hello, World!&quot;)\n</code></pre>\n"

Indented Code Blocks

Code indented by at least 4 spaces. No language info string is possible.

ast = parser("    function hello()\n        println(\"Hello\")\n    end")
html(ast)
"<pre><code>function hello()\n    println(&quot;Hello&quot;)\nend\n</code></pre>\n"

HTML Blocks

Raw HTML that passes through unchanged. Useful for embedding content that markdown can't express.

ast = parser("<div class=\"custom\">\n  Raw HTML content.\n</div>")
html(ast)
"<div class=\"custom\">\n  Raw HTML content.\n</div>\n"

Thematic Breaks

Horizontal rules created with three or more -, *, or _ characters.

ast = parser("Above\n\n---\n\nBelow")
html(ast)
"<p>Above</p>\n<hr />\n<p>Below</p>\n"

Inline Rules

Inline rules handle formatting within paragraphs: emphasis, links, code spans, and other elements that flow with text.

Emphasis

Asterisks and underscores create emphasis. Single delimiters produce <em>, double delimiters produce <strong>.

ast = parser("*italic* and **bold** and ***both***")
html(ast)
"<p><em>italic</em> and <strong>bold</strong> and <em><strong>both</strong></em></p>\n"
ast = parser("_italic_ and __bold__ and ___both___")
html(ast)
"<p><em>italic</em> and <strong>bold</strong> and <em><strong>both</strong></em></p>\n"

Inline Code

Backticks create code spans. Use multiple backticks to include literal backticks.

ast = parser("Use `code` for inline code.")
html(ast)
"<p>Use <code>code</code> for inline code.</p>\n"

Inline links with the URL in parentheses, or reference links defined elsewhere.

ast = parser("[inline link](https://example.com)")
html(ast)
"<p><a href=\"https://example.com\">inline link</a></p>\n"

Reference-style links separate the URL from the text, useful for keeping paragraphs readable or reusing the same URL multiple times.

Images

Same syntax as links but prefixed with !. The link text becomes alt text.

ast = parser("![alt text](image.png)")
html(ast)
"<p><img src=\"image.png\" alt=\"alt text\" /></p>\n"

URLs and email addresses in angle brackets become clickable links automatically.

ast = parser("<https://example.com>")
html(ast)
"<p><a href=\"https://example.com\">https://example.com</a></p>\n"

HTML Inline

Raw HTML tags within paragraphs pass through unchanged.

ast = parser("This has <em>inline HTML</em> content.")
html(ast)
"<p>This has <em>inline HTML</em> content.</p>\n"

HTML Entities

Named and numeric HTML entities are decoded.

ast = parser("&amp; &copy; &mdash;")
html(ast)
"<p>&amp; © —</p>\n"

Disabling Default Rules

Use disable! to turn off rules you don't want. This is useful for stricter parsing or when certain syntax conflicts with your content.

p = Parser()
disable!(p, SetextHeadingRule())  # Only ATX headings
disable!(p, [HtmlBlockRule(), HtmlInlineRule()])  # No raw HTML