nsrosenqvist/cmb2-wiki

This package is abandoned and no longer maintained. No replacement package was suggested.

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:wordpress-plugin

1.1.1 2018-06-30 13:12 UTC

This package is auto-updated.

Last update: 2020-06-09 20:10:16 UTC


README

Integrates a simple but powerful CMB2 field that is not meant to save user input but to display a wiki. Great to use for the landing page that explains the theme when using CMB2 to create a page for theme options.

$docs = new_cmb2_box(array(
    'id'            => 'docs',
    'title'         => __('Documentation', 'theme'),
));

$wiki = $docs->add_field(array(
    'name' => __('Wiki', 'theme'),
    'id'   => 'wiki',
    'type' => 'wiki',
    'meta' => true, // Display file path and modification date
    'wiki_root' => __DIR__.'/wiki',
    'theme_root' => __DIR__,
    'pre_process' => true, // Enables running PHP code in the file before displaying it
    'files' => [
        __DIR__.'/wiki/Introduction.md',
    ],
));

Add a markdown renderer:

use League\CommonMark\Converter;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;

use Webuni\CommonMark\TableExtension\TableExtension;
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Inline\Element\Image;

// Prebuilt classes that fixes relative links within the files
// to be properly linked when displayed in the admin UI
use NSRosenqvist\CMB2\WikiField\Markdown\LinkRenderer;
use NSRosenqvist\CMB2\WikiField\Markdown\ImageRenderer;

// Configure Markdown renderer
add_filter('cmb2_wiki_file_content', function($content, $file, $root) { // cmb2_{field_id}_file_content
    $environment = Environment::createCommonMarkEnvironment();
    $environment->addExtension(new TableExtension());
    $environment->addInlineRenderer(Link::class, new LinkRenderer($file, $root));
    $environment->addInlineRenderer(Image::class, new ImageRenderer($file, $root));

    $converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
    return $converter->convertToHtml($content);
}, 10, 3);

// Configure markdown file title
add_filter('cmb2_wiki_file_title', function($name, $file) { // cmb2_{field_id}_file_title
    return basename($file, '.md');
}, 10, 2);