| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace App\Services\Markup\Templates;
- use App\Services\Markup\Format;
- use App\Services\Markup\Ir\Call;
- use App\Services\Markup\Ir\Document;
- use App\Services\Markup\Ir\Element;
- use App\Services\Markup\Ir\Node;
- use App\Services\Markup\Render\HtmlRenderer;
- use App\Services\Markup\Render\RenderContext;
- use App\Services\Markup\Templates\Concerns\OwnsBlockTitle;
- /**
- * 目录:`{{toc}}`,从本篇内容的标题自动生成,每项链接到对应标题(`#h-…`,IR 构建时生成的稳定 id)。
- *
- * - 层级:缺省收二、三级标题;`{{toc|2-4}}`、`{{toc|2}}` 指定
- * - 标题:目录自带一行标题,规则见 {@see OwnsBlockTitle}(labels.toc;`title=` 改文字或隐藏;
- * 紧跟在手写标题后面时不显示)
- * - 兼容旧写法:紧挨在 `{{toc}}` 前面的标题(手写的「## 目录」)不进目录
- * - 遍历整篇内容,写在哪里都能列全;标题里的术语、格式在目录里照常显示
- *
- * tree:`{name: 'toc', props: {title}, children: [ul]}`;html:`<nav class="toc">`。
- */
- final class TocTemplate extends Template
- {
- use OwnsBlockTitle;
- protected string $component = 'toc';
- public function block(): bool
- {
- return true;
- }
- public function params(): array
- {
- return ['levels' => ParamSpec::at(1, '2-3'), 'title' => new ParamSpec];
- }
- /**
- * @return array{0: int, 1: int}
- */
- private function levels(Call $call): array
- {
- if (preg_match('/^\s*([1-6])\s*(?:-\s*([1-6]))?\s*$/', (string) $call->param('levels'), $m) !== 1) {
- return [2, 3];
- }
- $from = (int) $m[1];
- $to = isset($m[2]) && $m[2] !== '' ? (int) $m[2] : $from;
- return [min($from, $to), max($from, $to)];
- }
- private function title(Call $call, RenderContext $ctx): string
- {
- return $this->blockTitle($call, $ctx, 'labels.toc');
- }
- /**
- * 本篇内容里要进目录的标题(文档顺序)。
- *
- * @return list<array{level: int, id: string, nodes: list<Node>}>
- */
- private function headings(Call $call, RenderContext $ctx): array
- {
- if ($ctx->document === null) {
- return [];
- }
- [$from, $to] = $this->levels($call);
- $all = [];
- $skip = null;
- $this->walk($ctx->document->nodes, $call, $all, $skip);
- return array_values(array_filter(
- $all,
- fn (array $heading, int $index) => $index !== $skip && $heading['level'] >= $from && $heading['level'] <= $to,
- ARRAY_FILTER_USE_BOTH,
- ));
- }
- /**
- * @param list<Node> $nodes
- * @param list<array{level: int, id: string, nodes: list<Node>}> $all
- */
- private function walk(array $nodes, Call $self, array &$all, ?int &$skip): void
- {
- foreach ($nodes as $node) {
- if ($node === $self) {
- // 紧挨在目录前面的标题(手写的「## 目录」)不进目录
- $skip = $all === [] ? null : count($all) - 1;
- continue;
- }
- if (! $node instanceof Element) {
- continue;
- }
- if (preg_match('/^h([1-6])$/', $node->tag, $m) === 1 && isset($node->attrs['id'])) {
- $all[] = ['level' => (int) $m[1], 'id' => $node->attrs['id'], 'nodes' => $node->children];
- continue;
- }
- $this->walk($node->children, $self, $all, $skip);
- }
- }
- /**
- * 按层级嵌套的 `ul > li > a`。
- *
- * @param list<array{level: int, id: string, nodes: list<Node>}> $headings
- */
- private function tree(array $headings): ?Element
- {
- if ($headings === []) {
- return null;
- }
- $root = new Element('ul');
- /** @var list<array{level: int, list: Element}> $stack */
- $stack = [['level' => min(array_column($headings, 'level')), 'list' => $root]];
- foreach ($headings as $heading) {
- while ($heading['level'] < end($stack)['level'] && count($stack) > 1) {
- array_pop($stack);
- }
- while ($heading['level'] > end($stack)['level']) {
- $list = end($stack)['list'];
- $parent = end($list->children);
- if (! $parent instanceof Element) {
- $parent = new Element('li');
- $list->children[] = $parent;
- }
- $nested = new Element('ul');
- $parent->children[] = $nested;
- $stack[] = ['level' => end($stack)['level'] + 1, 'list' => $nested];
- }
- end($stack)['list']->children[] = new Element('li', [], [
- new Element('a', ['href' => '#'.$heading['id']], $heading['nodes']),
- ]);
- }
- return $root;
- }
- /**
- * @return array<string, mixed>
- */
- public function props(Call $call, RenderContext $ctx): array
- {
- $title = $this->title($call, $ctx);
- return $title === '' ? [] : ['title' => $title];
- }
- public function children(Call $call, RenderContext $ctx): ?Document
- {
- $list = $this->tree($this->headings($call, $ctx));
- return $list === null ? null : new Document([$list], false, '', [], $call->scope);
- }
- public function html(Call $call, RenderContext $ctx): string
- {
- $list = $this->tree($this->headings($call, $ctx));
- if ($list === null || ! $ctx->renderer instanceof HtmlRenderer) {
- return '';
- }
- $title = $this->title($call, $ctx);
- return '<nav class="toc">'
- .($title === '' ? '' : '<div class="toc-title">'.e($title).'</div>')
- .$ctx->renderer->nodes([$list], $ctx)
- .'</nav>';
- }
- /**
- * 纯文本:标题一行,下面按层级缩进列出。
- */
- public function plain(Call $call, RenderContext $ctx, Format $format): string
- {
- return $this->lines($call, $ctx, fn (string $text, string $id) => $text);
- }
- public function text(Call $call, RenderContext $ctx): string
- {
- return $this->plain($call, $ctx, Format::Text);
- }
- /**
- * markdown:`- [标题](#id)` 列表。
- */
- public function markdown(Call $call, RenderContext $ctx): string
- {
- return $this->lines($call, $ctx, fn (string $text, string $id) => "- [{$text}](#{$id})");
- }
- /**
- * @param callable(string, string): string $line
- */
- private function lines(Call $call, RenderContext $ctx, callable $line): string
- {
- $headings = $this->headings($call, $ctx);
- if ($headings === []) {
- return '';
- }
- $top = min(array_column($headings, 'level'));
- $title = $this->title($call, $ctx);
- $out = $title === '' ? [] : [$title];
- foreach ($headings as $heading) {
- $text = trim($ctx->renderString(new Document($heading['nodes'], true, '', [], $call->scope, fragment: true)));
- $out[] = str_repeat(' ', $heading['level'] - $top).$line($text, $heading['id']);
- }
- return implode("\n", $out)."\n";
- }
- }
|