Skip to content

Commit

Permalink
Merge pull request #248 from froschdesign/feature/table-of-contents
Browse files Browse the repository at this point in the history
Add support for rendering Table of Contents in blog posts
  • Loading branch information
arhimede authored Feb 11, 2025
2 parents 3aadb3e + 02a4639 commit 81dfcc2
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 26 deletions.
1 change: 0 additions & 1 deletion bootstrap/gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'}))
Expand Down
6 changes: 0 additions & 6 deletions bootstrap/js/base.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
1 change: 0 additions & 1 deletion bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 0 additions & 16 deletions bootstrap/scss/_custom-styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/App/ContentParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct()
'heading_permalink' => [
'insert' => 'after',
'min_heading_level' => 1,
'max_heading_level' => 2,
'max_heading_level' => 3,
],
]
);
Expand All @@ -62,7 +62,7 @@ public function __construct()
[
'table_of_contents' => [
'min_heading_level' => 2,
'max_heading_level' => 2,
'max_heading_level' => 3,
],
]
);
Expand Down
4 changes: 4 additions & 0 deletions src/Blog/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class BlogPost
/** @var string */
public $extended;

public ?string $toc;

/** @var string */
public $id;

Expand Down Expand Up @@ -47,6 +49,7 @@ public function __construct(
array $tags,
string $body,
string $extended,
?string $toc,
bool $isDraft,
bool $isPublic
) {
Expand All @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Blog/CreateBlogPostFromDataArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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']
);
Expand Down
4 changes: 4 additions & 0 deletions src/Blog/templates/post.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ $this->end();
<br />Last updated on <time class="dt-updated" datetime="<?= $this->formatDateRfc($post->updated) ?>"><?= $this->formatDate($post->updated) ?></time>.
<?php endif ?>
</p>
<?php if ($post->toc): ?>
<h5>Table of Contents</h5>
<?= $post->toc ?>
<?php endif ?>
</aside>
</div>
</div>

0 comments on commit 81dfcc2

Please sign in to comment.