An OFM comment is hidden text marked with two consecutive percent signs (%%). Use comments for notes to yourself or other authors, drafts, and other text that shouldn’t appear in the rendered document.
OFM comments originate in Obsidian Flavored Markdown. This document describes Comrak’s OFM comments.
To enable OFM comments, pass -e ofm-comments on the command line, or set extension.ofm_comments in the options struct.
Here’s a paragraph with an inline comment, followed by a block comment:
$ comrak -e ofm-comments -t commonmark -t xml
This is an comment.
This is an comment.
This is an
inline
comment.
This is a block comment.
Block comments can span multiple lines.
Syntax
A comment marker is two consecutive percent signs (%%). A single percent sign is ordinary text. Comrak recognises two kinds of comments: inline comments and block comments.
The contents of a comment are literal text. Comrak doesn’t parse Markdown inside a comment, and HTML output omits the comment entirely.
Inline comments
An inline comment begins with a comment marker in inline content and ends at the next comment marker on the same line. Its opening and closing markers can appear anywhere in the inline content:
$ comrak -e ofm-comments -t commonmark -t xml
-
A heading
- An item
Bold text
A heading
An item
Bold text
-
A
hidden
heading
An
hidden
item
Bold
hidden
text
An inline comment may be empty. Its contents may include a single percent sign, and any Markdown inside it stays literal:
$ comrak -e ofm-comments -t commonmark -t xml
a b
a b
a b
a b
a
b
a
100% *not emphasis*
b
Comment markers don’t nest. The next marker always closes the comment, so this example contains two comments with visible text between them:
$ comrak -e ofm-comments -t commonmark -t xml
visible
visible
first
visible
second
An inline comment can’t span a line ending. If no closing marker appears on the same line, the opening marker is literal text:
$ comrak -e ofm-comments -t commonmark -t xml
x %%aa bb%% tail
x %%aa
bb%% tail
x %%aa
bb%% tail
A marker on a later line can close a different inline comment, but it can’t close one left unclosed on an earlier line:
$ comrak -e ofm-comments -t commonmark -t xml
x %%unclosed y z
x %%unclosed
y z
x %%unclosed
y
closed
z
Block comments
A block comment begins with a comment marker at the start of a line, after up to three spaces, when the rest of that line contains no percent sign. Any text after the opening marker is part of the comment. Unlike an inline comment, a block comment can contain line endings, blank lines, and Markdown block structure:
$ comrak -e ofm-comments -t commonmark -t xml
- -
- -
Notes for the next revision
# A hidden heading
- A hidden item
- Another hidden item
A block comment can interrupt a paragraph:
$ comrak -e ofm-comments -t commonmark -t xml
before
after
before
after
before
hidden
after
The block comment ends at the next comment marker, wherever it appears on a later line. Text after the closing marker remains visible, and Comrak parses its Markdown as usual:
$ comrak -e ofm-comments -t commonmark -t xml
-
tail
A visible heading
tail
A visible heading
-
# A hidden heading
- A hidden item
tail
hidden
A visible heading
If the opening line contains another percent sign, it doesn’t begin a block comment. A complete pair of markers on that line forms an inline comment instead:
$ comrak -e ofm-comments -t commonmark -t xml
visible
visible
inline
visible
If that line has no complete pair, its markers remain literal; an inline comment can’t continue onto the next line:
$ comrak -e ofm-comments -t commonmark -t xml
%%a%b cc%%tail
%%a%b
cc%%tail
%%a%b
cc%%tail
A block comment without a closing marker continues to the end of the document:
$ comrak -e ofm-comments -t commonmark -t xml
aa
bb
Block comments may appear in containers, such as block quotes and list items. The usual container markers and indentation apply to the opening and closing lines:
$ comrak -e ofm-comments -t commonmark -t xml
-
visible
- visible
visible
visible
-
hidden
visible
hidden
visible
Precedence and escaping
Code spans and fenced code blocks take precedence over comments. Comment markers inside them are literal text:
$ comrak -e ofm-comments -t commonmark -t xml
%%inline%%
%%
# text
%%
%%inline%%
%%
# text
%%
%%inline%%
%%
# text
%%
A backslash escape or an entity reference produces a literal percent sign, not part of a comment marker:
$ comrak -e ofm-comments -t commonmark -t xml
%%not a comment%%
%%also not a comment%%
%%not a comment%%
%%also not a comment%%
%%not a comment%%
%%also not a comment%%
Output
HTML output omits each comment, including its markers and contents. It doesn’t replace the comment with an HTML comment or any other element. Whitespace around an inline comment remains, which can leave two adjacent spaces:
$ comrak -e ofm-comments -t commonmark -t xml
before after
before after
before
hidden
after
CommonMark output writes comments back with %% markers. It preserves their literal contents, and leaves an unclosed block comment unclosed.
In XML output, both kinds of comment are a comment element with xml:space="preserve". The element’s text is the comment’s literal contents, without its markers.
In the AST, an inline comment is a NodeValue::CommentInline. Its NodeComment holds the literal contents. A block comment is a NodeValue::MultilineComment. Its NodeCommentBlock holds the literal contents and whether the comment was explicitly closed. Neither kind of comment has children.
Interactions
Inline comments work in table cells just as they do in other inline content:
$ comrak -e ofm-comments -e table -t commonmark -t xml
| a | b |
|---|---|
| x y | z |
a
b
x y
z
a
hidden
b
x
hidden
y
z
With OFM callouts, an inline comment can hide part or all of a callout title:
$ comrak -e ofm-callouts -e ofm-comments -t commonmark -t xml
Body
Body
hidden
Body
A block comment between lines of a list item doesn’t by itself make the containing list loose:
$ comrak -e ofm-comments -t commonmark -t xml
- -
- before after
- next
before
after
next
- -
before
hidden
after
next
A blank line inside such a comment still affects the list’s block structure, so it can make the list loose:
$ comrak -e ofm-comments -t commonmark -t xml
-
-
-
after
-
next
after
next
-
-
first hidden paragraph
second hidden paragraph
after
next