MarkupAnnotationTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. use App\Services\AnnotationLayout;
  3. use App\Services\HtmlAnnotationMarkup;
  4. use App\Services\Markup\Annotation\AnnotationSet;
  5. use App\Services\Markup\Annotation\AnnotationToken;
  6. use App\Services\Markup\Format;
  7. use App\Services\Markup\MarkupService;
  8. use App\Services\Markup\RenderOptions;
  9. /**
  10. * 阅读页批注的结构化注入:AnnotationLayout 用 AnnotationSet 作输出策略,插入点写占位符,
  11. * App\Services\Markup 解析成 anno-* / note 模板节点,各格式都能输出。
  12. */
  13. /**
  14. * 排版 + 渲染一句。
  15. *
  16. * @param array<string, mixed> $layout AnnotationLayout::apply 的 glossaries / issues / notes / assessment
  17. */
  18. function annotated(string $content, array $layout, Format $format = Format::Tree, ?callable $notes = null): array|string
  19. {
  20. $set = new AnnotationSet;
  21. $noteSpecs = $notes ? $notes($set) : [];
  22. $result = AnnotationLayout::apply(
  23. $content,
  24. $layout['glossaries'] ?? [],
  25. $layout['issues'] ?? [],
  26. $noteSpecs,
  27. true,
  28. $layout['assessment'] ?? null,
  29. $set,
  30. );
  31. $rendered = app(MarkupService::class)->renderMany(
  32. [['source' => $result['content'], 'annotations' => $set]],
  33. new RenderOptions(format: $format),
  34. )[0];
  35. return is_array($rendered) ? json_decode(json_encode($rendered), true) : $rendered;
  36. }
  37. it('wraps an issue range as an anno-issue node in the tree', function () {
  38. $tree = annotated('first sentence', ['issues' => [['id' => 'i1', 'start' => 0, 'end' => 5, 'severity' => 'error']]]);
  39. expect($tree[0])->toMatchArray(['type' => 'tpl', 'name' => 'anno-issue', 'props' => ['ids' => ['i1'], 'severity' => 'error']]);
  40. expect($tree[0]['children'])->toBe([['type' => 'text', 'value' => 'first']]);
  41. expect($tree[1])->toBe(['type' => 'text', 'value' => ' sentence']);
  42. });
  43. it('renders the same html as the legacy injection', function () {
  44. $layout = [
  45. 'issues' => [
  46. ['id' => 'i1', 'start' => 0, 'end' => 5, 'severity' => 'warning'],
  47. ['id' => 'i2', 'start' => null, 'end' => null, 'severity' => 'error'],
  48. ],
  49. 'assessment' => ['id' => 'a1', 'score' => 85],
  50. ];
  51. $html = annotated('first sentence', $layout, Format::Html);
  52. $markup = new HtmlAnnotationMarkup;
  53. expect($html)->toBe('<span>'
  54. .HtmlAnnotationMarkup::issueOpenTag(['i1'], 'warning').'first</span> sentence'
  55. .$markup->issueMark(['i2'], 'error')
  56. .$markup->assessmentMark('a1', 85)
  57. .'</span>');
  58. });
  59. it('keeps highlighted text and drops icons in plain formats', function () {
  60. $layout = [
  61. 'issues' => [['id' => 'i1', 'start' => 0, 'end' => 5, 'severity' => null], ['id' => 'i2', 'start' => null, 'end' => null, 'severity' => null]],
  62. 'assessment' => ['id' => 'a1', 'score' => 30],
  63. ];
  64. expect(annotated('first sentence', $layout, Format::Text))->toBe('first sentence');
  65. expect(annotated('first sentence', $layout, Format::Markdown))->toBe('first sentence');
  66. });
  67. it('exposes assessments and issue marks as props in the tree', function () {
  68. $tree = annotated('句子', [
  69. 'issues' => [['id' => 'i2', 'start' => null, 'end' => null, 'severity' => 'suggestion']],
  70. 'assessment' => ['id' => 'a1', 'score' => 65],
  71. ]);
  72. expect($tree[1])->toMatchArray(['name' => 'anno-issue-mark', 'props' => ['ids' => ['i2'], 'severity' => 'suggestion']]);
  73. expect($tree[2])->toMatchArray(['name' => 'anno-assessment', 'props' => ['id' => 'a1', 'score' => 65, 'level' => 'medium']]);
  74. });
  75. it('renders the unassessed mark as a level-none assessment', function () {
  76. $set = new AnnotationSet;
  77. $source = '句子'.$set->unassessedMark();
  78. $render = fn (Format $f) => app(MarkupService::class)->renderMany([['source' => $source, 'annotations' => $set]], new RenderOptions(format: $f))[0];
  79. expect(json_decode(json_encode($render(Format::Tree)), true)[1])->toMatchArray(['name' => 'anno-assessment', 'props' => ['level' => 'none']]);
  80. expect($render(Format::Html))->toBe('<span>句子'.HtmlAnnotationMarkup::unassessedMark().'</span>');
  81. });
  82. it('lifts an issue that crosses an emphasis boundary to wrap the whole element', function () {
  83. // 「*斜体*」不在 AnnotationLayout 的受保护区间里:区间 [3, 8) 从斜体中间开始
  84. $content = 'ab *cdef* gh';
  85. $tree = annotated($content, ['issues' => [['id' => 'i1', 'start' => 5, 'end' => 10, 'severity' => null]]]);
  86. $issue = collect($tree)->firstWhere('name', 'anno-issue');
  87. expect($issue)->not->toBeNull();
  88. expect(collect($issue['children'])->pluck('tag')->filter()->values()->all())->toBe(['em']);
  89. expect(json_encode($tree, JSON_UNESCAPED_UNICODE))->not->toContain("\u{E002}");
  90. });
  91. it('injects notes as structured note nodes with markdown and templates kept', function () {
  92. $tree = annotated('first sentence', [], Format::Tree, fn (AnnotationSet $set) => [
  93. ['id' => 'n1', 'at' => 5, 'order' => [0, 0, 0], 'html' => $set->note('见 **粗体** 与 [[dhamma]]', ['cite' => '义注', 'citelink' => '1-2-3-4'])],
  94. ]);
  95. expect($tree[1])->toMatchArray(['name' => 'note', 'props' => ['cite' => '义注', 'citelink' => '1-2-3-4']]);
  96. expect(collect($tree[1]['children'])->pluck('tag')->filter()->values()->all())->toBe(['strong']);
  97. expect(collect($tree[1]['children'])->firstWhere('name', 'term')['props']['word'])->toBe('dhamma');
  98. });
  99. it('renders note sidenotes with only inline html even for block content', function () {
  100. $html = annotated('first sentence', [], Format::Html, fn (AnnotationSet $set) => [
  101. ['id' => 'n1', 'at' => 5, 'order' => [0, 0, 0], 'html' => $set->note("1. 第一点\n2. 第二点\n\n另起一段")],
  102. ]);
  103. expect($html)->toContain('<span class="sidenote">')
  104. ->not->toContain('<ol')->not->toContain('<p>')
  105. ->toContain('<span class="md-ol"><span class="md-li">第一点</span>')
  106. ->toContain('<br /><span class="md-p">另起一段</span>');
  107. });
  108. it('renders notes as markdown footnotes', function () {
  109. $markdown = app(MarkupService::class)->renderMany(
  110. [['source' => 'first'.($set = new AnnotationSet)->note('注释正文').' sentence', 'annotations' => $set]],
  111. new RenderOptions(format: Format::Markdown, footnote: true),
  112. )[0];
  113. expect($markdown)->toBe("first[^1] sentence\n\n[^1]: 注释正文");
  114. });
  115. it('parses the note body in its own dialect', function () {
  116. $tree = annotated('句子', [], Format::Tree, fn (AnnotationSet $set) => [
  117. ['id' => 'n1', 'at' => 2, 'order' => [0, 0, 0], 'html' => $set->note('用 `code`', [], 'markdown-e2')],
  118. ]);
  119. expect($tree[1]['children'][1])->toMatchArray(['type' => 'el', 'tag' => 'code']);
  120. });
  121. it('gives sidenotes page-unique ids from the item id prefix', function () {
  122. $html = app(MarkupService::class)->renderMany([
  123. ['source' => 'a{{n|x}}', 'idPrefix' => '65-1-2-3'],
  124. ['source' => 'b{{n|y}}', 'idPrefix' => '65-1-4-5'],
  125. ], new RenderOptions(format: Format::Html));
  126. expect($html[0])->toContain('id="sn-65-1-2-3-1"')->and($html[1])->toContain('id="sn-65-1-4-5-1"');
  127. });
  128. it('ignores annotation tokens and internal template names written in content', function () {
  129. $stray = '正文'.AnnotationToken::make(0).'{{anno-issue|ids=x}}';
  130. $tree = json_decode(json_encode(app(MarkupService::class)->render($stray, new RenderOptions)), true);
  131. expect($tree[0])->toBe(['type' => 'text', 'value' => '正文']);
  132. expect($tree[1])->toMatchArray(['name' => 'unknown']);
  133. });