2022-08-07 22:21:58 +00:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-14 19:13:04 +00:00
|
|
|
|
2022-01-14 19:45:30 +00:00
|
|
|
/*
|
2022-02-06 20:37:02 +00:00
|
|
|
This file is a part of myTinyTodo.
|
|
|
|
|
(C) Copyright 2022 Max Pozdeev <maxpozdeev@gmail.com>
|
|
|
|
|
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
|
2022-01-14 19:45:30 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-07-24 09:42:47 +00:00
|
|
|
# We do not use composer autoloader because only one class is declared in Parsedown.
|
|
|
|
|
require_once(MTTINC. 'vendor/erusev/parsedown/Parsedown.php');
|
2022-07-24 09:08:31 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-14 19:13:04 +00:00
|
|
|
class MTTParsedown extends Parsedown
|
|
|
|
|
{
|
|
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
protected $toExternal;
|
2022-01-14 21:07:03 +00:00
|
|
|
|
2022-01-14 19:13:04 +00:00
|
|
|
function __construct()
|
|
|
|
|
{
|
2022-02-06 20:37:02 +00:00
|
|
|
$this->toExternal = false;
|
2022-01-14 19:13:04 +00:00
|
|
|
|
2022-01-14 21:07:03 +00:00
|
|
|
$this->InlineTypes['#'][]= 'TaskId';
|
2022-01-14 19:13:04 +00:00
|
|
|
$this->inlineMarkerList .= '#';
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
public function setToExternal(bool $v)
|
|
|
|
|
{
|
|
|
|
|
$this->toExternal = $v;
|
|
|
|
|
}
|
2022-01-14 21:07:03 +00:00
|
|
|
|
2022-01-14 19:13:04 +00:00
|
|
|
protected function inlineTaskId($excerpt)
|
|
|
|
|
{
|
|
|
|
|
if (preg_match('/^#(\d+)/', $excerpt['text'], $matches))
|
|
|
|
|
{
|
2022-02-06 20:37:02 +00:00
|
|
|
$attrs = array(
|
|
|
|
|
'href' => get_mttinfo('url'). '?task='. $matches[1],
|
|
|
|
|
'target' => '_blank',
|
|
|
|
|
);
|
|
|
|
|
if (!$this->toExternal) {
|
|
|
|
|
$attrs['class'] = 'mtt-link-to-task';
|
2022-07-24 09:08:31 +00:00
|
|
|
$attrs['data-target-id'] = $matches[1];
|
2022-02-06 20:37:02 +00:00
|
|
|
}
|
2022-01-14 19:13:04 +00:00
|
|
|
return array(
|
|
|
|
|
|
|
|
|
|
// How many characters to advance the Parsedown's
|
|
|
|
|
// cursor after being done processing this tag.
|
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
|
'element' => array(
|
|
|
|
|
'name' => 'a',
|
|
|
|
|
'text' => '#'. $matches[1],
|
2022-01-14 21:07:03 +00:00
|
|
|
'attributes' => $attrs,
|
2022-01-14 19:13:04 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|