From d3f38d1bf2f445d6cc0812907c05cf288f573bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 18 Apr 2023 20:52:47 -0700 Subject: [PATCH] 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 --- app/Core/Markdown.php | 22 ++++++++++++++++++++++ tests/units/Helper/TextHelperTest.php | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/Core/Markdown.php b/app/Core/Markdown.php index 2a866cf25..beb8ec258 100644 --- a/app/Core/Markdown.php +++ b/app/Core/Markdown.php @@ -41,11 +41,33 @@ class Markdown extends Parsedown { $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 * diff --git a/tests/units/Helper/TextHelperTest.php b/tests/units/Helper/TextHelperTest.php index c010094aa..0bd02e879 100644 --- a/tests/units/Helper/TextHelperTest.php +++ b/tests/units/Helper/TextHelperTest.php @@ -29,6 +29,16 @@ class TextHelperTest extends Base $this->assertEquals('

Test

', $textHelper->markdown('Test')); + $this->assertEquals( + '#123', + $textHelper->markdown('#123') + ); + + $this->assertEquals( + "

Heading 1

\n

Task #123

\n

Heading 2

", + $textHelper->markdown("# Heading 1\r\n\r\nTask #123\r\n\r\n## Heading 2") + ); + $this->assertEquals( '

Task #123

', $textHelper->markdown('Task #123')