1
0

Add a gallery of unused attachments to the bottom of articles

This commit is contained in:
Richard Dern 2024-04-28 11:30:40 +02:00
parent 2bbd15f0f1
commit 0f6d7168b4
6 changed files with 77 additions and 2 deletions

View File

@ -46,7 +46,7 @@ class AttachmentsManager
private bool $isLoaded = false; 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(); $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 * Find an attachment from its original URL, if specified
*/ */

View File

@ -29,6 +29,24 @@ public function attachments(string $kind): AttachmentsManager
return $this->registerAttachmentsManager($kind); 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 * Register an attachments manager for specified filename
*/ */

View File

@ -15,6 +15,7 @@
@import "drop"; @import "drop";
@import "site-footer"; @import "site-footer";
@import "form"; @import "form";
@import "gallery";
:root { :root {
--body-width: 800px; --body-width: 800px;

View File

@ -30,7 +30,7 @@ figure {
margin: .75rem auto; margin: .75rem auto;
text-align: center; text-align: center;
color: #a8b4bd; color: #a8b4bd;
font-size: .85em; font-size: .75em;
p { p {
text-align: center !important; text-align: center !important;

28
resources/css/gallery.css Normal file
View File

@ -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);
}
}

View File

@ -44,6 +44,14 @@ class="{!! $mainLink['classes'] !!}"
{!! $body !!} {!! $body !!}
@foreach($bundle->listUnusedAttachments() as $kind => $attachments)
<div class="gallery">
@foreach($attachments as $attachment)
{!! $attachment->render() !!}
@endforeach
</div>
@endforeach
@if(isset($showToc) && $showToc) @if(isset($showToc) && $showToc)
<details class="drop up"> <details class="drop up">
<summary>Sommaire</summary> <summary>Sommaire</summary>