1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-11 15:44:49 +02:00

feat(parsers): add commonmark instead of parsedown #540

This commit is contained in:
Awilum
2021-01-12 14:41:26 +03:00
parent 66ab7a142d
commit 929a787d23
3 changed files with 44 additions and 22 deletions

View File

@@ -9,8 +9,11 @@ declare(strict_types=1);
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;
@@ -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.
@@ -49,25 +57,31 @@ final class Markdown
/**
* 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 facade(): ParsedownExtra
public function environment(): Environment
{
return $this->markdownFacade;
return $this->environment;
}
/**
* Markdown Converter
*/
public function converter(): CommonMarkConverter
{
return $this->converter;
}
/**
@@ -102,13 +116,13 @@ final class Markdown
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);
}
/**

View File

@@ -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: