ArticleTemplate.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Render\RenderContext;
  6. /**
  7. * 嵌入文章 / 章节:`{{article|type|id|title|channel|style|book|paragraphs|anthology}}`。
  8. * `type=chapter` 且没给 id 时由 book + paragraphs 拼出 id。
  9. */
  10. final class ArticleTemplate extends Template
  11. {
  12. protected string $component = 'article';
  13. public function params(): array
  14. {
  15. return [
  16. 'type' => ParamSpec::at(1),
  17. 'id' => ParamSpec::at(2),
  18. 'title' => ParamSpec::at(3),
  19. 'channel' => ParamSpec::at(4),
  20. 'style' => ParamSpec::at(5),
  21. 'book' => ParamSpec::at(6),
  22. 'paragraphs' => ParamSpec::at(7),
  23. 'anthology' => ParamSpec::at(8),
  24. ];
  25. }
  26. /**
  27. * @return array<string, mixed>
  28. */
  29. public function props(Call $call, RenderContext $ctx): array
  30. {
  31. $type = (string) $call->param('type');
  32. $id = (string) $call->param('id');
  33. $book = (string) $call->param('book');
  34. $paragraphs = (string) $call->param('paragraphs');
  35. if ($type === 'chapter' && $id === '') {
  36. $id = ((int) $book).'-'.((int) $paragraphs);
  37. }
  38. $props = ['type' => $type, 'id' => $id];
  39. // style 为空时不给:前端组件用它自己的默认值(modal),传空串反而什么都不渲染
  40. foreach (['style', 'channel', 'title', 'anthology'] as $name) {
  41. $value = (string) $call->param($name);
  42. if ($value !== '') {
  43. $props[$name] = $value;
  44. }
  45. }
  46. if ($book !== '') {
  47. $props['book'] = $book;
  48. }
  49. if ($paragraphs !== '') {
  50. $props['paragraphs'] = $paragraphs;
  51. }
  52. $props['parentChannels'] = $call->scope->channels;
  53. return $props;
  54. }
  55. public function plain(Call $call, RenderContext $ctx, Format $format): string
  56. {
  57. return $format === Format::Prompt ? '' : (string) $call->param('title');
  58. }
  59. public function html(Call $call, RenderContext $ctx): string
  60. {
  61. return '';
  62. }
  63. }