From 0f6d7168b467fef664ee7c79d4dc05efb3683b40 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Sun, 28 Apr 2024 11:30:40 +0200 Subject: [PATCH] Add a gallery of unused attachments to the bottom of articles --- app/Classes/AttachmentsManager.php | 22 +++++++++++++++++- app/Classes/Traits/ManagesAttachments.php | 18 +++++++++++++++ resources/css/app.css | 1 + resources/css/figure.css | 2 +- resources/css/gallery.css | 28 +++++++++++++++++++++++ resources/views/article.blade.php | 8 +++++++ 6 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 resources/css/gallery.css diff --git a/app/Classes/AttachmentsManager.php b/app/Classes/AttachmentsManager.php index f1d52ed..4570faa 100644 --- a/app/Classes/AttachmentsManager.php +++ b/app/Classes/AttachmentsManager.php @@ -46,7 +46,7 @@ class AttachmentsManager private bool $isLoaded = false; - public function __construct(protected string $kind, protected Bundle $bundle) + public function __construct(public string $kind, protected Bundle $bundle) { $this->disk = $bundle->getDisk(); @@ -327,6 +327,26 @@ public function repair() } } + /** + * Collect and return unused attachments refs + */ + public function findUnusedAttachments(string $markdown, ?string $cover = null) + { + $unused = []; + + foreach (array_keys($this->manager->get('files', [])) as $ref) { + if (!empty($cover) && $cover === $ref) { + continue; + } + + if (!Str::contains($markdown, $ref)) { + $unused[] = $ref; + } + } + + return $unused; + } + /** * Find an attachment from its original URL, if specified */ diff --git a/app/Classes/Traits/ManagesAttachments.php b/app/Classes/Traits/ManagesAttachments.php index 6be38e5..c33cbc3 100644 --- a/app/Classes/Traits/ManagesAttachments.php +++ b/app/Classes/Traits/ManagesAttachments.php @@ -29,6 +29,24 @@ public function attachments(string $kind): AttachmentsManager return $this->registerAttachmentsManager($kind); } + /** + * Find unused attachments + */ + public function listUnusedAttachments() + { + $unusedAttachments = []; + + foreach ($this->attachmentsManagers as $manager) { + $unused = $manager->findUnusedAttachments($this->markdown()->get(), $this->metadata()->get('cover')); + + foreach ($unused as $ref) { + $unusedAttachments[$manager->kind][] = $manager->getComponentByRef($ref, 'gallery'); + } + } + + return $unusedAttachments; + } + /** * Register an attachments manager for specified filename */ diff --git a/resources/css/app.css b/resources/css/app.css index 5bc652a..584e298 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -15,6 +15,7 @@ @import "drop"; @import "site-footer"; @import "form"; +@import "gallery"; :root { --body-width: 800px; diff --git a/resources/css/figure.css b/resources/css/figure.css index 7e05c6a..bf94694 100644 --- a/resources/css/figure.css +++ b/resources/css/figure.css @@ -30,7 +30,7 @@ figure { margin: .75rem auto; text-align: center; color: #a8b4bd; - font-size: .85em; + font-size: .75em; p { text-align: center !important; diff --git a/resources/css/gallery.css b/resources/css/gallery.css new file mode 100644 index 0000000..26406fb --- /dev/null +++ b/resources/css/gallery.css @@ -0,0 +1,28 @@ +.gallery { + max-width: var(--design-width); + width: 100%; + display: grid; + grid-template-columns: repeat(3, 1fr); + gap: 1rem; + margin: auto; + + figure { + background-color: #282828; + border: 1px solid #444; + border-radius: 8px; + padding: 10px; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3); + } +} + +@media only screen and (max-width: 799px) { + .gallery { + grid-template-columns: repeat(2, 1fr); + } +} + +@media only screen and (max-width: 399px) { + .gallery { + grid-template-columns: repeat(1, 1fr); + } +} \ No newline at end of file diff --git a/resources/views/article.blade.php b/resources/views/article.blade.php index 3fd4335..49d304c 100644 --- a/resources/views/article.blade.php +++ b/resources/views/article.blade.php @@ -44,6 +44,14 @@ class="{!! $mainLink['classes'] !!}" {!! $body !!} + @foreach($bundle->listUnusedAttachments() as $kind => $attachments) + + @endforeach + @if(isset($showToc) && $showToc)
Sommaire