EmbedsArticle.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Services\Markup\Templates\Concerns;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Render\RenderContext;
  6. /**
  7. * 交给前端 `article` 组件嵌入的模板({{article}}、{{chapter}})共用的输出:
  8. * props 的拼法、纯文本只留标题、html 不输出(只有前端组件能渲染)。
  9. * 标题走 {@see self::title()},模板可覆盖。
  10. */
  11. trait EmbedsArticle
  12. {
  13. /**
  14. * @param list<string> $optional 有值才写进 props 的参数,按顺序
  15. * @return array<string, mixed>
  16. */
  17. protected function embedProps(Call $call, string $type, string $id, array $optional): array
  18. {
  19. $props = ['type' => $type, 'id' => $id];
  20. // style 为空时不给:前端组件用它自己的默认值(modal),传空串反而什么都不渲染
  21. foreach ($optional as $name) {
  22. $value = $name === 'title' ? $this->title($call) : (string) $call->param($name);
  23. if ($value !== '') {
  24. $props[$name] = $value;
  25. }
  26. }
  27. $props['parentChannels'] = $call->scope->channels;
  28. return $props;
  29. }
  30. /**
  31. * 显示的标题(props 与纯文本共用);嵌入经文的模板按 {@see CitesParagraph} 补缺省标题。
  32. */
  33. protected function title(Call $call): string
  34. {
  35. return (string) $call->param('title');
  36. }
  37. public function plain(Call $call, RenderContext $ctx, Format $format): string
  38. {
  39. return $format === Format::Prompt ? '' : $this->title($call);
  40. }
  41. public function html(Call $call, RenderContext $ctx): string
  42. {
  43. return '';
  44. }
  45. }