User defined syntax for ParsedownExtra
Define whatever syntax you want... using a minimalistic PHP class. Also capable of running PHP embedded in md. Use it for calculations, dynamic content, ...
If you like visit my personal homepage: walter-a-jablonowski.github.io
See also running demo in /demo
composer require walter-a-jablonowski/parsedown-user-styles
Include your stylesheets if you have class names in replace html.
Define whatever syntax you want, these are just for demo. Using Symfony yaml here.
$md = new ParsedownUserStyles(); // pass $userdata if needed (see third style below)
// use parsedown's methods as needed
$md->setStyles( Yaml::parse( file_get_contents( ... )) ); // get your styles.yml
$mdString = $md->text( ... );
Define styles
styles.yml
Unique - name: # Each style can have a name and multiple replacements
- # Simple str replace
find: "[["
replace: "<span class="header">"
-
find: "]]"
replace: "</span>"
Unique - name 2: # regex version
-
regFind: "\\{Img:\\s*(.*)\\}"
replace: "<img src=\"$1\" style=\"max-width: 100%;\" />"
Unique - name 5: # Use of anonymous function
# $found is result of preg_match_all() with SET ORDER (no OFFSET CAPTURE)
-
regFind: "\\{Url:\\s*([^\\s]*)\\s*(.*)\\}"
do: |
function( $text, $found, $userdata = [] ) {
// do something with $found
// replace in $text
foreach( $found as $f )
{
$fullStr = $f[0];
$label = $f[1];
$url = $f[2];
$s = str_replace( "[URL]", "http://" . $url, "<a href=\"[URL]\">[LABEL]</a>" );
$s = str_replace( "[LABEL]", $label, $s );
$text = str_replace( $fullStr, $s, $text );
}
return $text;
}
Just include PHP code inside normal PHP tags
# My markdown
<?= 'Hi' ?>
Some content
The code will be executed if you call textPHP($file, $args = [])
instead of text($text)
. Use $args for vars.
All style samples see above.
$md = new ParsedownUserStyles();
// use parsedown's methods as needed
$md->addStyle( 'Unique - name', [
[
'find' => '[[',
'replace' => '<span class="header">'
],
[
'find' => ']]',
'replace' => '</span>'
]
]);
$mdString = $md->text( ... );
Copyright (C) Walter A. Jablonowski 2020, MIT License
Licenses of third party software and stuff see credits.