From 6d5e438984988754b77b45dea6f98650ba612e1c Mon Sep 17 00:00:00 2001 From: Jelle Schutter Date: Mon, 7 Sep 2020 09:29:57 +0200 Subject: [PATCH] =?UTF-8?q?Header=20ID=20aus=20Markdown=20n=C3=A4her=20an?= =?UTF-8?q?=20GitHub=20anpassen=20(#3833)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redaxo/src/core/lib/util/markdown.php | 21 ++++++------ redaxo/src/core/tests/util/markdown_test.php | 35 +++++++++++++++----- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/redaxo/src/core/lib/util/markdown.php b/redaxo/src/core/lib/util/markdown.php index 39da494fd2..baca5c3734 100644 --- a/redaxo/src/core/lib/util/markdown.php +++ b/redaxo/src/core/lib/util/markdown.php @@ -136,14 +136,11 @@ private function handleHeader(array $block = null) [$level] = sscanf($block['element']['name'], 'h%d'); - if ($level < $this->topLevel || $level > $this->bottomLevel) { - return $block; - } + $plainText = strip_tags($this->{$block['element']['handler']}($block['element']['text'])); if (!isset($block['element']['attributes']['id'])) { - $baseId = $id = 'header-'.rex_string::normalize($block['element']['text'], '-'); - - for ($i = 2; isset($this->ids[$id]); ++$i) { + $baseId = $id = rex_string::normalize($plainText, '-'); + for ($i = 1; isset($this->ids[$id]); ++$i) { $id = $baseId.'-'.$i; } @@ -153,11 +150,13 @@ private function handleHeader(array $block = null) $id = $block['element']['attributes']['id']; $this->ids[$id] = true; - $this->headers[] = [ - 'level' => $level, - 'id' => $id, - 'text' => $block['element']['text'], - ]; + if ($level >= $this->topLevel && $level <= $this->bottomLevel) { + $this->headers[] = [ + 'level' => $level, + 'id' => $id, + 'text' => $plainText, + ]; + } return $block; } diff --git a/redaxo/src/core/tests/util/markdown_test.php b/redaxo/src/core/tests/util/markdown_test.php index 932ebfacf9..31348a228d 100644 --- a/redaxo/src/core/tests/util/markdown_test.php +++ b/redaxo/src/core/tests/util/markdown_test.php @@ -66,6 +66,14 @@ public function testParseWithToc() ## Sub 3 ### Sub 3.1 + +## Duplicate Test + +## Duplicate Test + +## [Title with Markdown](#sub-1) + +## Title with HTML MARKDOWN; [$toc, $content] = rex_markdown::factory()->parseWithToc($input, 2, 4); @@ -73,34 +81,45 @@ public function testParseWithToc() $expected = <<<'HTML'