TreeRenderer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Services\Markup\Render;
  3. use App\Services\Markup\Ir\Call;
  4. use App\Services\Markup\Ir\Document;
  5. use App\Services\Markup\Ir\Element;
  6. use App\Services\Markup\Ir\Node;
  7. use App\Services\Markup\Ir\Text;
  8. /**
  9. * IR → JSON 节点树(v3 API 下发给 dashboard-v6 / wikipali-mobile)。
  10. *
  11. * 三种节点:
  12. * - `{type: 'text', value}`:纯文本,前端直接作为 React 文本节点(自动转义)
  13. * - `{type: 'el', tag, attrs?, children}`:HTML 元素,tag 与 attrs 已过白名单
  14. * - `{type: 'tpl', name, props, children}`:模板,name 是前端组件名;props 里嵌套的内容(术语 note、句子正文)也是节点数组
  15. *
  16. * 单段落的内容(如一句译文)直接给出段落里的行内节点,不包 `p`。
  17. */
  18. final class TreeRenderer implements Renderer
  19. {
  20. /**
  21. * @return list<array<string, mixed>>
  22. */
  23. public function document(Document $document, RenderContext $ctx): array
  24. {
  25. return $this->nodes($document->nodes, $ctx);
  26. }
  27. /**
  28. * @param list<Node> $nodes
  29. * @return list<array<string, mixed>>
  30. */
  31. private function nodes(array $nodes, RenderContext $ctx): array
  32. {
  33. $out = [];
  34. foreach ($nodes as $node) {
  35. if (! $node instanceof Text && ! $node instanceof Element && ! $node instanceof Call) {
  36. continue; // 构建期的辅助节点(如配不上对的批注边界)不输出
  37. }
  38. $rendered = $this->node($node, $ctx);
  39. $last = array_key_last($out);
  40. // 相邻文本并成一个节点
  41. if ($rendered['type'] === 'text' && $last !== null && $out[$last]['type'] === 'text') {
  42. $out[$last]['value'] .= $rendered['value'];
  43. continue;
  44. }
  45. $out[] = $rendered;
  46. }
  47. return $out;
  48. }
  49. /**
  50. * @return array<string, mixed>
  51. */
  52. private function node(Node $node, RenderContext $ctx): array
  53. {
  54. if ($node instanceof Text) {
  55. return ['type' => 'text', 'value' => $node->value];
  56. }
  57. if ($node instanceof Element) {
  58. $element = ['type' => 'el', 'tag' => $node->tag];
  59. if ($node->attrs !== []) {
  60. $element['attrs'] = $node->attrs;
  61. }
  62. $element['children'] = $this->nodes($node->children, $ctx);
  63. return $element;
  64. }
  65. /** @var Call $node */
  66. $children = $node->template->children($node, $ctx);
  67. return [
  68. 'type' => 'tpl',
  69. 'name' => $node->template->component($node, $ctx),
  70. 'props' => (object) $this->props($node->template->props($node, $ctx), $ctx),
  71. 'children' => $children === null ? [] : $this->document($children, $ctx),
  72. ];
  73. }
  74. /**
  75. * props 里的 Document 展开成节点数组(递归到数组里)。
  76. *
  77. * @param array<array-key, mixed> $props
  78. * @return array<array-key, mixed>
  79. */
  80. private function props(array $props, RenderContext $ctx): array
  81. {
  82. foreach ($props as $key => $value) {
  83. if ($value instanceof Document) {
  84. $props[$key] = $this->document($value, $ctx);
  85. } elseif (is_array($value)) {
  86. $props[$key] = $this->props($value, $ctx);
  87. }
  88. }
  89. return $props;
  90. }
  91. }