TOC: extract h1/h2/h3, indent sub-levels in charte/licence sidebar

This commit is contained in:
Pontoporeia
2026-07-10 14:16:05 +02:00
parent 485324368f
commit c513ad2545
5 changed files with 16 additions and 7 deletions
+7
View File
@@ -38,6 +38,13 @@
font-weight: 400;
}
/* Indent sub-headings in TOC */
.toc-list .toc-level-2 { padding-left: var(--space-s); }
.toc-list .toc-level-3 { padding-left: var(--space-m); }
.toc-list .toc-level-2 a { font-size: var(--step--1); }
.toc-list .toc-level-3 a { font-size: var(--step--2); }
/* ── Public TOC (details/summary block) ─────────────────────────────────── */
.toc {
border: none;
+6 -5
View File
@@ -6,12 +6,12 @@
class MarkdownHelper
{
/**
* Extract h1 headings from raw markdown content as TOC items.
* Extract h1h3 headings from raw markdown content as TOC items.
*
* Each heading gets an anchor id matching CommonMark's default slugification
* (lowercase, spaces → hyphens, punctuation stripped).
*
* @return array<int, array{label: string, href: string}>
* @return array<int, array{label: string, href: string, level: int}>
*/
public static function extractToc(string $content): array
{
@@ -22,10 +22,11 @@ class MarkdownHelper
$normalizer = new \League\CommonMark\Normalizer\SlugNormalizer();
foreach ($lines as $line) {
if (preg_match('/^#\s+(.+)$/', $line, $m)) {
$label = trim($m[1]);
if (preg_match('/^(#{1,3})\s+(.+)$/', $line, $m)) {
$level = strlen($m[1]);
$label = trim($m[2]);
$id = $normalizer->normalize($label);
$items[] = ['label' => $label, 'href' => '#' . $id];
$items[] = ['label' => $label, 'href' => '#' . $id, 'level' => $level];
}
}
return $items;
+1 -1
View File
@@ -6,7 +6,7 @@
<summary><?= icon('caret-down', 0, 'toc-caret') ?> PARTIES</summary>
<ul class="toc-list">
<?php foreach ($tocItems as $item): ?>
<li><a href="<?= htmlspecialchars($item['href']) ?>"><?= htmlspecialchars($item['label']) ?></a></li>
<li class="toc-level-<?= $item['level'] ?? 1 ?>"><a href="<?= htmlspecialchars($item['href']) ?>"><?= htmlspecialchars($item['label']) ?></a></li>
<?php endforeach; ?>
</ul>
</details>
+1 -1
View File
@@ -6,7 +6,7 @@
<summary><?= icon('caret-down', 0, 'toc-caret') ?> PARTIES</summary>
<ul class="toc-list">
<?php foreach ($tocItems as $item): ?>
<li><a href="<?= htmlspecialchars($item['href']) ?>"><?= htmlspecialchars($item['label']) ?></a></li>
<li class="toc-level-<?= $item['level'] ?? 1 ?>"><a href="<?= htmlspecialchars($item['href']) ?>"><?= htmlspecialchars($item['label']) ?></a></li>
<?php endforeach; ?>
</ul>
</details>