diff --git a/src/flextype/Parsers/Markdown.php b/src/flextype/Parsers/Markdown.php new file mode 100644 index 00000000..2c713842 --- /dev/null +++ b/src/flextype/Parsers/Markdown.php @@ -0,0 +1,135 @@ +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(); + } +} \ No newline at end of file diff --git a/src/flextype/Parsers/Parsers.php b/src/flextype/Parsers/Parsers.php index 5a04adbc..64c7ced1 100644 --- a/src/flextype/Parsers/Parsers.php +++ b/src/flextype/Parsers/Parsers.php @@ -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(); + } } diff --git a/src/flextype/Parsers/Shortcodes.php b/src/flextype/Parsers/Shortcodes.php index 12c94c2b..05158fa1 100644 --- a/src/flextype/Parsers/Shortcodes.php +++ b/src/flextype/Parsers/Shortcodes.php @@ -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); diff --git a/src/flextype/Parsers/Shortcodes/EntriesShortcode.php b/src/flextype/Parsers/Shortcodes/EntriesShortcode.php index d755ee0b..02dcdd3e 100644 --- a/src/flextype/Parsers/Shortcodes/EntriesShortcode.php +++ b/src/flextype/Parsers/Shortcodes/EntriesShortcode.php @@ -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')); }); diff --git a/src/flextype/Parsers/Shortcodes/MediaShortcode.php b/src/flextype/Parsers/Shortcodes/MediaShortcode.php index 69038f68..26bbde31 100644 --- a/src/flextype/Parsers/Shortcodes/MediaShortcode.php +++ b/src/flextype/Parsers/Shortcodes/MediaShortcode.php @@ -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')); }); diff --git a/src/flextype/Parsers/Shortcodes/RawShortcode.php b/src/flextype/Parsers/Shortcodes/RawShortcode.php index c343c493..47c114ec 100644 --- a/src/flextype/Parsers/Shortcodes/RawShortcode.php +++ b/src/flextype/Parsers/Shortcodes/RawShortcode.php @@ -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(); }); diff --git a/src/flextype/Parsers/Shortcodes/RegistryShortcode.php b/src/flextype/Parsers/Shortcodes/RegistryShortcode.php index 1afe00f6..88d66861 100644 --- a/src/flextype/Parsers/Shortcodes/RegistryShortcode.php +++ b/src/flextype/Parsers/Shortcodes/RegistryShortcode.php @@ -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')); }); diff --git a/src/flextype/Parsers/Shortcodes/UrlShortcode.php b/src/flextype/Parsers/Shortcodes/UrlShortcode.php index 366696dc..f8341c3d 100644 --- a/src/flextype/Parsers/Shortcodes/UrlShortcode.php +++ b/src/flextype/Parsers/Shortcodes/UrlShortcode.php @@ -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'); diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index a465438f..8c86e946 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -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 # diff --git a/tests/src/flextype/Parsers/MarkdownTest.php b/tests/src/flextype/Parsers/MarkdownTest.php new file mode 100644 index 00000000..e79ece90 --- /dev/null +++ b/tests/src/flextype/Parsers/MarkdownTest.php @@ -0,0 +1,16 @@ +assertInstanceOf(Flextype\Parsers\Markdown::class, parsers()->markdown()->getInstance()); +}); + +test('test parse() method', function () { + $this->assertEquals('
Bold
', trim(parsers()->markdown()->parse('**Bold**'))); +}); + +test('test getCacheID() method', function () { + $this->assertNotEquals(parsers()->markdown()->getCacheID('**Bold**'), + parsers()->markdown()->getCacheID('**Bold Text**')); +});