Merge fix #545, fix invalid HTML when linking to URLs with numeric fragment identifiers and update Parsedown

This commit is contained in:
Frederic Guillot 2015-01-25 12:23:27 -05:00
parent e506648cbc
commit 0812ceedde
5 changed files with 62 additions and 27 deletions

View File

@ -3,7 +3,6 @@
namespace Core; namespace Core;
use Pimple\Container; use Pimple\Container;
use Parsedown;
/** /**
* Template helpers * Template helpers
@ -474,24 +473,9 @@ class Helper
*/ */
public function markdown($text, array $link = array()) public function markdown($text, array $link = array())
{ {
$html = Parsedown::instance() $parser = new Markdown($link, $this);
->setMarkupEscaped(true) # escapes markup (HTML) $parser->setMarkupEscaped(true);
->text($text); return $parser->text($text);
// Replace task #123 by a link to the task
if (! empty($link) && preg_match_all('!#(\d+)!i', $html, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$html = str_replace(
$match[0],
$this->a($match[0], $link['controller'], $link['action'], $link['params'] + array('task_id' => $match[1])),
$html
);
}
}
return $html;
} }
/** /**

43
app/Core/Markdown.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace Core;
use Parsedown;
/**
* Specific Markdown rules for Kanboard
*
* @package core
* @author norcnorc
* @author Frederic Guillot
*/
class Markdown extends Parsedown
{
private $link;
private $helper;
public function __construct($link, Helper $helper)
{
$this->link = $link;
$this->helper = $helper;
$this->InlineTypes['#'][] = 'TaskLink';
$this->inlineMarkerList .= '#';
}
protected function inlineTaskLink($Excerpt)
{
// Replace task #123 by a link to the task
if (! empty($this->link) && preg_match('!#(\d+)!i', $Excerpt['text'], $matches)) {
$url = $this->helper->u($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)));
}
}
}

View File

@ -5,7 +5,7 @@
"swiftmailer/swiftmailer": "@stable", "swiftmailer/swiftmailer": "@stable",
"fguillot/json-rpc": "0.0.1", "fguillot/json-rpc": "0.0.1",
"fguillot/picodb": "0.0.2", "fguillot/picodb": "0.0.2",
"erusev/parsedown": "1.1.1", "erusev/parsedown": "1.5.1",
"lusitanian/oauth": "0.3.5", "lusitanian/oauth": "0.3.5",
"pimple/pimple": "~3.0", "pimple/pimple": "~3.0",
"symfony/console": "@stable", "symfony/console": "@stable",

12
composer.lock generated
View File

@ -4,20 +4,20 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "a5f10d6c565a2a7b5b63650d550cabf2", "hash": "671bd4694072aed17a542db8f08db217",
"packages": [ "packages": [
{ {
"name": "erusev/parsedown", "name": "erusev/parsedown",
"version": "1.1.1", "version": "1.5.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/erusev/parsedown.git", "url": "https://github.com/erusev/parsedown.git",
"reference": "da5d75e97e1ed19e57bd54fa6cb595a6a0879a67" "reference": "9da19c1108c39df9b42adc42e39b8371070652d0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/erusev/parsedown/zipball/da5d75e97e1ed19e57bd54fa6cb595a6a0879a67", "url": "https://api.github.com/repos/erusev/parsedown/zipball/9da19c1108c39df9b42adc42e39b8371070652d0",
"reference": "da5d75e97e1ed19e57bd54fa6cb595a6a0879a67", "reference": "9da19c1108c39df9b42adc42e39b8371070652d0",
"shasum": "" "shasum": ""
}, },
"type": "library", "type": "library",
@ -43,7 +43,7 @@
"markdown", "markdown",
"parser" "parser"
], ],
"time": "2014-10-29 20:29:46" "time": "2015-01-24 13:01:47"
}, },
{ {
"name": "fguillot/json-rpc", "name": "fguillot/json-rpc",

View File

@ -18,8 +18,16 @@ class HelperTest extends Base
); );
$this->assertEquals( $this->assertEquals(
'<p>Task <a href="?controller=a&amp;action=b&amp;c=d&amp;task_id=123" class="" title="" >#123</a></p>', '<p>Task <a href="?controller=a&amp;action=b&amp;c=d&amp;task_id=123">#123</a></p>',
$h->markdown('Task #123', array('controller' => 'a', 'action' => 'b', 'params' => array('c' => 'd'))) $h->markdown('Task #123', array('controller' => 'a', 'action' => 'b', 'params' => array('c' => 'd')))
); );
$this->assertEquals(
'<p>Check that: <a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454</a></p>',
$h->markdown(
'Check that: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454',
array('controller' => 'a', 'action' => 'b', 'params' => array('c' => 'd'))
)
);
} }
} }