Add support for task links in Markdown headings

If a text block matches #(\d+) it will be interpreted as a task link instead
of a heading.

Closes #5017
This commit is contained in:
Frédéric Guillot 2023-04-18 20:52:47 -07:00 committed by Frédéric Guillot
parent e06c6656bc
commit d3f38d1bf2
2 changed files with 32 additions and 0 deletions

View File

@ -41,11 +41,33 @@ class Markdown extends Parsedown
{ {
$this->isPublicLink = $isPublicLink; $this->isPublicLink = $isPublicLink;
$this->container = $container; $this->container = $container;
$this->BlockTypes['#'][0] = 'CustomHeader';
$this->InlineTypes['#'][] = 'TaskLink'; $this->InlineTypes['#'][] = 'TaskLink';
$this->InlineTypes['@'][] = 'UserLink'; $this->InlineTypes['@'][] = 'UserLink';
$this->inlineMarkerList .= '#@'; $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 * Handle Task Links
* *

View File

@ -29,6 +29,16 @@ class TextHelperTest extends Base
$this->assertEquals('<p>Test</p>', $textHelper->markdown('Test')); $this->assertEquals('<p>Test</p>', $textHelper->markdown('Test'));
$this->assertEquals(
'<a href="?controller=TaskViewController&amp;action=show&amp;task_id=123">#123</a>',
$textHelper->markdown('#123')
);
$this->assertEquals(
"<h1>Heading 1</h1>\n<p>Task <a href=\"?controller=TaskViewController&amp;action=show&amp;task_id=123\">#123</a></p>\n<h2>Heading 2</h2>",
$textHelper->markdown("# Heading 1\r\n\r\nTask #123\r\n\r\n## Heading 2")
);
$this->assertEquals( $this->assertEquals(
'<p>Task <a href="?controller=TaskViewController&amp;action=show&amp;task_id=123">#123</a></p>', '<p>Task <a href="?controller=TaskViewController&amp;action=show&amp;task_id=123">#123</a></p>',
$textHelper->markdown('Task #123') $textHelper->markdown('Task #123')