undefinedoffset/silverstripe-markdown

Adds a field and a data type that allows for Markdown editing, uses the github api to render the html

Installs: 2 708

Dependents: 1

Suggesters: 0

Security: 0

Stars: 13

Watchers: 1

Forks: 6

Type:silverstripe-vendormodule

2.0.1 2019-05-31 16:17 UTC

This package is auto-updated.

Last update: 2020-08-30 01:12:14 UTC


README

Adds a field and a data type that allows for Markdown editing, uses a supported renderer (default is the GitHub API) to render the HTML.

Requirements

  • SilverStripe 4.x
  • PHP cURL Support

Installation

  • Install with composer: composer require undefinedoffset/silverstripe-markdown ^2.0
  • Run dev/build?flush=all to regenerate the manifest

Usage

Use the Markdown data type as your fields data type, then use the MarkdownEditor field in the cms for editing.

Page class

use UndefinedOffset\Markdown\Forms\MarkdownEditor;

class MyPage extends Page
{
    public static $db = array(
        'MarkdownContent' => 'Markdown'
    );
    
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        
        $editor = new MarkdownEditor('MarkdownContent', 'Page Content (Markdown)');
        $editor->setRows(15); //optional, set number of rows in CMS
        $editor->setWrapMode(true); //optional, turn on word wrapping
        $fields->addFieldToTab('Root.Main', $editor);
        
        return $fields;
    }
}

Template

<div class="content">
    $MarkdownContent  <!-- Will show as rendered HTML -->
</div>

You may also request the markdown using Github Flavored Markdown by calling $YourField.AsHTML(true) in your template by default Github Flavored Markdown is not used just regular Markdown is used.

<div class="content">
    $MarkdownContent.AsHTML(true)  <!-- Will render the content using Github Flavoured Markdown -->
</div>

Configuration

The default renderer is the GitHub renderer. However, other renderers are supported.

To set what renderer to use, in _config.php do the following:

use UndefinedOffset\Markdown\Model\FieldTypes\Markdown;

// Fully qualified (namespaced) class name of any implementation of IMarkdownRenderer will work:
Markdown::setRenderer('UndefinedOffset\\Markdown\\Renderer\\GithubMarkdownRenderer'); 

GithubMarkdownRenderer

The following options are available on the default GithubMarkdownRenderer:

use UndefinedOffset\Markdown\Renderer\GitHubMarkdownRenderer;

// authenticate to the Github API to get 5,000 requests per hour instead of 60
GithubMarkdownRenderer::useBasicAuth('github username', 'github password'); 
// whether or not to use Github Flavoured Markdown
GithubMarkdownRenderer::setUseGFM(true);

PHPMarkdownMarkdownRenderer

PHPMarkdownMarkdownRenderer is simple and has no options. Use this to avoid the delay on page load the first time after editing that comes from using the Github renderer (especially if the page has many sections of markdown). You will need to install PHP Markdown for this to work - it can be installed with composer.

Note: This renderer does not support Github Flavoured Markdown.

use UndefinedOffset\Markdown\Model\FieldTypes\Markdown;

Markdown::setRenderer('UndefinedOffset\\Markdown\\Renderer\\PHPMarkdownMarkdownRenderer');