From eeb84c5be16ecca239adae9c851bc0f7db0875a1 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 4 Mar 2023 01:46:45 +0300 Subject: blog: support ToC --- lib/MyParsedown.php | 9 +++++++-- lib/markup.php | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/MyParsedown.php b/lib/MyParsedown.php index cd537bc..85ed9c4 100644 --- a/lib/MyParsedown.php +++ b/lib/MyParsedown.php @@ -3,13 +3,18 @@ class MyParsedown extends ParsedownExtended { public function __construct( + ?array $opts = null, protected bool $useImagePreviews = false ) { - parent::__construct([ + $parsedown_opts = [ 'tables' => [ 'tablespan' => true ] - ]); + ]; + if (!is_null($opts)) { + $parsedown_opts = array_merge($parsedown_opts, $opts); + } + parent::__construct($parsedown_opts); $this->InlineTypes['{'][] = 'FileAttach'; $this->InlineTypes['{'][] = 'Image'; diff --git a/lib/markup.php b/lib/markup.php index 2f25c6c..f6ddd0f 100644 --- a/lib/markup.php +++ b/lib/markup.php @@ -7,6 +7,19 @@ class markup { return $pd->text($md); } + public static function toc(string $md): string { + $pd = new MyParsedown([ + 'toc' => [ + 'lowercase' => true, + 'transliterate' => true, + 'urlencode' => false, + 'headings' => ['h1', 'h2', 'h3'] + ] + ]); + $pd->text($md); + return $pd->contentsList(); + } + public static function htmlToText(string $html): string { $text = html_entity_decode(strip_tags($html)); $lines = explode("\n", $text); -- cgit v1.2.3