summaryrefslogtreecommitdiff
path: root/app/Core/Markdown.php
blob: 827fd0dfc3fa40e7148672dd02855792b1fc80c2 (plain)
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
<?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
{
    /**
     * Link params for tasks
     *
     * @access private
     * @var array
     */
    private $link = array();

    /**
     * Container
     *
     * @access private
     * @var Container
     */
    private $container;

    /**
     * Constructor
     *
     * @access public
     * @param  Container  $container
     * @param  array      $link
     */
    public function __construct(Container $container, array $link)
    {
        $this->link = $link;
        $this->container = $container;
        $this->InlineTypes['#'][] = 'TaskLink';
        $this->InlineTypes['@'][] = 'UserLink';
        $this->inlineMarkerList .= '#@';
    }

    /**
     * Handle Task Links
     *
     * Replace "#123" by a link to the task
     *
     * @access public
     * @param  array  $Excerpt
     * @return array
     */
    protected function inlineTaskLink(array $Excerpt)
    {
        if (! empty($this->link) && preg_match('!#(\d+)!i', $Excerpt['text'], $matches)) {
            $url = $this->container['helper']->url->href(
                $this->link['controller'],
                $this->link['action'],
                $this->link['params'] + array('task_id' => $matches[1])
            );

            return array(
                'extent' => strlen($matches[0]),
                'element' => array(
                    'name' => 'a',
                    'text' => $matches[0],
                    'attributes' => array('href' => $url)
                ),
            );
        }
    }

    /**
     * Handle User Mentions
     *
     * Replace "@username" by a link to the user
     *
     * @access public
     * @param  array  $Excerpt
     * @return array
     */
    protected function inlineUserLink(array $Excerpt)
    {
        if (preg_match('/^@([^\s]+)/', $Excerpt['text'], $matches)) {
            $user_id = $this->container['user']->getIdByUsername($matches[1]);

            if (! empty($user_id)) {
                $url = $this->container['helper']->url->href('user', 'profile', array('user_id' => $user_id));

                return array(
                    'extent' => strlen($matches[0]),
                    'element' => array(
                        'name' => 'a',
                        'text' => $matches[0],
                        'attributes' => array('href' => $url, 'class' => 'user-mention-link'),
                    ),
                );
            }
        }
    }
}