mirror of
https://github.com/flextype/flextype.git
synced 2025-08-11 07:34:22 +02:00
feat(parsers): update codebase for shortcodes and markdown #562
This commit is contained in:
135
src/flextype/Parsers/Markdown.php
Normal file
135
src/flextype/Parsers/Markdown.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype\Parsers;
|
||||
|
||||
use Exception;
|
||||
use League\CommonMark\MarkdownConverter;
|
||||
use League\CommonMark\Environment\Environment;
|
||||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||||
use League\CommonMark\Extension\Attributes\AttributesExtension;
|
||||
use League\CommonMark\Extension\Table\TableExtension;
|
||||
|
||||
use function flextype;
|
||||
use function strings;
|
||||
|
||||
final class Markdown
|
||||
{
|
||||
/**
|
||||
* Markdown instance.
|
||||
*/
|
||||
private static ?Markdown $instance = null;
|
||||
|
||||
/**
|
||||
* Markdown Environment
|
||||
*/
|
||||
private $environment = null;
|
||||
|
||||
/**
|
||||
* Markdown Converter
|
||||
*/
|
||||
private $converter = null;
|
||||
|
||||
/**
|
||||
* Markdown should not be cloneable.
|
||||
*/
|
||||
protected function __clone()
|
||||
{
|
||||
throw new Exception('Cannot clone a Markdown.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Markdown should not be restorable from strings.
|
||||
*/
|
||||
public function __wakeup(): void
|
||||
{
|
||||
throw new Exception('Cannot unserialize a Markdown.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Markdown construct
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$this->environment = new Environment(registry()->get('flextype.settings.parsers.markdown.commonmark'));
|
||||
$this->environment->addExtension(new CommonMarkCoreExtension());
|
||||
$this->environment->addExtension(new AttributesExtension());
|
||||
$this->environment->addExtension(new TableExtension());
|
||||
$this->converter = new MarkdownConverter($this->environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Markdown Environment
|
||||
*/
|
||||
public function environment(): Environment
|
||||
{
|
||||
return $this->environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Markdown Converter
|
||||
*/
|
||||
public function converter(): MarkdownConverter
|
||||
{
|
||||
return $this->converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the instance via lazy initialization (created on first usage).
|
||||
*/
|
||||
public static function getInstance(): Markdown
|
||||
{
|
||||
if (static::$instance === null) {
|
||||
static::$instance = new self();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a MARKDOWN encoded string and converts it into a PHP variable.
|
||||
*
|
||||
* @param string $input A string containing MARKDOWN
|
||||
*
|
||||
* @return mixed The MARKDOWN converted to a PHP value
|
||||
*/
|
||||
public function parse(string $input)
|
||||
{
|
||||
$cache = registry()->get('flextype.settings.parsers.markdown.cache');
|
||||
|
||||
if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($dataFromCache = cache()->get($key)) {
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $this->converter()->convertToHtml($input)->getContent();
|
||||
cache()->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $this->converter()->convertToHtml($input)->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Cache ID for markdown.
|
||||
*
|
||||
* @param string $input Input.
|
||||
*
|
||||
* @return string Cache ID.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getCacheID(string $input): string
|
||||
{
|
||||
return strings('markdown' . $input)->hash()->toString();
|
||||
}
|
||||
}
|
@@ -11,6 +11,7 @@ namespace Flextype\Parsers;
|
||||
|
||||
use Atomastic\Macroable\Macroable;
|
||||
use Flextype\Parsers\Shortcodes;
|
||||
use Flextype\Parsers\Markdown;
|
||||
|
||||
class Parsers
|
||||
{
|
||||
@@ -23,4 +24,12 @@ class Parsers
|
||||
{
|
||||
return Shortcodes::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Markdown instance.
|
||||
*/
|
||||
public function markdown(): Markdown
|
||||
{
|
||||
return Markdown::getInstance();
|
||||
}
|
||||
}
|
||||
|
@@ -146,8 +146,10 @@ final class Shortcodes
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function parse(string $input, bool $cache = true)
|
||||
public function parse(string $input)
|
||||
{
|
||||
$cache = registry()->get('flextype.settings.parsers.shortcodes.cache');
|
||||
|
||||
if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
|
@@ -12,7 +12,7 @@ namespace Flextype\Parsers\Shortcodes;
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
// Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"]
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.entries.enabled')) {
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.entries.enabled')) {
|
||||
parsers()->shortcodes()->addHandler('entries_fetch', static function (ShortcodeInterface $s) {
|
||||
return arrays(entries()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
|
||||
});
|
||||
|
@@ -12,7 +12,7 @@ namespace Flextype\Parsers\Shortcodes;
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
// Shortcode: [media_files_fetch id="media-id" field="field-name" default="default-value"]
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.media.enabled')) {
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.media.enabled')) {
|
||||
parsers()->shortcodes()->addHandler('media_files_fetch', static function (ShortcodeInterface $s) {
|
||||
return arrays(flextype('media')->files()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
|
||||
});
|
||||
|
@@ -14,7 +14,7 @@ use Thunder\Shortcode\Events;
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
// Shortcode: [raw]
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.raw.enabled')) {
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.raw.enabled')) {
|
||||
parsers()->shortcodes()->addHandler('raw', static function (ShortcodeInterface $s) {
|
||||
return $s->getContent();
|
||||
});
|
||||
|
@@ -12,7 +12,7 @@ namespace Flextype\Parsers\Shortcodes;
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
// Shortcode: [registry_get name="item-name" default="default-value"]
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.registry.enabled')) {
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.registry.enabled')) {
|
||||
parsers()->shortcodes()->addHandler('registry_get', static function (ShortcodeInterface $s) {
|
||||
return registry()->get($s->getParameter('name'), $s->getParameter('default'));
|
||||
});
|
||||
|
@@ -13,7 +13,7 @@ use Slim\Http\Environment;
|
||||
use Slim\Http\Uri;
|
||||
|
||||
// Shortcode: [url]
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.url.enabled')) {
|
||||
if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) {
|
||||
parsers()->shortcodes()->addHandler('url', static function () {
|
||||
if (registry()->has('flextype.settings.url') && registry()->get('flextype.settings.url') !== '') {
|
||||
return registry()->get('flextype.settings.url');
|
||||
|
@@ -417,26 +417,43 @@ serializers:
|
||||
|
||||
# Parsers
|
||||
#
|
||||
# Shortcode
|
||||
#
|
||||
# - shortcodes: Flextype Shortcodes to load.
|
||||
parsers:
|
||||
markdown:
|
||||
cache: true
|
||||
commonmark:
|
||||
renderer:
|
||||
block_separator: "\n"
|
||||
inner_separator: "\n"
|
||||
soft_break: "\n"
|
||||
commonmark:
|
||||
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: 9223372036854775807
|
||||
slug_normalizer:
|
||||
max_length: 255
|
||||
shortcodes:
|
||||
media:
|
||||
path: "/src/flextype/Parsers/Shortcodes/MediaShortcode.php"
|
||||
enabled: true
|
||||
entries:
|
||||
path: "/src/flextype/Parsers/Shortcodes/EntriesShortcode.php"
|
||||
enabled: true
|
||||
raw:
|
||||
path: "/src/flextype/Parsers/Shortcodes/RawShortcode.php"
|
||||
enabled: true
|
||||
registry:
|
||||
path: "/src/flextype/Parsers/Shortcodes/RegistryShortcode.php"
|
||||
enabled: true
|
||||
url:
|
||||
path: "/src/flextype/Parsers/Shortcodes/UrlShortcode.php"
|
||||
enabled: true
|
||||
cache: true
|
||||
shortcodes:
|
||||
media:
|
||||
path: "/src/flextype/Parsers/Shortcodes/MediaShortcode.php"
|
||||
enabled: true
|
||||
entries:
|
||||
path: "/src/flextype/Parsers/Shortcodes/EntriesShortcode.php"
|
||||
enabled: true
|
||||
raw:
|
||||
path: "/src/flextype/Parsers/Shortcodes/RawShortcode.php"
|
||||
enabled: true
|
||||
registry:
|
||||
path: "/src/flextype/Parsers/Shortcodes/RegistryShortcode.php"
|
||||
enabled: true
|
||||
url:
|
||||
path: "/src/flextype/Parsers/Shortcodes/UrlShortcode.php"
|
||||
enabled: true
|
||||
|
||||
# CORS
|
||||
#
|
||||
|
16
tests/src/flextype/Parsers/MarkdownTest.php
Normal file
16
tests/src/flextype/Parsers/MarkdownTest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
test('test getInstance() method', function () {
|
||||
$this->assertInstanceOf(Flextype\Parsers\Markdown::class, parsers()->markdown()->getInstance());
|
||||
});
|
||||
|
||||
test('test parse() method', function () {
|
||||
$this->assertEquals('<p><strong>Bold</strong></p>', trim(parsers()->markdown()->parse('**Bold**')));
|
||||
});
|
||||
|
||||
test('test getCacheID() method', function () {
|
||||
$this->assertNotEquals(parsers()->markdown()->getCacheID('**Bold**'),
|
||||
parsers()->markdown()->getCacheID('**Bold Text**'));
|
||||
});
|
Reference in New Issue
Block a user