From a58901e014fa4c559669e51a4ad328d5807ade5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Br=C3=BCckner?= Date: Fri, 7 Feb 2025 13:46:07 +0100 Subject: [PATCH 1/2] Add support for rendering Table of Contents in blog posts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduced Table of Contents parsing and rendering functionality in blog posts. Updated `BlogPost` model, `CreateBlogPostFromDataArray` class, and the template to include and display the TOC. Adjusted heading level configuration to ensure deeper heading levels are included in the TOC generation. Signed-off-by: Frank Brückner --- bootstrap/gulpfile.mjs | 1 - src/App/ContentParser/Parser.php | 4 ++-- src/Blog/BlogPost.php | 4 ++++ src/Blog/CreateBlogPostFromDataArray.php | 2 ++ src/Blog/templates/post.phtml | 4 ++++ 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bootstrap/gulpfile.mjs b/bootstrap/gulpfile.mjs index f1313763..5a8b0718 100644 --- a/bootstrap/gulpfile.mjs +++ b/bootstrap/gulpfile.mjs @@ -58,7 +58,6 @@ function js() { 'node_modules/jquery/dist/jquery.slim.min.js', 'node_modules/@popperjs/core/dist/umd/popper.min.js', 'node_modules/bootstrap/dist/js/bootstrap.min.js', - 'node_modules/anchor-js/anchor.min.js', 'js/base.js' ])) .pipe(concat({path: 'laminas.js'})) diff --git a/src/App/ContentParser/Parser.php b/src/App/ContentParser/Parser.php index 4be071e3..a61fd20d 100755 --- a/src/App/ContentParser/Parser.php +++ b/src/App/ContentParser/Parser.php @@ -44,7 +44,7 @@ public function __construct() 'heading_permalink' => [ 'insert' => 'after', 'min_heading_level' => 1, - 'max_heading_level' => 2, + 'max_heading_level' => 3, ], ] ); @@ -62,7 +62,7 @@ public function __construct() [ 'table_of_contents' => [ 'min_heading_level' => 2, - 'max_heading_level' => 2, + 'max_heading_level' => 3, ], ] ); diff --git a/src/Blog/BlogPost.php b/src/Blog/BlogPost.php index 3d4ac72e..e434c934 100644 --- a/src/Blog/BlogPost.php +++ b/src/Blog/BlogPost.php @@ -20,6 +20,8 @@ class BlogPost /** @var string */ public $extended; + public ?string $toc; + /** @var string */ public $id; @@ -47,6 +49,7 @@ public function __construct( array $tags, string $body, string $extended, + ?string $toc, bool $isDraft, bool $isPublic ) { @@ -58,6 +61,7 @@ public function __construct( $this->tags = $tags; $this->body = $body; $this->extended = $extended; + $this->toc = $toc; $this->isDraft = $isDraft; $this->isPublic = $isPublic; } diff --git a/src/Blog/CreateBlogPostFromDataArray.php b/src/Blog/CreateBlogPostFromDataArray.php index fb9cc177..fba7e2be 100644 --- a/src/Blog/CreateBlogPostFromDataArray.php +++ b/src/Blog/CreateBlogPostFromDataArray.php @@ -55,6 +55,7 @@ private function createBlogPostFromDataArray(array $post): BlogPost $document = $this->getContentParser()->parse($post['path']); $post = $document->getFrontMatter(); + $toc = $document->getTableOfContents(); $parts = explode($this->postDelimiter, $document->getContent(), 2); $created = $this->createDateTimeFromString($post['created']); $updated = $post['updated'] && $post['updated'] !== $post['created'] @@ -72,6 +73,7 @@ private function createBlogPostFromDataArray(array $post): BlogPost : explode('|', trim((string) $post['tags'], '|')), $parts[0], $parts[1] ?? '', + $toc, (bool) $post['draft'], (bool) $post['public'] ); diff --git a/src/Blog/templates/post.phtml b/src/Blog/templates/post.phtml index 72872b9c..a2e10422 100644 --- a/src/Blog/templates/post.phtml +++ b/src/Blog/templates/post.phtml @@ -53,6 +53,10 @@ $this->end();
Last updated on .

+ toc): ?> +
Table of Contents
+ toc ?> + From 02a4639c2d7c1be456c4a26aa006d28742a7f315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Br=C3=BCckner?= Date: Sat, 8 Feb 2025 11:50:28 +0100 Subject: [PATCH 2/2] Remove Anchor.js integration and related styles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Anchor.js functionality and sticky header fixes have been removed from the project. This includes deleting associated SCSS styles, JavaScript code, and the package dependency in `package.json`. The cleanup helps simplify the codebase and remove unused features. Signed-off-by: Frank Brückner --- bootstrap/js/base.js | 6 ------ bootstrap/package.json | 1 - bootstrap/scss/_custom-styles.scss | 16 ---------------- 3 files changed, 23 deletions(-) diff --git a/bootstrap/js/base.js b/bootstrap/js/base.js index a5795ee1..f61a966b 100644 --- a/bootstrap/js/base.js +++ b/bootstrap/js/base.js @@ -1,12 +1,6 @@ 'use strict'; { - // Anchors - anchors.options.placement = 'left'; - anchors.add( - 'article h1, article h2, article h3, article h4, article h5' - ); - // Pre elements - Prism code samples const preElements = document.querySelectorAll('pre'); preElements.forEach(element => { diff --git a/bootstrap/package.json b/bootstrap/package.json index 3c54b42e..af53d833 100644 --- a/bootstrap/package.json +++ b/bootstrap/package.json @@ -9,7 +9,6 @@ "license": "BSD-3-Clause", "devDependencies": { "@popperjs/core": "^2.11.8", - "anchor-js": "^5.0.0", "autoprefixer": "^10.4.14", "bootstrap": "^5.3.3", "gulp": "^5.0.0", diff --git a/bootstrap/scss/_custom-styles.scss b/bootstrap/scss/_custom-styles.scss index 7a2f4a52..fdb11b3d 100644 --- a/bootstrap/scss/_custom-styles.scss +++ b/bootstrap/scss/_custom-styles.scss @@ -168,22 +168,6 @@ pre[class*=language-] { font-size: smaller; } -/* - * anchor-js, sticky header fix - * @see https://www.bryanbraun.com/anchorjs/#pages-with-a-sticky-navbar - */ -article { - h1, h2, h3, h4, h5 { - &::before { - content: ''; - display: block; - margin-top: -100px; - height: 100px; - width: 1px; - } - } -} - .dropdown-item { &.active, &:active {