Download the PHP package taufik-nurrohman/markdown without Composer
On this page you can find all versions of the php package taufik-nurrohman/markdown. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download taufik-nurrohman/markdown
More information about taufik-nurrohman/markdown
Files in taufik-nurrohman/markdown
Package markdown
Short Description Obviously, a Markdown parser.
License MIT
Homepage https://github.com/taufik-nurrohman/markdown
Informations about the package markdown
PHP Markdown Parser
With 99% compliance to the CommonMark 0.31.2 specifications. It is as accurate as
league/commonmark, but faster 🚀
Motivation
I appreciate the Parsedown project for its simplicity and speed. It only uses a single class file to convert Markdown syntax to HTML. However, given the decrease in the project’s activity over time, I assume it is already “feature complete”. There are still some bugs to fix, and some of the PHP syntax there has become obsolete with the recent release of PHP version 8.1.
There is actually a draft available for Parsedown version 2.0, but it is no longer created as a single class file. It has been broken down into components. I think the goal is to make it easy to add functionality without messing with the core features. Others may find it useful, but I find it too similar to the features provided by CommonMark. Therefore, if I want to upgrade, it would be better to simply switch to CommonMark.
I’m not into things like that. As someone who just needs a function to convert Markdown syntax to HTML, that kind of flexibility is completely unnecessary for me. I just want to convert Markdown syntax to HTML for once and then move on. Parsedown version 1.8 did the job, but it seems that it is no longer being actively maintained.
The goal of this project is to have it ready for use in my future Markdown extension for Mecha. Initially, I wanted to develop the parser directly into the extension. However, my friend advised me to create this project separately, as it may be useful for developers who work with other applications besides the Mecha CMS.
Usage
This parser can be installed using Composer, but it doesn’t require any other dependencies.
It simply uses Composer’s ability to automatically include files. Those of you who don’t use Composer should be able to
include the from.php file directly into your application.
Using Composer
From the command line interface, navigate to your project folder then run this command:
composer require taufik-nurrohman/markdown
Require the generated auto-loader file in your application:
<?= from($value, ['tab' => 2]); ?>
<?= from($value, ['tab' => "\t"]); ?>
with
A very simple extension system:
"[^"]*"|\'[^\']*\'|[^>])*)?>(.+?)<\/\1>/s', static function ($m) use (&$f) {
if (!empty($m[2]) && false !== strpos($m[2], 'id=') && preg_match('/\bid=("[^"]+"|\'[^\']+\'|[^\s>]+)/', $m[2], $v)) {
if ('"' === $v[1][0] && '"' === $v[1][-1]) {
$id = substr($v[1], 1, -1);
} else if ("'" === $v[1][0] && "'" === $v[1][-1]) {
$id = substr($v[1], 1, -1);
} else {
$id = $v[1];
}
$f[$id] = ($f[$id] ?? -1) + 1;
$id .= ($f[$id] > 0 ? '.' . $f[$id] : "");
$m[3] = '<a href="#' . htmlspecialchars($id) . '" style="text-decoration: none;">⚓</a> ' . $m[3];
return '<' . $m[1] . $m[2] . '>' . $m[3] . '</' . $m[1] . '>';
}
$text = html_entity_decode($m[3], ENT_HTML5 | ENT_QUOTES, 'UTF-8');
$id = trim(preg_replace('/[^a-z\d]+/', '-', strtolower($text)), '-');
$f[$id] = ($f[$id] ?? -1) + 1;
$id .= ($f[$id] > 0 ? '.' . $f[$id] : "");
$m[3] = '<a href="#' . htmlspecialchars($id) . '" style="text-decoration: none;">⚓</a> ' . $m[3];
return '<' . $m[1] . ($m[2] ?? "") . ' id="' . htmlspecialchars($id) . '">' . $m[3] . '</' . $m[1] . '>';
}, $value);
}
echo $value;
Idea: Embed Syntax
The CommonMark specification for automatic links does not limit specific types of URL schemes. It specifies a pattern that allows us to use the automatic link syntax as a kind of “embed” syntax. This can then be transformed into a chunk of HTML elements.
I’m sure this idea has never been done before, which is why I want to be the first to mention it. However, I won’t integrate this feature directly into my parser to keep it slim. I just want to give you a couple ideas.
Here’s a naive method that converts auto-links with gist: scheme into GitHub’s Gist embed
code:
$value = preg_replace('/^[ ]{0,3}<gist:([^>]+)>\s*$/m', '<script src="https://gist.github.com/$1.js"></script>', $value);
echo from($value);
That method does not take fenced code blocks and raw HTML blocks into consideration. Therefore, you will likely end up converting auto-link syntax in places where it should be left as is:
<gist:taufik-nurrohman/1da313a2db48a26d836d781258b6c139>
~~~ md
<gist:taufik-nurrohman/1da313a2db48a26d836d781258b6c139>
To avoid such cases, transform the auto-link syntax [this way](x/embed.php).
### Idea: Note Block
Several people have discussed this feature, and I think I like
[this answer](https://stackoverflow.com/a/41449789/1163000) the most. The syntax is compatible with native Markdown
syntax, which is nice to look at directly through the Markdown source, even when it gets rendered to HTML:
~~~ md
------------------------------
**NOTE:** asdf asdf asdf
------------------------------
------------------------------
**NOTE:**
asdf asdf asdf asdf
asdf asdf asdf asdf
asdf asdf asdf asdf
------------------------------
Most Markdown parsers will render the above syntax to the following HTML, which is still acceptable as a note block from its appearance, despite its broken semantics:
<hr />
<p><strong>NOTE:</strong> asdf asdf asdf</p>
<hr />
<hr />
<p><strong>NOTE:</strong></p>
<p>asdf asdf asdf asdf asdf asdf asdf asdf</p>
<p>asdf asdf asdf asdf</p>
<hr />
With regular expressions, you can improve its semantic:
$value = from($value);
$value = preg_replace_callback('/<hr\s*\/?>(\s*<p><strong>NOTE:<\/strong>.*?<\/p>\s*)<hr\s*\/?>/s', static function ($m) {
return '<div role="note">' . $m[1] . '</div>';
}, $value);
echo $value;
License
This library is licensed under the MIT License. Please consider donating 💰 if you benefit financially from this library.
Links
- Autumn image sample by @blmiers2
- Emoticon image sample by @emoticons4u (web archive)