PHP code example of raffaelj / cockpit-bettermarkdown

1. Go to this page and download the library: Download raffaelj/cockpit-bettermarkdown 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/ */

    

raffaelj / cockpit-bettermarkdown example snippets



return [
    'app.name' => 'markdown test',

    'bettermarkdown' => [
        'parser' => 'extended', // (string) parsedown|extra|extended - default: extended
        'cache' => false, // cache is still active, but rebuild is forced --> useful for debugging

        // change settings for ParsedownToC
        // see: https://github.com/BenjaminHoegh/parsedownToc#configuration
        'toc' => [ 
            'selectors' => ['h2', 'h3', 'h4', 'h5', 'h6'], // omit h1 from toc

            // blacklist existing ids in your frontend
            'blacklist' => [
                'nav', // turns to 'nav-1'
                'top',
            ],

            // array of regexes for text replacements - before heading ids are generated
            'replacements' => [ 
                '/^old-id$/' => 'new-id',
            ],
        ],

        'cached_toc_format' => 'tree', // (string) flat|tree - default: flat

        // transform the cached toc in tree format with cleaner output
        'tree_toc' => [
            'replace_keys' => [
                'text' => 'title',
            ],
            'unset_keys' => ['level', 'id'],
        ],

        // add classes to task lists
        'tasks' => [
            'classUnchecked' => 'parsedown-task-list parsedown-task-list-open',
            'classChecked'   => 'parsedown-task-list parsedown-task-list-close',
        ],
    ],
];

$html = $app->module('cockpit')->markdown($md);

$html = $app->module('cockpit')->markdown($md);

$hash = md5($md);

$cachepath = "tmp:///{$hash}.md.toc.json";

if ($app->filestorage->has($cachepath)) {
    $toc = \json_decode($app->filestorage->read($cachepath), true);
}

print_r($toc);