Skip to content

Commit

Permalink
feat(parsers): add commonmark instead of parsedown #540
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jan 12, 2021
1 parent 66ab7a1 commit 929a787
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@

"bnf/slim3-psr15": "^1.1.1",

"erusev/parsedown": "^1.7.3",
"erusev/parsedown-extra": "^0.7.1",
"league/commonmark": "^1.5.7",

"thunderer/shortcode": "^0.7.4",

Expand Down
48 changes: 31 additions & 17 deletions src/flextype/Support/Parsers/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

namespace Flextype\Support\Parsers;

use Exception;
use ParsedownExtra;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;

use function flextype;
use function strings;
Expand All @@ -27,9 +30,14 @@ final class Markdown
private static $instances = [];

/**
* Markdown facade
* Markdown Environment
*/
private $markdownFacade = null;
private $environment = null;

/**
* Markdown Converter
*/
private $converter = null;

/**
* Markdown should not be cloneable.
Expand All @@ -49,25 +57,31 @@ public function __wakeup(): void

/**
* Markdown construct
*
* @param
*/
protected function __construct()
{
$this->markdownFacade = new ParsedownExtra();
$this->markdownFacade->setBreaksEnabled(flextype('registry')->get('flextype.settings.parsers.markdown.auto_line_breaks'));
$this->markdownFacade->setUrlsLinked(flextype('registry')->get('flextype.settings.parsers.markdown.auto_url_links'));
$this->markdownFacade->setMarkupEscaped(flextype('registry')->get('flextype.settings.parsers.markdown.escape_markup'));
$config = flextype('registry')->get('flextype.settings.parsers.markdown');
$this->environment = Environment::createCommonMarkEnvironment();
$this->environment->addExtension(new AttributesExtension());
$this->environment->addExtension(new TableExtension());
$this->environment->addExtension(new StrikethroughExtension());
$this->converter = new CommonMarkConverter($config, $this->environment);
}

/**
* Markdown facade
*
* @param
* Markdown Environment
*/
public function environment(): Environment
{
return $this->environment;
}

/**
* Markdown Converter
*/
public function facade(): ParsedownExtra
public function converter(): CommonMarkConverter
{
return $this->markdownFacade;
return $this->converter;
}

/**
Expand Down Expand Up @@ -102,13 +116,13 @@ public function parse(string $input, bool $cache = true): string
return $dataFromCache;
}

$data = $this->facade()->text($input);
$data = $this->converter()->convertToHtml($input);
flextype('cache')->set($key, $data);

return $data;
}

return $this->facade()->text($input);
return $this->converter()->convertToHtml($input);
}

/**
Expand Down
15 changes: 12 additions & 3 deletions src/flextype/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,18 @@ image:
# - shortcodes: Flextype Shortcodes to load.
parsers:
markdown:
auto_line_breaks: false
auto_url_links: false
escape_markup: false
renderer:
block_separator: "\n"
inner_separator: "\n"
soft_break: "\n"
enable_em: true
enable_strong: true
use_asterisk: true
use_underscore: true
unordered_list_markers: ['-', '*', '+']
html_input: 'allow'
allow_unsafe_links: false
max_nesting_level: INF
shortcode:
shortcodes:
entries:
Expand Down

0 comments on commit 929a787

Please sign in to comment.