Skip to content

Commit

Permalink
Header ID aus Markdown näher an GitHub anpassen (#3833)
Browse files Browse the repository at this point in the history
  • Loading branch information
jelleschutter committed Sep 7, 2020
1 parent 7050c2a commit 6d5e438
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
21 changes: 10 additions & 11 deletions redaxo/src/core/lib/util/markdown.php
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
35 changes: 27 additions & 8 deletions redaxo/src/core/tests/util/markdown_test.php
Expand Up @@ -66,41 +66,60 @@ public function testParseWithToc()
## Sub 3
### Sub 3.1
## Duplicate Test
## Duplicate Test
## [Title with Markdown](#sub-1)
## <i>Title with HTML</i>
MARKDOWN;

[$toc, $content] = rex_markdown::factory()->parseWithToc($input, 2, 4);

$expected = <<<'HTML'
<ul>
<li>
<a href="#header-sub-1">Sub 1</a>
<a href="#sub-1">Sub 1</a>
<ul>
<li>
<a href="#header-sub-1-1">Sub 1.1</a>
<a href="#sub-1-1">Sub 1.1</a>
</li>
<li>
<a href="#header-sub-1-2">Sub 1.2</a>
<a href="#sub-1-2">Sub 1.2</a>
<ul>
<li>
<a href="#header-sub-sub-1-2-1">Sub Sub 1.2.1</a>
<a href="#sub-sub-1-2-1">Sub Sub 1.2.1</a>
</li>
</ul>
</li>
</ul>
<li>
<a href="#header-sub-2">Sub 2</a>
<a href="#sub-2">Sub 2</a>
<ul>
<li>
<a href="#header-sub-2-1">Sub 2.1</a>
<a href="#sub-2-1">Sub 2.1</a>
</li>
</ul>
<li>
<a href="#header-sub-3">Sub 3</a>
<a href="#sub-3">Sub 3</a>
<ul>
<li>
<a href="#header-sub-3-1">Sub 3.1</a>
<a href="#sub-3-1">Sub 3.1</a>
</li>
</ul>
<li>
<a href="#duplicate-test">Duplicate Test</a>
</li>
<li>
<a href="#duplicate-test-1">Duplicate Test</a>
</li>
<li>
<a href="#title-with-markdown">Title with Markdown</a>
</li>
<li>
<a href="#title-with-html">Title with HTML</a>
</li>
</ul>
Expand Down

0 comments on commit 6d5e438

Please sign in to comment.