IrBuilder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace App\Services\Markup\Parsing;
  3. use App\Services\Markup\Annotation\AnnotationSet;
  4. use App\Services\Markup\Annotation\AnnotationToken;
  5. use App\Services\Markup\Annotation\AnnotationTokenNode;
  6. use App\Services\Markup\Annotation\RangeWrapper;
  7. use App\Services\Markup\Html\HtmlFragment;
  8. use App\Services\Markup\Html\HtmlPolicy;
  9. use App\Services\Markup\Html\InlineHtmlAssembler;
  10. use App\Services\Markup\Ir\Call;
  11. use App\Services\Markup\Ir\Document;
  12. use App\Services\Markup\Ir\Element;
  13. use App\Services\Markup\Ir\Node;
  14. use App\Services\Markup\Ir\Scope;
  15. use App\Services\Markup\Ir\Text;
  16. use App\Services\Markup\Templates\NoteTemplate;
  17. use Closure;
  18. use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote;
  19. use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
  20. use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
  21. use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
  22. use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
  23. use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
  24. use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
  25. use League\CommonMark\Extension\CommonMark\Node\Block\ThematicBreak;
  26. use League\CommonMark\Extension\CommonMark\Node\Inline\Code;
  27. use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis;
  28. use League\CommonMark\Extension\CommonMark\Node\Inline\HtmlInline;
  29. use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
  30. use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
  31. use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
  32. use League\CommonMark\Extension\Strikethrough\Strikethrough;
  33. use League\CommonMark\Extension\Table\Table;
  34. use League\CommonMark\Extension\Table\TableCell;
  35. use League\CommonMark\Extension\Table\TableRow;
  36. use League\CommonMark\Extension\Table\TableSection;
  37. use League\CommonMark\Extension\TaskList\TaskListItemMarker;
  38. use League\CommonMark\Node\Block\Paragraph;
  39. use League\CommonMark\Node\Inline\Newline;
  40. use League\CommonMark\Node\Inline\Text as MdText;
  41. use League\CommonMark\Node\Node as MdNode;
  42. /**
  43. * CommonMark AST → IR。
  44. *
  45. * 除了逐个节点对译,还做四件事:
  46. * - 占位符换回模板调用;代码里的占位符换回源码原文
  47. * - ```mermaid 变成 mermaid 模板;`<li><div>…</div></li>` 变成 toggle 模板(v2 的「可展开」)
  48. * - 只有脚注的段落并到上一段末尾(```note 前后都是空行时)
  49. * - 原始 HTML 过白名单
  50. */
  51. final class IrBuilder
  52. {
  53. private int $headingCount = 0;
  54. /**
  55. * @param array<int, Call> $calls 占位符下标 => 调用
  56. * @param array<int, string> $raws 占位符下标 => 源码原文
  57. * @param Closure(string, list<string>, string): Call $makeCall 建内部生成的调用(mermaid、toggle)
  58. * @param (Closure(int): ?Node)|null $annotation 批注占位符下标 → 模板调用或区间边界({@see AnnotationSet})
  59. */
  60. public function __construct(
  61. private readonly array $calls,
  62. private readonly array $raws,
  63. private readonly Closure $makeCall,
  64. private readonly Scope $scope,
  65. private readonly string $headingSeed,
  66. private readonly ?Closure $annotation = null,
  67. ) {}
  68. /**
  69. * @return array{0: list<Node>, 1: bool} [节点, 是否单段落(行内)]
  70. */
  71. public function build(MdNode $document): array
  72. {
  73. $blocks = $this->blocks($document);
  74. if ($this->annotation !== null) {
  75. $blocks = RangeWrapper::apply($blocks, $this->scope);
  76. }
  77. if (count($blocks) === 1 && $blocks[0] instanceof Element && $blocks[0]->tag === 'p') {
  78. return [$blocks[0]->children, true];
  79. }
  80. return [$blocks, $blocks === []];
  81. }
  82. /**
  83. * @return list<Node>
  84. */
  85. private function blocks(MdNode $container, bool $tight = false): array
  86. {
  87. $out = [];
  88. foreach ($container->children() as $child) {
  89. if ($child instanceof Paragraph) {
  90. $inlines = $this->inlines($child);
  91. if ($tight) {
  92. array_push($out, ...$inlines);
  93. continue;
  94. }
  95. $previous = end($out);
  96. if ($this->onlyNotes($inlines) && $previous instanceof Element && $previous->tag === 'p') {
  97. array_push($previous->children, ...$this->trimWhitespace($inlines));
  98. continue;
  99. }
  100. if ($inlines !== []) {
  101. $out[] = new Element('p', [], $inlines);
  102. }
  103. continue;
  104. }
  105. array_push($out, ...$this->block($child));
  106. }
  107. return $out;
  108. }
  109. /**
  110. * @return list<Node>
  111. */
  112. private function block(MdNode $node): array
  113. {
  114. return match (true) {
  115. $node instanceof Heading => [$this->heading($node)],
  116. $node instanceof BlockQuote => [new Element('blockquote', [], $this->blocks($node))],
  117. $node instanceof ListBlock => [$this->listBlock($node)],
  118. $node instanceof FencedCode => [$this->fencedCode($node)],
  119. $node instanceof IndentedCode => [$this->code($node->getLiteral(), '')],
  120. $node instanceof HtmlBlock => HtmlFragment::parseBlock($node->getLiteral(), $this->expandText(...)),
  121. $node instanceof ThematicBreak => [new Element('hr')],
  122. $node instanceof Table => [new Element('table', [], $this->tableChildren($node))],
  123. default => $this->blocks($node),
  124. };
  125. }
  126. private function heading(Heading $node): Element
  127. {
  128. $this->headingCount++;
  129. $id = 'h-'.substr(md5($this->headingSeed), 0, 8).'-'.$this->headingCount;
  130. return new Element('h'.$node->getLevel(), ['id' => $id], $this->inlines($node));
  131. }
  132. private function listBlock(ListBlock $node): Element
  133. {
  134. $data = $node->getListData();
  135. $ordered = $data->type === ListBlock::TYPE_ORDERED;
  136. $attrs = $ordered && $data->start !== null && $data->start !== 1 ? ['start' => (string) $data->start] : [];
  137. $items = [];
  138. foreach ($node->children() as $item) {
  139. if ($item instanceof ListItem) {
  140. $items[] = $this->listItem($this->blocks($item, $node->isTight()));
  141. }
  142. }
  143. return new Element($ordered ? 'ol' : 'ul', $attrs, $items);
  144. }
  145. /**
  146. * 列表项里只有一个 `<div>` 时变成 toggle(v2 react 格式的后处理,前端渲染成可展开的树)。
  147. *
  148. * @param list<Node> $children
  149. */
  150. private function listItem(array $children): Element
  151. {
  152. $significant = $this->trimWhitespace($children);
  153. if (count($significant) === 1 && $significant[0] instanceof Element && $significant[0]->tag === 'div') {
  154. $toggle = ($this->makeCall)('toggle', [], '');
  155. $toggle->slots['content'] = new Document($significant, false, '', [], $this->scope);
  156. return new Element('li', [], [$toggle]);
  157. }
  158. return new Element('li', [], $children);
  159. }
  160. private function fencedCode(FencedCode $node): Node
  161. {
  162. $words = $node->getInfoWords();
  163. $language = strtolower($words[0] ?? '');
  164. $literal = AnnotationToken::strip(Placeholder::restore($node->getLiteral(), $this->raws));
  165. if ($language === 'mermaid') {
  166. return ($this->makeCall)('mermaid', [rtrim($literal, "\n")], "```mermaid\n{$literal}```");
  167. }
  168. return $this->code($literal, $language);
  169. }
  170. private function code(string $literal, string $language): Element
  171. {
  172. $literal = AnnotationToken::strip(Placeholder::restore($literal, $this->raws));
  173. $attrs = $language !== '' ? ['class' => 'language-'.$language] : [];
  174. return new Element('pre', [], [new Element('code', $attrs, [new Text($literal)])]);
  175. }
  176. /**
  177. * @return list<Node>
  178. */
  179. private function tableChildren(Table $table): array
  180. {
  181. $sections = [];
  182. foreach ($table->children() as $section) {
  183. if (! $section instanceof TableSection) {
  184. continue;
  185. }
  186. $rows = [];
  187. foreach ($section->children() as $row) {
  188. if (! $row instanceof TableRow) {
  189. continue;
  190. }
  191. $cells = [];
  192. foreach ($row->children() as $cell) {
  193. if ($cell instanceof TableCell) {
  194. $tag = $cell->getType() === TableCell::TYPE_HEADER ? 'th' : 'td';
  195. $attrs = $cell->getAlign() !== null ? ['align' => $cell->getAlign()] : [];
  196. $cells[] = new Element($tag, $attrs, $this->inlines($cell));
  197. }
  198. }
  199. $rows[] = new Element('tr', [], $cells);
  200. }
  201. $sections[] = new Element($section->isHead() ? 'thead' : 'tbody', [], $rows);
  202. }
  203. return $sections;
  204. }
  205. /**
  206. * @return list<Node>
  207. */
  208. private function inlines(MdNode $container): array
  209. {
  210. $assembler = new InlineHtmlAssembler;
  211. foreach ($container->children() as $child) {
  212. if ($child instanceof HtmlInline) {
  213. $assembler->tag($child->getLiteral());
  214. continue;
  215. }
  216. foreach ($this->inline($child) as $node) {
  217. $assembler->add($node);
  218. }
  219. }
  220. return $assembler->nodes();
  221. }
  222. /**
  223. * @return list<Node>
  224. */
  225. private function inline(MdNode $node): array
  226. {
  227. return match (true) {
  228. $node instanceof PlaceholderNode => [$this->call($node->index)],
  229. $node instanceof AnnotationTokenNode => $this->annotationNode($node->index),
  230. $node instanceof MdText => [new Text($node->getLiteral())],
  231. $node instanceof Newline => $node->getType() === Newline::HARDBREAK ? [new Element('br')] : [new Text("\n")],
  232. $node instanceof Code => [new Element('code', [], [new Text(AnnotationToken::strip(Placeholder::restore($node->getLiteral(), $this->raws)))])],
  233. $node instanceof Emphasis => [new Element('em', [], $this->inlines($node))],
  234. $node instanceof Strong => [new Element('strong', [], $this->inlines($node))],
  235. $node instanceof Strikethrough => [new Element('del', [], $this->inlines($node))],
  236. $node instanceof Link => [$this->link($node)],
  237. $node instanceof Image => [$this->image($node)],
  238. $node instanceof TaskListItemMarker => [new Element('input', array_filter([
  239. 'type' => 'checkbox',
  240. 'disabled' => 'disabled',
  241. 'checked' => $node->isChecked() ? 'checked' : null,
  242. ]))],
  243. default => $this->inlines($node),
  244. };
  245. }
  246. private function link(Link $node): Element
  247. {
  248. $attrs = [];
  249. $url = HtmlPolicy::url(AnnotationToken::strip(Placeholder::restore($node->getUrl(), $this->raws)));
  250. if ($url !== null) {
  251. $attrs['href'] = $url;
  252. }
  253. if (($node->getTitle() ?? '') !== '') {
  254. $attrs['title'] = (string) $node->getTitle();
  255. }
  256. return new Element('a', $attrs, $this->inlines($node));
  257. }
  258. private function image(Image $node): Element
  259. {
  260. $attrs = [];
  261. $url = HtmlPolicy::url(AnnotationToken::strip(Placeholder::restore($node->getUrl(), $this->raws)));
  262. if ($url !== null) {
  263. $attrs['src'] = $url;
  264. }
  265. $attrs['alt'] = $this->plainText($node);
  266. if (($node->getTitle() ?? '') !== '') {
  267. $attrs['title'] = (string) $node->getTitle();
  268. }
  269. return new Element('img', $attrs);
  270. }
  271. private function plainText(MdNode $node): string
  272. {
  273. $text = '';
  274. foreach ($node->iterator() as $child) {
  275. if ($child instanceof MdText || $child instanceof Code) {
  276. $text .= $child->getLiteral();
  277. }
  278. }
  279. return AnnotationToken::strip(Placeholder::restore($text, $this->raws));
  280. }
  281. /**
  282. * @return list<Node>
  283. */
  284. private function annotationNode(int $index): array
  285. {
  286. $node = $this->annotation === null ? null : ($this->annotation)($index);
  287. return $node === null ? [] : [$node];
  288. }
  289. private function call(int $index): Node
  290. {
  291. return $this->calls[$index] ?? new Text('');
  292. }
  293. /**
  294. * HTML 块里的文字:展开模板占位符(`<nt>{{65-464-2-27}}</nt>` 里的句子模板照常渲染)。
  295. *
  296. * @return list<Node>
  297. */
  298. private function expandText(string $text): array
  299. {
  300. $nodes = [];
  301. $parts = preg_split(Placeholder::PATTERN, $text, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
  302. foreach ($parts as $i => $part) {
  303. if ($i % 2 === 1) {
  304. $nodes[] = $this->call((int) $part);
  305. continue;
  306. }
  307. // 批注占位符
  308. $pieces = preg_split(AnnotationToken::PATTERN, $part, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
  309. foreach ($pieces as $j => $piece) {
  310. if ($j % 2 === 1) {
  311. array_push($nodes, ...$this->annotationNode((int) $piece));
  312. } elseif ($piece !== '') {
  313. $nodes[] = new Text($piece);
  314. }
  315. }
  316. }
  317. return $nodes;
  318. }
  319. /**
  320. * @param list<Node> $nodes
  321. */
  322. private function onlyNotes(array $nodes): bool
  323. {
  324. $significant = $this->trimWhitespace($nodes);
  325. if ($significant === []) {
  326. return false;
  327. }
  328. foreach ($significant as $node) {
  329. if (! $node instanceof Call || ! $node->template instanceof NoteTemplate) {
  330. return false;
  331. }
  332. }
  333. return true;
  334. }
  335. /**
  336. * @param list<Node> $nodes
  337. * @return list<Node>
  338. */
  339. private function trimWhitespace(array $nodes): array
  340. {
  341. return array_values(array_filter(
  342. $nodes,
  343. fn (Node $node) => ! ($node instanceof Text && trim($node->value) === ''),
  344. ));
  345. }
  346. }