1. Go to this page and download the library: Download taufik-nurrohman/markdown library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
taufik-nurrohman / markdown example snippets
use function x\markdown\from;
// Returns `'<h1 id="asdf">asdf</h1>'`
~~~
### Using File
Require the `from.php` file in your application:
~~~ php
use function x\markdown\from;
tax only. Any syntax appearing to be
block syntax will be rendered literally. Here’s an example of when this option will be useful:
~~~ php
use function x\markdown\from;
echo '<p>';
echo from('# [asdf](asdf)', [
'block' => false
]); // Returns `'# <a href="asdf">asdf</a>'`
echo '</p>';
~~~
### `tab`
If this option is set to a number greater than or equal to `0`, it will determine the indent size for block elements,
which will tidy up the HTML output. If it is set to a string, the string will be used as the indent. For example, you
can set its value to `"\t"` to indent the HTML output with
[Tab](https://www.unicode.org/Public/18.0.0/charts/nameslist/0000/#0009) characters:
~~~ php
<?= from($value, ['tab' => 0]);
use function x\markdown\from;
// Extension as a closure
$extension = function (array $rows) { /* … */ };
// Extension as an array of event(s)
$extension = [
// Pre-parse
function (?string $value) { /* … */ },
// Parse
function (array $rows) { /* … */ },
// Post-parse
function (?string $value) { /* … */ }
];
// Extension as a function
function extension(array $rows) { /* … */ }
// Extension as a class
class Extension {
public function __invoke(array $rows) { /* … */ }
}
echo from($value, [
'with' => [
$extension,
'extension',
new Extension,
// …
]
]);
~~~
Dialect
-------
From time to time, the history of Mecha influences my Markdown writing style. Mecha’s Markdown extension
[was initially](https://github.com/mecha-cms/mecha/tree/v1.2.2) developed using [Michel Fortin’s Markdown converter](https://michelf.ca/projects/php-markdown),
which I believe was the first PHP-based Markdown converter, originally written in Perl by [John Gruber](https://daringfireball.net/projects/markdown).
I decided to switch to [Parsedown](https://github.com/erusev/parsedown) at the release of [Mecha version 1.2.3](https://github.com/mecha-cms/mecha/tree/v1.2.3)
because it was quite popular at the time. It can also do the conversion process much faster. Emanuil Rusev’s method of
recognizing the block type by [looking at the first character](https://github.com/erusev/parsedown/tree/1.7.4#questions)
is very clever and efficient, in my opinion.
### Attributes
My Markdown parser supports an extensive attribute syntax that includes a combination of `.class` and `#id` attribute
syntax, and `key=value` attribute syntax:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
# asdf {#asdf}
~~~
</td>
<td>
~~~ html
<h1 id="asdf">asdf</h1>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
# asdf {#asdf.asdf}
~~~
</td>
<td>
~~~ html
<h1 class="asdf" id="asdf">asdf</h1>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
# asdf {#asdf.asdf asdf=asdf}
~~~
</td>
<td>
~~~ html
<h1 asdf="asdf" class="asdf" id="asdf">asdf</h1>
~~~
</td>
</tr>
</tbody>
</table>
Inline attributes always win over native and pre-defined attribute syntax:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
[asdf](asdf){href=1}
~~~
</td>
<td>
~~~ html
<p><a href="1">asdf</a></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
[asdf]
[asdf]: asdf {href=1}
~~~
</td>
<td>
~~~ html
<p><a href="1">asdf</a></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
[asdf]{href=2}
[asdf]: asdf {href=1}
~~~
</td>
<td>
~~~ html
<p><a href="2">asdf</a></p>
~~~
</td>
</tr>
</tbody>
</table>
The attribute syntax can be written after any Markdown inline syntax (and some block syntax), as long as there are no
spaces present before it. This is different from the original Markdown Extra attribute syntax rules, which allow for
optional spaces before the opening attribute syntax:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
<asdf:asdf>{#asdf}
~~~
</td>
<td>
~~~ html
<p><a href="asdf:asdf" id="asdf">asdf:asdf</a></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
<asdf@asdf>{#asdf}
~~~
</td>
<td>
~~~ html
<p><a href="mailto:asdf@asdf" id="asdf">asdf@asdf</a></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
{#asdf}
~~~
</td>
<td rowspan="4">
~~~ html
<figure>
<img alt="asdf" id="asdf" src="asdf">
</figure>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
![asdf]{#asdf}
[asdf]: asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
![asdf][]{#asdf}
[asdf]: asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
![asdf][asdf]{#asdf}
[asdf]: asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
*asdf*{#asdf}
~~~
</td>
<td rowspan="2">
~~~ html
<p><em id="asdf">asdf</em></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
_asdf_{#asdf}
~~~
</td>
</tr>
<tr>
<td>
~~~ md
**asdf**{#asdf}
~~~
</td>
<td rowspan="2">
~~~ html
<p><strong id="asdf">asdf</strong></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
__asdf__{#asdf}
~~~
</td>
</tr>
<tr>
<td>
~~~ md
[asdf](asdf){#asdf}
~~~
</td>
<td rowspan="4">
~~~ html
<p><a href="asdf" id="asdf">asdf</a></p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
[asdf]{#asdf}
[asdf]: asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
[asdf][]{#asdf}
[asdf]: asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
[asdf][asdf]{#asdf}
[asdf]: asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
`asdf`{#asdf}
~~~
</td>
<td>
~~~ html
<p><code id="asdf">asdf</code></p>
~~~
</td>
</tr>
</tbody>
</table>
I decided it would be better to not allow optional spaces between them. There are several reasons for this:
1. The CommonMark rules do not allow optional spaces after the link label [^1] for consistency with the link
[shortcut](https://spec.commonmark.org/0.31.2#shortcut-reference-link) syntax. I assume that people who are already
familiar with CommonMark rules would expect me to treat the attribute syntax the same way CommonMark treats the link
parts syntax.
1. It is easier to determine the priority when this construct occurs:
~~~ md
# asdf asdf asdf [asdf](asdf) {#asdf}
~~~
Allowing optional spaces before the attribute syntax would make it difficult to determine if the attribute syntax is
part of the link or the header. With this restriction, the intent is clear:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
# asdf [asdf](asdf){#asdf}
~~~
</td>
<td>
~~~ html
<h1>asdf <a href="asdf" id="asdf">asdf</a></h1>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
# asdf [asdf](asdf) {#asdf}
~~~
</td>
<td>
~~~ html
<h1 id="asdf">asdf <a href="asdf">asdf</a></h1>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
# asdf [asdf](asdf){#asdf} {#asdf}
~~~
</td>
<td>
~~~ html
<h1 id="asdf">asdf <a href="asdf" id="asdf">asdf</a></h1>
~~~
</td>
</tr>
</tbody>
</table>
[^1]: See examples [511](https://spec.commonmark.org/0.31.2#example-511), [542](https://spec.commonmark.org/0.31.2#example-542), and [556](https://spec.commonmark.org/0.31.2#example-556).
Attribute syntax can be classified into two types:
#### Wrapped Attributes
Wrapped attribute syntax contains an attribute list written between curly braces:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
# asdf asdf asdf asdf {#asdf}
~~~
</td>
<td rowspan="2">
~~~ html
<h1 id="asdf">asdf asdf asdf asdf</h1>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
# asdf asdf asdf asdf # {#asdf}
~~~
</td>
</tr>
</tbody>
</table>
#### Naked Attributes
Naked attribute syntax contains an attribute list written with no curly braces. It usually appears after a block marker
where generic textual content should not immediately follow. Naked attribute syntax is currently supported by fenced
code block (where my attribute parser treats the [info string](https://spec.commonmark.org/0.31.2#info-string)),
[_setext_](https://spec.commonmark.org/0.31.2#setext-heading) header, and thematic break syntax:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~~ md
~~~~
</td>
<td rowspan="2">
~~~ html
<pre><code asdf="asdf" id="asdf">asdf asdf asdf asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ #asdf asdf=asdf
asdf asdf asdf asdf
~~~
~~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
=== #asdf asdf=asdf
~~~
</td>
<td>
~~~ html
<h1 asdf="asdf" id="asdf">asdf asdf asdf asdf</h1>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
--- #asdf asdf=asdf
~~~
</td>
<td>
~~~ html
<h2 asdf="asdf" id="asdf">asdf asdf asdf asdf</h2>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
*** #asdf asdf=asdf
asdf asdf asdf asdf
~~~
</td>
<td rowspan="3">
~~~ html
<p>asdf asdf asdf asdf</p>
<hr asdf="asdf" id="asdf">
<p>asdf asdf asdf asdf</p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
--- #asdf asdf=asdf
asdf asdf asdf asdf
~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
___ #asdf asdf=asdf
asdf asdf asdf asdf
~~~
</td>
</tr>
</tbody>
</table>
There is one special case regarding how they treat value-less attributes (attributes without the `=` part). Wrapped
attributes will treat value-less attributes as boolen attributes, whereas naked attributes will simply reject the entire
attribute list:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
============== asdf
~~~
</td>
<td>
~~~ html
<p>asdf asdf asdf asdf ============== asdf</p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
============ {asdf}
~~~
</td>
<td>
~~~ html
<h1 asdf>asdf asdf asdf asdf</h1>
~~~
</td>
</tr>
</tbody>
</table>
An exception to the fenced code block syntax is that value-less attributes in a naked attribute list will be treated as
a class suffix instead. This feature is implemented for compatibility with other Markdown parsers, which will generally
treat the info string as a code class with a `language-` prefix:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~~ md
~~~ asdf
asdf asdf asdf asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-asdf">asdf asdf asdf asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf asdf
asdf asdf asdf asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf language-asdf">asdf asdf asdf asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {asdf}
asdf asdf asdf asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code asdf>asdf asdf asdf asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {.asdf asdf}
asdf asdf asdf asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code asdf class="asdf">asdf asdf asdf asdf
</code></pre>
~~~
</td>
</tr>
</tbody>
</table>
Most Markdown parsers that support attribute syntax do not explicitly state that they support attribute syntax in the
form of a fenced code block’s info string (my parser called it “naked attributes”) in a _setext_ header or thematic
break. This is just my own take on [this discussion](https://talk.commonmark.org/t/info-strings-elsewhere/2610).
### Code Block
I try to avoid incompatibilities between different Markdown dialects and support whatever dialect you are using. For
example, I am adapted to Markdown Extra’s fenced code block syntax, which employs an info string with a dot prefix.
However, this is not supported by Parsedown. Parsedown simply appends a `language-` prefix to the info string without
giving any special consideration to the pattern of the info string. CommonMark also does not specify any special rules
for processing info strings in fenced code block syntax.
Here’s how the code block results compare across each Markdown converter:
#### Markdown Extra
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~~ md
~~~ asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ asdf asdf
asdf
~~~
~~~~
</td>
<td>
_Invalid._
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf.asdf
asdf
~~~
~~~~
</td>
<td>
_Invalid._
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {#asdf.asdf}
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf" id="asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {#asdf.asdf asdf=asdf}
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code asdf="asdf" class="asdf" id="asdf">asdf
</code></pre>
~~~
</td>
</tr>
</tbody>
</table>
#### Parsedown Extra
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~~ md
~~~ asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-asdf">asdf</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-.asdf">asdf</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ asdf asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-asdf">asdf</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf.asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-.asdf.asdf">asdf</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {#asdf.asdf}
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-{#asdf.asdf}">asdf</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {#asdf.asdf asdf=asdf}
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-{#asdf.asdf">asdf</code></pre>
~~~
</td>
</tr>
</tbody>
</table>
#### Mine
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~~ md
~~~ asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ asdf asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="language-asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ .asdf.asdf
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {#asdf.asdf}
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code class="asdf" id="asdf">asdf
</code></pre>
~~~
</td>
</tr>
<tr>
<td>
~~~~ md
~~~ {#asdf.asdf asdf=asdf}
asdf
~~~
~~~~
</td>
<td>
~~~ html
<pre><code asdf="asdf" class="asdf" id="asdf">asdf
</code></pre>
~~~
</tr>
</tbody>
</table>
### HTML Block
CommonMark doesn’t care about the DOM, so it can’t tell if an HTML element is correctly balanced. Unlike the original
Markdown syntax specification, which doesn’t allow Markdown syntax to be processed inside HTML blocks, the CommonMark
specification doesn’t limit such cases. It only cares about blank lines that appear around lines that resemble HTML
block tags, as specified in [Section 4.6](https://spec.commonmark.org/0.31.2#html-blocks), type 6.
Any text that follows the opening and/or closing of an HTML block is treated as plain text and will not be processed as
Markdown syntax. A blank line is f asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
1) asdf asdf asdf asdf
2) asdf asdf asdf asdf
2) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
1) asdf asdf asdf asdf
3) asdf asdf asdf asdf
4) asdf asdf asdf asdf
5) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol>
<li>asdf asdf asdf asdf 3) asdf asdf asdf asdf 4) asdf asdf asdf asdf 5) asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
1) asdf asdf asdf asdf
3) asdf asdf asdf asdf
2) asdf asdf asdf asdf
3) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf 3) asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
1) asdf asdf asdf asdf
3) asdf asdf asdf asdf
1) asdf asdf asdf asdf
1) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol>
<li>asdf asdf asdf asdf 3) asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
</tbody>
</table>
1. A list item continuation of **type “A”** list block can only use the current character or the next character after
it. After the character “Z”, the list continues with “AA”, “AB”, “AC”, and so on:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
<tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
A) asdf asdf asdf asdf
B) asdf asdf asdf asdf
C) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
A) asdf asdf asdf asdf
A) asdf asdf asdf asdf
A) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
B) asdf asdf asdf asdf
B) asdf asdf asdf asdf
B) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<p>B) asdf asdf asdf asdf B) asdf asdf asdf asdf B) asdf asdf asdf asdf</p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
A) asdf asdf asdf asdf
B) asdf asdf asdf asdf
B) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
A) asdf asdf asdf asdf
C) asdf asdf asdf asdf
D) asdf asdf asdf asdf
E) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf C) asdf asdf asdf asdf D) asdf asdf asdf asdf E) asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
A) asdf asdf asdf asdf
C) asdf asdf asdf asdf
B) asdf asdf asdf asdf
C) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf C) asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
A) asdf asdf asdf asdf
C) asdf asdf asdf asdf
A) asdf asdf asdf asdf
A) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="A">
<li>asdf asdf asdf asdf C) asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
</tbody>
</table>
1. A list item continuation of **type “a”** list block can only use the current character or the next character after
it. After the character “z”, the list continues with “aa”, “ab”, “ac”, and so on.
1. A list Item continuation of **type “I”** list block can only use the current Roman numeral or the next Roman numeral
after it:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
<tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
I) asdf asdf asdf asdf
II) asdf asdf asdf asdf
III) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="I">
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
I) asdf asdf asdf asdf
I) asdf asdf asdf asdf
I) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="I">
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
II) asdf asdf asdf asdf
II) asdf asdf asdf asdf
II) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<p>II) asdf asdf asdf asdf II) asdf asdf asdf asdf II) asdf asdf asdf asdf</p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
I) asdf asdf asdf asdf
II) asdf asdf asdf asdf
II) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="I">
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
I) asdf asdf asdf asdf
III) asdf asdf asdf asdf
IV) asdf asdf asdf asdf
V) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="I">
<li>asdf asdf asdf asdf III) asdf asdf asdf asdf IV) asdf asdf asdf asdf V) asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
I) asdf asdf asdf asdf
III) asdf asdf asdf asdf
II) asdf asdf asdf asdf
III) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="I">
<li>asdf asdf asdf asdf III) asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
I) asdf asdf asdf asdf
III) asdf asdf asdf asdf
I) asdf asdf asdf asdf
I) asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<ol type="I">
<li>asdf asdf asdf asdf III) asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
<li>asdf asdf asdf asdf</li>
</ol>
~~~
</td>
</tr>
</tbody>
</table>
1. A list Item continuation of **type “i”** list block can only use the current Roman numeral or the next Roman numeral
after it.
Those rules should effectively prevent results that most people don’t expect. This usually happens when a paragraph
continuation contains a sequence of letters followed by a `) ` or a `. `.
Consider the following examples. Without stricter rules for the list block, my parser would mistakenly treat `asdf) `
and `asdf. ` as the start of another list item:
~~~
a) asdf (asdf asdf asdf
asdf) asdf asdf asdf
b) asdf asdf asdf asdf
c) asdf asdf asdf asdf
~~~
~~~
a) asdf (asdf asdf asdf
asdf) asdf asdf asdf
b) asdf asdf asdf asdf
c) asdf asdf asdf asdf
~~~
~~~
a. asdf asdf asdf asdf
asdf. asdf asdf asdf
b. asdf asdf asdf asdf
c. asdf asdf asdf asdf
~~~
~~~
a. asdf asdf asdf asdf
asdf. asdf asdf asdf
b. asdf asdf asdf asdf
c. asdf asdf asdf asdf
~~~
Also, consider the following example which is taken from [this discussion](https://talk.commonmark.org/t/bad-interaction-between-laziness-rule-and-ordered-lists/9085?u=taufik-nurrohman):
~~~
1. Before the end of the paragraph, I invite you to
consider a very large number. For example,
45000000. Are you thinking about it?
~~~
With my stricter rules, the result will be more desirable:
<table>
<thead>
<tr>
<th>CommonMark</th>
<th>Mine</th>
<tr>
</thead>
<tbody>
<tr>
<td>
~~~ html
<ol>
<li>Before the end of the paragraph, I invite you to
consider a very large number. For example,</li>
<li>Are you thinking about it?</li>
</ol>
~~~
</td>
<td>
~~~ html
<ol>
<li>Before the end of the paragraph, I invite you to consider a very large number. For example, 45000000. Are you thinking about it?</li>
</ol>
~~~
</td>
</tr>
</tbody>
</table>
Please note that this is just my idea of how CommonMark could improve its implementation. For best compatibility with
other Markdown parsers, I would still recommend you to write the list item numbers in order, just in case you want to
switch in the future. Alternatively, if you’re too lazy or expect that the list will grow over time, you can always
reuse the number from the last list item.
### Notes
Notes follow [Markdown Extra’s syntax for notes](https://michelf.ca/projects/php-markdown/extra#footnotes), but with
slightly different HTML output to match [Mecha](https://github.com/mecha-cms)’s common naming style. Unlike Markdown
Extra, multi-line notes don’t have to be indented by four spaces. A space or tab is suffice to continue the note:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
asdf asdf asdf asdf [^1]
[^1]: asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<p>asdf asdf asdf asdf <sup id="from:1"><a href="#to:1" role="doc-noteref">1</a></sup></p>
<div role="doc-endnotes">
<hr>
<ol>
<li id="to:1" role="doc-endnote">
<p>asdf asdf asdf asdf <a href="#from:1" role="doc-backlink">↩</a></p>
</li>
</ol>
</div>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
asdf asdf asdf asdf [^1]
[^1]:
asdf asdf asdf asdf
-------------------
asdf asdf asdf asdf
asdf asdf asdf asdf
asdf asdf asdf asdf
asdf asdf asdf asdf
asdf asdf asdf asdf
asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<p>asdf asdf asdf asdf <sup id="from:1"><a href="#to:1" role="doc-noteref">1</a></sup></p>
<p>asdf asdf asdf asdf</p>
<div role="doc-endnotes">
<hr>
<ol>
<li id="to:1" role="doc-endnote">
<h2>asdf asdf asdf asdf</h2>
<p>asdf asdf asdf asdf asdf asdf asdf asdf</p>
<pre><code>asdf asdf asdf asdf
</code></pre>
<p>asdf asdf asdf asdf asdf asdf asdf asdf <a href="#from:1" role="doc-backlink">↩</a></p>
</li>
</ol>
</div>
~~~
</td>
</tr>
</tbody>
</table>
### Soft Break
[Soft breaks](https://spec.commonmark.org/0.31.2#softbreak) are collapsed to spaces in non-critical parts, such as
within paragraphs and list items:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
asdf asdf asdf asdf
asdf asdf asdf asdf
asdf asdf asdf asdf
~~~
</td>
<td>
~~~ html
<p>asdf asdf asdf asdf asdf asdf asdf asdf</p>
<p>asdf asdf asdf asdf</p>
~~~
</td>
</tr>
</tbody>
</table>
### Table Block
Table blocks follow [Markdown Extra’s syntax for table blocks](https://michelf.ca/projects/php-markdown/extra#table),
but with a few additional features and rules:
1. The actual number of columns for each row in a table is determined by the number of columns in the table header
separator.
1. If the number of columns in the table header and/or table data exceeds the actual number of columns, the excess
literal pipe characters will be treated as plain text so that they won’t create unnecessary columns. Conversely, if
the number of columns in the table header and/or table data is less than the actual number of columns, several empty
columns will be inserted to the right automatically.
1. Literal pipe characters in table columns must be properly escaped. The only exceptions are those that appear in
[auto-link](https://spec.commonmark.org/0.31.2#autolink), [code span](https://spec.commonmark.org/0.31.2#code-span),
and the attribute values of [raw HTML](https://spec.commonmark.org/0.31.2#raw-html) tags:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
<tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
| asdf | asdf \| asdf |
| ------------ | ------------ |
| asdf \| asdf | asdf |
| asdf | asdf \| asdf |
~~~
</td>
<td>
~~~ html
<table>
<thead>
<tr>
<th>asdf</th>
<th>asdf | asdf</th>
</tr>
</thead>
<tbody>
<tr>
<td>asdf | asdf</td>
<td>asdf</td>
</tr>
<tr>
<td>asdf</td>
<td>asdf | asdf</td>
</tr>
</tbody>
</table>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
| asdf | <asdf:asdf?asdf=|asdf|> |
| ----------------------- | ----------------------- |
| <asdf:asdf?asdf=|asdf|> | asdf |
| asdf | <asdf:asdf?asdf=|asdf|> |
~~~
</td>
<td>
~~~ html
<table>
<thead>
<tr>
<th>asdf</th>
<th><a href="asdf:asdf?asdf=%7Casdf%7C">asdf:asdf?asdf=|asdf|</a></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="asdf:asdf?asdf=%7Casdf%7C">asdf:asdf?asdf=|asdf|</a></td>
<td>asdf</td>
</tr>
<tr>
<td>asdf</td>
<td><a href="asdf:asdf?asdf=%7Casdf%7C">asdf:asdf?asdf=|asdf|</a></td>
</tr>
</tbody>
</table>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
| asdf | `asdf | asdf` |
| ------------- | ------------- |
| `asdf | asdf` | asdf |
| asdf | `asdf | asdf` |
~~~
</td>
<td>
~~~ html
<table>
<thead>
<tr>
<th>asdf</th>
<th><code>asdf | asdf</code></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>asdf | asdf</code></td>
<td>asdf</td>
</tr>
<tr>
<td>asdf</td>
<td><code>asdf | asdf</code></td>
</tr>
</tbody>
</table>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
| asdf | <asdf asdf="|"> |
| --------------- | --------------- |
| <asdf asdf="|"> | asdf |
| asdf | <asdf asdf="|"> |
~~~
</td>
<td>
~~~ html
<table>
<thead>
<tr>
<th>asdf</th>
<th><asdf asdf="|"></th>
</tr>
</thead>
<tbody>
<tr>
<td><asdf asdf="|"></td>
<td>asdf</td>
</tr>
<tr>
<td>asdf</td>
<td><asdf asdf="|"></td>
</tr>
</tbody>
</table>
~~~
</td>
</tr>
</tbody>
</table>
1. Header-less table is supported, but may not be compatible with other Markdown parsers. Consider using this feature
as rarely as possible, unless you have no plans to switch to other Markdown parsers in the future:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
<tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
| asdf | asdf |
| ---- | ---- |
~~~
</td>
<td>
~~~ html
<table>
<thead>
<tr>
<th>asdf</th>
<th>asdf</th>
</tr>
</thead>
</table>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
| ---- | ---- |
| asdf | asdf |
~~~
</td>
<td>
~~~ html
<table>
<tbody>
<tr>
<td>asdf</td>
<td>asdf</td>
</tr>
</tbody>
</table>
~~~
</td>
</tr>
</tbody>
</table>
1. Table captions are supported and can be written in the same way as image captions are written:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
<tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
| asdf | asdf |
| ---- | ---- |
| asdf | asdf |
asdf
asdf
~~~
</td>
<td>
~~~ html
<table>
<caption>asdf</caption>
<thead>
<tr>
<th>asdf</th>
<th>asdf</th>
</tr>
</thead>
<tbody>
<tr>
<td>asdf</td>
<td>asdf</td>
</tr>
</tbody>
</table>
<p>asdf</p>
~~~
</td>
</tr>
<tr>
<td>
~~~ md
| asdf | asdf |
| ---- | ---- |
| asdf | asdf |
asdf
----
asdf
asdf
~~~
</td>
<td>
~~~ html
<table>
<caption>
<h2>asdf</h2>
<p>asdf</p>
</caption>
<thead>
<tr>
<th>asdf</th>
<th>asdf</th>
</tr>
</thead>
<tbody>
<tr>
<td>asdf</td>
<td>asdf</td>
</tr>
</tbody>
</table>
<p>asdf</p>
~~~
</td>
</tr>
</tbody>
</table>
### Tabs
I use a dummy character to hold the visual column position occupied by the
[`\t`](https://www.unicode.org/Public/18.0.0/charts/nameslist/0000#0009) character, so that slicing through the string
will retain the correct visual column position. This is the easiest method to preserve tab characters compared to
tracking tab offsets during every block parsing phase.
Using dummy characters means that the visual column position is stored directly in the string. Thus, I can safely slice
the string, as every character now holds exactly one column. It’s not perfect. You’ll probably still find bugs when
using tabs for indentation. For the best results and maximum compatibility, use multiple spaces for indentation.
I chose [`\v`](https://www.unicode.org/Public/18.0.0/charts/nameslist/0000#000b) as the dummy character because it is
extremely rare for it to be written by hand in a standard Markdown document. It’s also very rare to find it in an HTML
document. The probability of a person wanting to keep a vertical tab in an HTML document exactly as-is is very small.
One benefit of using the `\v` character instead of a more appropriate character such as
[`\x1a`](https://www.unicode.org/Public/18.0.0/charts/nameslist/0000#001a) is that `\v` character can be trimmed using
the PHP’s [`trim()`](https://www.php.net/trim) function, natively.
XSS
---
This parser is intended only to convert Markdown syntax to HTML based on the
[CommonMark](https://spec.commonmark.org/0.31.2) specification. It doesn’t care about your user input. I have no
intention of adding any special security features in the future, sorry. The attribute syntax feature may be a security
risk for you if you want to use this parser on your comment entries, for example:
<table>
<thead>
<tr>
<th>Markdown</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>
~~~ md
{onerror="alert('Yo!')"}
~~~
</td>
<td>
~~~ html
<figure>
<img alt="asdf" onerror="alert('Yo!')" src="asdf">
</figure>
~~~
</td>
</tr>
</tbody>
</table>
There should be many specialized PHP applications already that have specific tasks to deal with XSS, so consider
post-processing the generated HTML markup before putting it out to the web:
- [ezyang/htmlpurifier](https://github.com/ezyang/htmlpurifier)
- [voku/anti-xss](https://github.com/voku/anti-xss)
Tools
-----
Clone this repository into the root of your web server that supports PHP and then you can open the `tools/test/from.php`
and `tools/try/from.php` file with your browser to see the result and the performance of this parser in various cases.
Tweaks
------
Let me share some tips with you so that you can add features without having to ask me to modify the core functionality.
Some of these need to be implemented at the data structure level, which is very specific to my Markdown parser. Others
need to be implemented after the HTML string is ready [^2].
[^2]: This one can actually be applied to any HTML string, not only to the HTML string generated by my Markdown parser.
I will call this type of method the “naive” method.
### Strike
Use this naive method to transform pairs of `~` delimiters into HTML `<del>…</del>` elements before processing the
Markdown. After processing, the `<del>` and `</del>` tags simply become raw HTML tokens:
~~~ php
$value = preg_replace('/(~{1,2})(?!\s)(.+?)(?<!\s)\1/s', '<del>$2</del>', $value);
echo from($value);
~~~
The previous method will blindly parse the [strike syntax](https://github.github.com/gfm#example-491) in codes and
trailing raw HTML block fragments before the blank line or the end of the stream, which should be left as they are. If
you want a more native functionality, you can implement [this method](x/strike.php).
### Strip HTML
If you’re naive, just use [`strip_tags()`](https://www.php.net/strip-tags) and list the HTML elements commonly permitted
in a strict Markdown document:
~~~ php
echo strip_tags(from($value), [
'a', 'img',
'abbr', 'code', 'sup',
'blockquote', 'p', 'pre',
'br', 'hr',
'caption', 'table', 'tbody', 'td', 'th', 'thead', 'tr',
'dd', 'dl', 'dt',
'div', // for `<div role="doc-endnotes">`
'em', 'strong',
'figcaption', 'figure',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'li', 'ol', 'ul',
]);
~~~
Even better, strip them from the Markdown source immediately:
~~~ php
echo from(strip_tags($value));
~~~
The only issue with the second method is that it will misidentify the auto-link syntax as an HTML/XML element, causing
it to be accidentally stripped. To correctly remove only the raw HTML tags from a Markdown document (for example, to
allow `**asdf**` but not `<strong>asdf</strong>`), you can remove them [this way](x/strip.php):
~~~ php
$strip = static function (array $rows) use (&$strip) {
if (!$rows) {
return $rows;
}
foreach ($rows as $k => $row) {
// Find raw HTML
if (false === ($row[0] ?? 0)) {
$rows[$k][1] = strip_tags($row[1]);
continue;
}
// Recurse to look for raw HTML syntax in child data
if (is_array($row[1] ?? 0)) {
$rows[$k][1] = $strip($row[1]);
}
}
return $rows;
};
echo from($value, ['with' => [$strip]]);
~~~
### Task List
I am against the task list feature because it promotes the abuse of form input elements, which is a bad practice.
Although it does display a check box interface correctly, I still believe that input elements should be used inside a
form element. Several Unicode symbols, such as ☐ and ☒, are more suitable and easier to read from the
Markdown source. This means that the task list feature can actually be made using the standard list feature:
~~~ md
- ☒ asdf
- ☐ asdf
- ☐ asdf
~~~
In case you need it or don’t want to revise the syntax of your existing task lists in your Markdown files, here’s a
naive hack:
~~~ php
$value = from($value, ['tab' => false]);
$value = strtr($value, [
// Loose list item(s)
'<li><p>[ ] ' => '<li><p>☐ ',
'<li><p>[x] ' => '<li><p>☒ ',
// Tight list item(s)
'<li>[ ] ' => '<li>☐ ',
'<li>[x] ' => '<li>☒ '
]);
echo $value;
~~~
However, it will only transform the list items and cannot seamlessly add a `class` or `style` attribute to the list
container. If you want to add a `class` or `style` attribute to the list container, it is best to use
[this method](x/task.php).
### Pre-Defined Abbreviations, Notes, and References
By inserting abbreviations, notes, and references at the end of the Markdown content, it will be as if you had a
pre-defined abbreviations, notes, and references feature. According to the
[link reference definitions](https://spec.commonmark.org/0.31.2#example-204), the first declared reference always takes
precedence, so the additional content must be placed at the end of the Markdown content:
~~~ php
$extra = implode("\n", [
"",
// Abbreviation(s)
'*[CSS]: Cascading Style Sheet',
'*[HTML]: Hyper Text Markup Language',
'*[JS]: JavaScript',
"",
// Note(s)
'[^1]: This is an example note.',
"",
// Reference(s)
'[mecha-cms]: https://github.com/mecha-cms (Mecha CMS)',
'[taufik-nurrohman]: https://github.com/taufik-nurrohman (Taufik Nurrohman)',
]);
echo from($value . "\n" . $extra);
~~~
### Pre-Defined Header’s ID
Add an automatic `id` attribute to headers level 2 through 6 if it’s not set, and then prepend an anchor element that
points to it:
~~~ php
$header = static function (array $rows) use (&$header) {
if (!$rows) {
return $rows;
}
static $f = []; // Keep track of the defined automatic identifier(s) to avoid duplicate(s)
foreach ($rows as $k => $row) {
// Find header
if (in_array($row[0] ?? 0, ['h2', 'h3', 'h4', 'h5', 'h6'], true)) {
$text = "";
if (is_array($row[1])) {
foreach ($row[1] as $r) {
if (is_array($r) && false !== $r[0]) {
if (is_string($r[1])) {
$text .= $r[1];
}
continue;
}
if (is_string($r)) {
$text .= $r;
}
}
} else if (is_string($row[1])) {
$text = $row[1];
}
$text = html_entity_decode($text, ENT_HTML5 | ENT_QUOTES, 'UTF-8');
$id = $row[2]['id'] ?? trim(preg_replace('/[^a-z\d]+/', '-', strtolower($text)), '-');
$f[$id] = ($f[$id] ?? -1) + 1;
$id .= ($f[$id] > 0 ? '.' . $f[$id] : ""); // Add a suffix if necessary
// Prepend an anchor element that points to this header
$anchor = ['a', '⚓', [
'href' => '#' . $id,
'style' => 'text-decoration: none;'
]];
if (is_array($row[1])) {
array_unshift($rows[$k][1], $anchor, ' ');
} else if (is_string($row[1])) {
$rows[$k][1] = [$anchor, ' ', $row[1]];
}
// Add `id` attribute
$rows[$k][2]['id'] = $id;
continue;
}
// Recurse to look for header syntax in child data
if (is_array($row[1] ?? 0)) {
$rows[$k][1] = $header($row[1]);
}
}
return $rows;
};
echo from($value, ['with' => [$header]]);
~~~
Or, if you prefer the naive method:
~~~ php
$value = from($value);
if ($value && false !== strpos($value, '</h')) {
static $f = [];
$value = preg_replace_callback('/<(h[2-6])(\s(
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.