allow markdown in timesheet descriptions (#296)
This commit is contained in:
@@ -75,6 +75,7 @@ class AppExtension extends Extension implements PrependExtensionInterface
|
||||
$container->setParameter('kimai.timesheet.rates', $config['timesheet']['rates']);
|
||||
$container->setParameter('kimai.timesheet.rounding', $config['timesheet']['rounding']);
|
||||
$container->setParameter('kimai.timesheet.duration_only', $config['timesheet']['duration_only']);
|
||||
$container->setParameter('kimai.timesheet.markdown', $config['timesheet']['markdown_content']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -55,6 +55,9 @@ class Configuration implements ConfigurationInterface
|
||||
->booleanNode('duration_only')
|
||||
->defaultValue(false)
|
||||
->end()
|
||||
->booleanNode('markdown_content')
|
||||
->defaultValue(false)
|
||||
->end()
|
||||
->arrayNode('rounding')
|
||||
->requiresAtLeastOneElement()
|
||||
->useAttributeAsKey('key')
|
||||
|
||||
@@ -21,14 +21,19 @@ class MarkdownExtension extends \Twig_Extension
|
||||
* @var Markdown
|
||||
*/
|
||||
private $markdown;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $timesheetIsMarkdown = false;
|
||||
|
||||
/**
|
||||
* MarkdownExtension constructor.
|
||||
* @param Markdown $parser
|
||||
*/
|
||||
public function __construct(Markdown $parser)
|
||||
public function __construct(Markdown $parser, bool $timesheetAsMarkdown = false)
|
||||
{
|
||||
$this->markdown = $parser;
|
||||
$this->timesheetIsMarkdown = $timesheetAsMarkdown;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,9 +43,25 @@ class MarkdownExtension extends \Twig_Extension
|
||||
{
|
||||
return [
|
||||
new TwigFilter('md2html', [$this, 'markdownToHtml'], ['is_safe' => ['html']]),
|
||||
new TwigFilter('desc2html', [$this, 'timesheetContent'], ['is_safe' => ['html']]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the timesheet description content into HTML.
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
public function timesheetContent(string $content): string
|
||||
{
|
||||
if ($this->timesheetIsMarkdown) {
|
||||
return $this->markdown->toHtml($content, false);
|
||||
}
|
||||
|
||||
return nl2br($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the given Markdown content into HTML
|
||||
*
|
||||
@@ -49,6 +70,6 @@ class MarkdownExtension extends \Twig_Extension
|
||||
*/
|
||||
public function markdownToHtml(string $content): string
|
||||
{
|
||||
return $this->markdown->toHtml($content);
|
||||
return $this->markdown->toHtml($content, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,12 @@ class Markdown
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @param bool $safe
|
||||
* @return string
|
||||
*/
|
||||
public function toHtml(string $text): string
|
||||
public function toHtml(string $text, bool $safe = true): string
|
||||
{
|
||||
$this->parser->setSafeMode($safe);
|
||||
return $this->parser->text($text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user