From b3c101cd10e08156d209beb175248aad5aae502a Mon Sep 17 00:00:00 2001 From: maxpozdeev Date: Sun, 24 Jul 2022 12:08:31 +0300 Subject: [PATCH] use markdown class and interface for easy connection of other md libs --- .../MTTParsedown.php => markup.parsedown.php} | 25 +++++++++++++-- src/includes/markup.php | 32 ++++++++++++++----- 2 files changed, 47 insertions(+), 10 deletions(-) rename src/includes/{parsedown/MTTParsedown.php => markup.parsedown.php} (69%) diff --git a/src/includes/parsedown/MTTParsedown.php b/src/includes/markup.parsedown.php similarity index 69% rename from src/includes/parsedown/MTTParsedown.php rename to src/includes/markup.parsedown.php index 40aa03e..f863259 100644 --- a/src/includes/parsedown/MTTParsedown.php +++ b/src/includes/markup.parsedown.php @@ -6,6 +6,28 @@ Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ +require_once(MTTINC. 'parsedown/Parsedown.php'); + +class MTTParsedownWrapper implements MTTMarkdownInterface +{ + /** @var MTTParsedown */ + protected $converter; + + function __construct() + { + $this->converter = new MTTParsedown(); + $this->converter->setSafeMode(true); + //$this->converter->setBreaksEnabled(true); + } + + public function convert(string $s, bool $toExternal = false) + { + $this->converter->setToExternal($toExternal); + return $this->converter->text($s); + } +} + + class MTTParsedown extends Parsedown { @@ -34,7 +56,7 @@ class MTTParsedown extends Parsedown ); if (!$this->toExternal) { $attrs['class'] = 'mtt-link-to-task'; - $attrs['target-id'] = $matches[1]; + $attrs['data-target-id'] = $matches[1]; } return array( @@ -50,5 +72,4 @@ class MTTParsedown extends Parsedown ); } } - } diff --git a/src/includes/markup.php b/src/includes/markup.php index 3f1c32e..016c69f 100644 --- a/src/includes/markup.php +++ b/src/includes/markup.php @@ -6,9 +6,30 @@ Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ -require_once(MTTINC. 'parsedown/Parsedown.php'); -require_once(MTTINC. 'parsedown/MTTParsedown.php'); +require_once(MTTINC. 'markup.parsedown.php'); +interface MTTMarkdownInterface +{ + public function convert(string $s, bool $toExternal = false); +} + +final class MTTMarkdown +{ + /** @var MTTMarkdownInterface */ + private static $instance; + + /** + * + * @return MTTMarkdownInterface + */ + public static function instance() : MTTMarkdownInterface + { + if (!isset(self::$instance)) { + self::$instance = new MTTParsedownWrapper(); + } + return self::$instance; + } +} function noteMarkup($note, $toExternal = false) { @@ -21,14 +42,9 @@ function noteMarkup($note, $toExternal = false) return markdownToHtml($note, $toExternal); } -// Markdown converter (Parsedown) function markdownToHtml($s, $toExternal = false) { - $parser = MTTParsedown::instance(); - $parser->setToExternal($toExternal); - $parser->setSafeMode(true); - //$parser->setBreaksEnabled(true); - return $parser->text($s); + return MTTMarkdown::instance()->convert($s, $toExternal); }