PHP code example of taufik-nurrohman / markdown-filter
1. Go to this page and download the library: Download taufik-nurrohman/markdown-filter 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-filter example snippets
use function x\markdown_filter\row as filter_row;
use function x\markdown_filter\rows as filter_rows;
$status) {
if (0 === $status || 2 === $status) {
return $block;
}
return filter_row($block, function ($chop, $status) {
if (0 === $status) {
return $chop;
}
// Safely replace `:)` with `😊`
return strtr($chop, [':)' => '😊']);
});
});
// You can now convert the Markdown document to HTML using your preferred Markdown converter
echo (new ParsedownExtra)->text($content);
~~~
### Using File
Require the `index.php` file in your application:
~~~ php
use function x\markdown_filter\row as filter_row;
use function x\markdown_filter\rows as filter_rows;
ns that the part of the document is generally not safe
for any kind of text substitutions. It is typically contained in the code and raw HTML chunks. A value of `2` means that
a block can contain other blocks, so it would be better to skip it as well, because indentation usually has a different
meaning in this situation, until then this filter reaches into the inner content of that block.
The main goal of this project is to introduce [the “embed” syntax for Markdown][1], which I believe has never been
discussed before (for this kind of syntax). That’s why I implemented this filter on the test page as a sort of utility
to safely replace the syntax:
![Example][2]
[1]: https://github.com/taufik-nurrohman/markdown
[2]: https://github.com/taufik-nurrohman/markdown-filter/assets/1669261/7fe0f9be-9d25-4e1e-b947-8a51a0275a3a
You can also use this filter to strip HTML tags other than those that are written in Markdown’s code syntax. People
usually write HTML syntax there to share a piece of code in your comments section:
~~~ php
use function x\markdown_filter\row as filter_row;
use function x\markdown_filter\rows as filter_rows;
if ('POST' === $_SERVER['REQUEST_METHOD'] && isset($_POST['comment']['content'])) {
$_POST['comment']['content'] = filter_rows($_POST['comment']['content'], function ($block, $status) {
if (2 === $status) {
return $block;
}
if (0 === $status) {
$dent = strspn($block, ' ');
if ($dent >= 4) {
return $block; // Code block (indent-style)
}
$test = substr($block, $dent);
if (0 === strpos($test, '```') || 0 === strpos($test, '~~~')) {
return $block; // Code block (fence-style)
}
return strip_tags($block);
}
return filter_row($block, function ($chop, $status) {
if (0 === $status) {
$test = strspn($chop, '`');
if ($test > 0 && str_repeat('`', $test) === substr($chop, -$test)) {
return $chop; // Code span
}
}
return strip_tags($chop);
});
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.