Skip to content

Commit

Permalink
fix: parsedown toc (#2009)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny authored Jul 3, 2024
1 parent 8a85a52 commit 7085d81
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/Converter/Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
use Cecil\Util;
use Highlight\Highlighter;

class Parsedown extends \ParsedownToC
/**
* @property array $InlineTypes
* @property string $inlineMarkerList
* @property array $specialCharacters
* @property array $BlockTypes
*/
class Parsedown extends \ParsedownToc
{
/** @var Builder */
protected $builder;
Expand Down Expand Up @@ -59,7 +65,8 @@ public function __construct(Builder $builder, ?array $options = null)
// options
$options = array_merge(['selectors' => (array) $this->config->get('pages.body.toc')], $options ?? []);

parent::__construct($options);
parent::__construct();
parent::setOptions($options);
}

/**
Expand Down Expand Up @@ -89,7 +96,7 @@ protected function inlineInsert($Excerpt)
*/
protected function inlineLink($Excerpt)
{
$link = parent::inlineLink($Excerpt);
$link = parent::inlineLink($Excerpt); // @phpstan-ignore staticMethod.notFound

if (!isset($link)) {
return null;
Expand Down Expand Up @@ -235,7 +242,7 @@ protected function inlineLink($Excerpt)
*/
protected function inlineImage($Excerpt)
{
$InlineImage = parent::inlineImage($Excerpt);
$InlineImage = parent::inlineImage($Excerpt); // @phpstan-ignore staticMethod.notFound
if (!isset($InlineImage)) {
return null;
}
Expand Down Expand Up @@ -429,7 +436,7 @@ protected function inlineImage($Excerpt)
],
];
$picture['element']['text'][] = $source['element'];
unset($image['element']['attributes']['title']);
unset($image['element']['attributes']['title']); // @phpstan-ignore unset.offset
$picture['element']['text'][] = $image['element'];
$image = $picture;
} catch (\Exception $e) {
Expand Down Expand Up @@ -648,7 +655,7 @@ private function createFigure(array $inline): array
return $inline;
}

$titleRawHtml = $this->line($inline['element']['attributes']['title']);
$titleRawHtml = $this->line($inline['element']['attributes']['title']); // @phpstan-ignore method.notFound
$inline['element']['attributes']['title'] = strip_tags($titleRawHtml);

$figcaption = [
Expand Down
5 changes: 3 additions & 2 deletions src/Renderer/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,13 @@ public function markdownToHtml(?string $markdown): ?string
*
* @throws RuntimeException
*/
public function markdownToToc(?string $markdown, $format = 'html', $url = ''): ?string
public function markdownToToc(?string $markdown, $format = 'html', ?array $selectors = null, string $url = ''): ?string
{
$markdown = $markdown ?? '';
$selectors = $selectors ?? (array) $this->config->get('pages.body.toc');

try {
$parsedown = new Parsedown($this->builder, ['selectors' => ['h2'], 'url' => $url]);
$parsedown = new Parsedown($this->builder, ['selectors' => $selectors, 'url' => $url]);
$parsedown->body($markdown);
$return = $parsedown->contentsList($format);
} catch (\Exception) {
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/website/layouts/markdown-filter.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

{{ page.content }}

<h2><code>markdown_to_html</code> filter</h2>

{% set md = '**This is bold text.**' %}
{{ md|markdown_to_html }}

Expand All @@ -13,4 +15,22 @@
[This a link to arnaudligny.fr](https://arnaudligny.fr)
{% endapply %}

<h2><code>toc</code> filter</h2>

Table of content:

{% apply toc %}
## H2 header

text

### H3 header

text

#### H4 header

text
{% endapply %}

{% endblock %}
2 changes: 1 addition & 1 deletion tests/fixtures/website/pages/Markdown/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
layout: markdown-filter
---
<!-- break -->
# Filter `markdown_to_html`
# Filter `markdown_to_html` and `toc`

0 comments on commit 7085d81

Please sign in to comment.