Storyline: unfoldable section list per chapter in the sidebar

Each chapter in the right-hand menu gets a caret that unfolds its
numbered sections (e.g. the prologue's 28 scenes); clicking one jumps
straight to it. The chapter you're reading starts unfolded, other
chapters' sections link across (/storyline/overview#section-…), and the
section at the top of the pane is highlighted as you scroll.

- ChapterOutline service parses each rendered chapter for its
  <h2 class="storyline-section"> headings, adds anchor ids and returns the
  outline; the controller renders all chapters once to feed the sidebar.
- storyline-toc Stimulus controller: fold/unfold, in-pane jump, scroll-spy
  (works in the split view and in the small-screen window-scroll fallback).
- Tests for the outline parser.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Frank
2026-09-21 18:44:54 +02:00
co-authored by Claude Sonnet 5
parent 8b8f67c1d0
commit b77e5eaa4f
6 changed files with 229 additions and 8 deletions
+16 -1
View File
@@ -4,6 +4,7 @@ namespace App\Controller;
use App\Form\ChapterNoteType;
use App\Repository\ChapterNoteRepository;
use App\Storyline\ChapterOutline;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@@ -51,7 +52,7 @@ class StorylineController extends AbstractController
}
#[Route('/storyline/{slug}', name: 'app_storyline_chapter', requirements: ['slug' => '[a-z0-9-]+'], methods: ['GET', 'POST'])]
public function chapter(string $slug, Request $request, ChapterNoteRepository $notes, EntityManagerInterface $em): Response
public function chapter(string $slug, Request $request, ChapterNoteRepository $notes, EntityManagerInterface $em, ChapterOutline $outline): Response
{
if (!isset(self::CHAPTERS[$slug])) {
throw $this->createNotFoundException('No such storyline chapter.');
@@ -71,6 +72,18 @@ class StorylineController extends AbstractController
return $this->redirectToRoute('app_storyline_chapter', ['slug' => $slug]);
}
// Render every chapter once: the current one is shown (with anchors),
// the rest only feed the sidebar's per-section jump lists.
$sections = [];
$body = '';
foreach (self::CHAPTERS as $s => $c) {
$processed = $outline->process($this->renderView($c['template']));
$sections[$s] = $processed['sections'];
if ($s === $slug) {
$body = $processed['html'];
}
}
$slugs = array_keys(self::CHAPTERS);
$pos = array_search($slug, $slugs, true);
@@ -78,6 +91,8 @@ class StorylineController extends AbstractController
'chapters' => self::CHAPTERS,
'slug' => $slug,
'chapter' => self::CHAPTERS[$slug],
'body' => $body,
'sections' => $sections,
'note' => $note,
'form' => $form,
'prev' => $pos > 0 ? $slugs[$pos - 1] : null,
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace App\Storyline;
use Symfony\Component\String\Slugger\AsciiSlugger;
/**
* Turns a rendered chapter into an outline: finds each numbered section
* heading (<h2 class="storyline-section"><span>21</span> Title</h2>), gives it
* an anchor id, and reports it so the sidebar can link straight to it.
*/
final class ChapterOutline
{
private const HEADING = '~<h2 class="storyline-section"([^>]*)>\s*<span>(.*?)</span>\s*(.*?)</h2>~s';
/**
* @return array{html: string, sections: list<array{id: string, num: string, title: string}>}
*/
public function process(string $html): array
{
$slugger = new AsciiSlugger();
$sections = [];
$n = 0;
$html = preg_replace_callback(self::HEADING, function (array $m) use (&$sections, &$n, $slugger): string {
++$n;
$num = trim(html_entity_decode(strip_tags($m[2]), \ENT_QUOTES | \ENT_HTML5));
$title = trim(html_entity_decode(strip_tags($m[3]), \ENT_QUOTES | \ENT_HTML5));
$id = sprintf('section-%d-%s', $n, strtolower($slugger->slug($title)->toString()));
$sections[] = ['id' => $id, 'num' => $num, 'title' => $title];
return sprintf('<h2 class="storyline-section" id="%s"%s><span>%s</span> %s</h2>', $id, $m[1], $m[2], $m[3]);
}, $html) ?? $html;
return ['html' => $html, 'sections' => $sections];
}
}