use markdown class and interface for easy connection of other md libs

This commit is contained in:
maxpozdeev 2022-07-24 12:08:31 +03:00
parent aab520b0d4
commit b3c101cd10
2 changed files with 47 additions and 10 deletions

View file

@ -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
);
}
}
}

View file

@ -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);
}