File size: 4,979 Bytes
e4f4821 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
<?php
namespace Kanboard\Core;
use Parsedown;
use Pimple\Container;
/**
* Specific Markdown rules for Kanboard
*
* @package core
* @author norcnorc
* @author Frederic Guillot
*/
class Markdown extends Parsedown
{
/**
* Task links generated will use the project token instead
*
* @access private
* @var boolean
*/
private $isPublicLink = false;
/**
* Container
*
* @access private
* @var Container
*/
private $container;
/**
* Constructor
*
* @access public
* @param Container $container
* @param boolean $isPublicLink
*/
public function __construct(Container $container, $isPublicLink)
{
$this->isPublicLink = $isPublicLink;
$this->container = $container;
$this->BlockTypes['#'][0] = 'CustomHeader';
$this->InlineTypes['#'][] = 'TaskLink';
$this->InlineTypes['@'][] = 'UserLink';
$this->inlineMarkerList .= '#@';
}
protected function blockCustomHeader($Line)
{
if (preg_match('!#(\d+)!i', $Line['text'], $matches))
{
$link = $this->buildTaskLink($matches[1]);
if (! empty($link)) {
return [
'extent' => strlen($matches[0]),
'element' => [
'name' => 'a',
'text' => $matches[0],
'attributes' => ['href' => $link],
],
];
}
}
return $this->blockHeader($Line);
}
/**
* Handle Task Links
*
* Replace "#123" by a link to the task
*
* @access public
* @param array $Excerpt
* @return array|null
*/
protected function inlineTaskLink(array $Excerpt)
{
if (preg_match('!#(\d+)!i', $Excerpt['text'], $matches)) {
$link = $this->buildTaskLink($matches[1]);
if (! empty($link)) {
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'a',
'text' => $matches[0],
'attributes' => array('href' => $link),
),
);
}
}
return null;
}
/**
* Handle User Mentions
*
* Replace "@username" by a link to the user
*
* @access public
* @param array $Excerpt
* @return array|null
*/
protected function inlineUserLink(array $Excerpt)
{
if (! $this->isPublicLink && preg_match('/^@([^\s,!:?]+)/', $Excerpt['text'], $matches)) {
$username = rtrim($matches[1], '.');
$user = $this->container['userCacheDecorator']->getByUsername($username);
if (! empty($user)) {
$url = $this->container['helper']->url->to('UserViewController', 'profile', array('user_id' => $user['id']));
$name = $user['name'] ?: $user['username'];
return array(
'extent' => strlen($username) + 1,
'element' => array(
'name' => 'a',
'text' => '@' . $username,
'attributes' => array(
'href' => $url,
'class' => 'user-mention-link',
'title' => $name,
'aria-label' => $name,
),
),
);
}
}
return null;
}
/**
* Build task link
*
* @access private
* @param integer $task_id
* @return string
*/
private function buildTaskLink($task_id)
{
if ($this->isPublicLink) {
$token = $this->container['memoryCache']->proxy($this->container['taskFinderModel'], 'getProjectToken', $task_id);
if (! empty($token)) {
return $this->container['helper']->url->to(
'TaskViewController',
'readonly',
array(
'token' => $token,
'task_id' => $task_id,
),
'',
true
);
}
return '';
}
return $this->container['helper']->url->to(
'TaskViewController',
'show',
array('task_id' => $task_id)
);
}
/**
* Exclude from nesting task links and user mentions for links
*
* @param array $Excerpt
* @return array|null
*/
protected function inlineLink($Excerpt)
{
$Inline = parent::inlineLink($Excerpt);
if (is_array($Inline)) {
array_push($Inline['element']['nonNestables'], 'TaskLink', 'UserLink');
}
return $Inline;
}
}
|