Przeglądaj źródła

🐛 (markup): {{toc}} 目录项只给纯文本,不再嵌套标题里的术语模板

目录项本身是链接,再套一层术语链接(悬停弹卡片、点击打开词条)会和跳转到标题抢鼠标。
标题文字改用纯文本渲染:术语取译名,没有译名取原词,格式去掉。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 6 godzin temu
rodzic
commit
b37dcab2f0

+ 27 - 6
api-v13/app/Services/Markup/Templates/TocTemplate.php

@@ -7,8 +7,10 @@ use App\Services\Markup\Ir\Call;
 use App\Services\Markup\Ir\Document;
 use App\Services\Markup\Ir\Document;
 use App\Services\Markup\Ir\Element;
 use App\Services\Markup\Ir\Element;
 use App\Services\Markup\Ir\Node;
 use App\Services\Markup\Ir\Node;
+use App\Services\Markup\Ir\Text;
 use App\Services\Markup\Render\HtmlRenderer;
 use App\Services\Markup\Render\HtmlRenderer;
 use App\Services\Markup\Render\RenderContext;
 use App\Services\Markup\Render\RenderContext;
+use App\Services\Markup\Render\TextRenderer;
 use App\Services\Markup\Templates\Concerns\OwnsBlockTitle;
 use App\Services\Markup\Templates\Concerns\OwnsBlockTitle;
 
 
 /**
 /**
@@ -18,7 +20,9 @@ use App\Services\Markup\Templates\Concerns\OwnsBlockTitle;
  * - 标题:目录自带一行标题,规则见 {@see OwnsBlockTitle}(labels.toc;`title=` 改文字或隐藏;
  * - 标题:目录自带一行标题,规则见 {@see OwnsBlockTitle}(labels.toc;`title=` 改文字或隐藏;
  *   紧跟在手写标题后面时不显示)
  *   紧跟在手写标题后面时不显示)
  * - 兼容旧写法:紧挨在 `{{toc}}` 前面的标题(手写的「## 目录」)不进目录
  * - 兼容旧写法:紧挨在 `{{toc}}` 前面的标题(手写的「## 目录」)不进目录
- * - 遍历整篇内容,写在哪里都能列全;标题里的术语、格式在目录里照常显示
+ * - 遍历整篇内容,写在哪里都能列全
+ * - 目录项只给纯文本:标题里的术语取译名、格式去掉。目录项本身是链接,再套一层术语链接
+ *   (悬停弹卡片、点击打开词条)会和跳转到标题抢鼠标
  *
  *
  * tree:`{name: 'toc', props: {title}, children: [ul]}`;html:`<nav class="toc">`。
  * tree:`{name: 'toc', props: {title}, children: [ul]}`;html:`<nav class="toc">`。
  */
  */
@@ -104,12 +108,29 @@ final class TocTemplate extends Template
         }
         }
     }
     }
 
 
+    /**
+     * 目录项文字:标题的纯文本(术语取译名,没有译名取原词;格式去掉)。
+     *
+     * @param  list<Node>  $nodes
+     */
+    private function label(array $nodes, Call $call, RenderContext $ctx): string
+    {
+        $text = new RenderContext(
+            $ctx->options->with(['format' => Format::Text]),
+            $ctx->resolver,
+            new TextRenderer(Format::Text),
+            $ctx->idPrefix,
+        );
+
+        return trim((string) preg_replace('/\s+/u', ' ', $text->renderString(new Document($nodes, true, '', [], $call->scope, fragment: true))));
+    }
+
     /**
     /**
      * 按层级嵌套的 `ul > li > a`。
      * 按层级嵌套的 `ul > li > a`。
      *
      *
      * @param  list<array{level: int, id: string, nodes: list<Node>}>  $headings
      * @param  list<array{level: int, id: string, nodes: list<Node>}>  $headings
      */
      */
-    private function tree(array $headings): ?Element
+    private function tree(array $headings, Call $call, RenderContext $ctx): ?Element
     {
     {
         if ($headings === []) {
         if ($headings === []) {
             return null;
             return null;
@@ -133,7 +154,7 @@ final class TocTemplate extends Template
                 $stack[] = ['level' => end($stack)['level'] + 1, 'list' => $nested];
                 $stack[] = ['level' => end($stack)['level'] + 1, 'list' => $nested];
             }
             }
             end($stack)['list']->children[] = new Element('li', [], [
             end($stack)['list']->children[] = new Element('li', [], [
-                new Element('a', ['href' => '#'.$heading['id']], $heading['nodes']),
+                new Element('a', ['href' => '#'.$heading['id']], [new Text($this->label($heading['nodes'], $call, $ctx))]),
             ]);
             ]);
         }
         }
 
 
@@ -152,14 +173,14 @@ final class TocTemplate extends Template
 
 
     public function children(Call $call, RenderContext $ctx): ?Document
     public function children(Call $call, RenderContext $ctx): ?Document
     {
     {
-        $list = $this->tree($this->headings($call, $ctx));
+        $list = $this->tree($this->headings($call, $ctx), $call, $ctx);
 
 
         return $list === null ? null : new Document([$list], false, '', [], $call->scope);
         return $list === null ? null : new Document([$list], false, '', [], $call->scope);
     }
     }
 
 
     public function html(Call $call, RenderContext $ctx): string
     public function html(Call $call, RenderContext $ctx): string
     {
     {
-        $list = $this->tree($this->headings($call, $ctx));
+        $list = $this->tree($this->headings($call, $ctx), $call, $ctx);
         if ($list === null || ! $ctx->renderer instanceof HtmlRenderer) {
         if ($list === null || ! $ctx->renderer instanceof HtmlRenderer) {
             return '';
             return '';
         }
         }
@@ -205,7 +226,7 @@ final class TocTemplate extends Template
         $title = $this->title($call, $ctx);
         $title = $this->title($call, $ctx);
         $out = $title === '' ? [] : [$title];
         $out = $title === '' ? [] : [$title];
         foreach ($headings as $heading) {
         foreach ($headings as $heading) {
-            $text = trim($ctx->renderString(new Document($heading['nodes'], true, '', [], $call->scope, fragment: true)));
+            $text = $this->label($heading['nodes'], $call, $ctx);
             $out[] = str_repeat('  ', $heading['level'] - $top).$line($text, $heading['id']);
             $out[] = str_repeat('  ', $heading['level'] - $top).$line($text, $heading['id']);
         }
         }
 
 

+ 4 - 2
api-v13/tests/Feature/MarkupSyntaxTest.php

@@ -697,9 +697,11 @@ describe('toc', function () {
         ]);
         ]);
     });
     });
 
 
-    it('keeps templates inside heading text', function () use ($source, $toc) {
+    it('gives plain text, not the templates inside heading text', function () use ($source, $toc) {
+        // 目录项是链接,再套一层术语链接会和跳转抢鼠标
         $link = $toc(markupTree($source))['children'][0]['children'][0]['children'][0];
         $link = $toc(markupTree($source))['children'][0]['children'][0]['children'][0];
-        expect(collect($link['children'])->firstWhere('name', 'term')['props']['word'])->toBe('dhamma');
+        expect($link['children'])->toBe([['type' => 'text', 'value' => '甲 dhamma']]);
+        expect(markupRender($source, Format::Html))->not->toMatch('/<nav class="toc">.*term-ref.*<\/nav>/s');
     });
     });
 
 
     it('skips a hand-written heading right before it', function () use ($toc, $items) {
     it('skips a hand-written heading right before it', function () use ($toc, $items) {