MarkupSyntaxTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. use App\Services\Markup\Format;
  3. use App\Services\Markup\MarkupService;
  4. use App\Services\Markup\Parsing\CjkFriendlyEmphasisParser;
  5. use App\Services\Markup\RenderOptions;
  6. /**
  7. * App\Services\Markup 的语法测试:标准 markdown、原始 HTML 白名单、全部模板写法、两种方言、各输出格式。
  8. * 这里的用例都不需要库里的数据(术语、句子等取数见 MarkupTemplateDataTest)。
  9. */
  10. /**
  11. * tree 输出,转成纯数组便于断言。
  12. *
  13. * @return list<array<string, mixed>>
  14. */
  15. function markupTree(string $source, array $options = []): array
  16. {
  17. $rendered = app(MarkupService::class)->render($source, new RenderOptions(...$options));
  18. return json_decode(json_encode($rendered), true);
  19. }
  20. function markupRender(string $source, Format $format, array $options = []): string
  21. {
  22. return app(MarkupService::class)->render($source, new RenderOptions(...array_merge($options, ['format' => $format])));
  23. }
  24. function markupE2(string $source): array
  25. {
  26. return markupTree($source, ['contentType' => 'markdown-e2']);
  27. }
  28. /**
  29. * 树里所有模板节点(深度优先,含 props 里嵌套的)。
  30. *
  31. * @return list<array<string, mixed>>
  32. */
  33. function markupTemplates(array $nodes): array
  34. {
  35. $found = [];
  36. foreach ($nodes as $node) {
  37. if (! is_array($node)) {
  38. continue;
  39. }
  40. if (($node['type'] ?? null) === 'tpl') {
  41. $found[] = $node;
  42. }
  43. foreach (['children', 'props'] as $key) {
  44. if (isset($node[$key]) && is_array($node[$key])) {
  45. array_push($found, ...markupTemplates(array_values($node[$key])));
  46. }
  47. }
  48. if (! isset($node['type'])) {
  49. array_push($found, ...markupTemplates(array_values($node)));
  50. }
  51. }
  52. return $found;
  53. }
  54. function markupText(array $nodes): string
  55. {
  56. $text = '';
  57. foreach ($nodes as $node) {
  58. if ($node['type'] === 'text') {
  59. $text .= $node['value'];
  60. } else {
  61. $text .= markupText($node['children'] ?? []);
  62. }
  63. }
  64. return $text;
  65. }
  66. describe('standard markdown', function () {
  67. it('renders a single paragraph as inline nodes without a p wrapper', function () {
  68. expect(markupTree('hello *world*'))->toBe([
  69. ['type' => 'text', 'value' => 'hello '],
  70. ['type' => 'el', 'tag' => 'em', 'children' => [['type' => 'text', 'value' => 'world']]],
  71. ]);
  72. });
  73. it('renders headings h1 to h6 with stable ids', function () {
  74. $tree = markupTree("# a\n\n## b\n\n### c\n\n#### d\n\n##### e\n\n###### f");
  75. expect(array_column($tree, 'tag'))->toBe(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
  76. expect($tree[0]['attrs']['id'])->toMatch('/^h-[0-9a-f]{8}-1$/');
  77. expect(markupTree("# a\n\n## b")[0]['attrs']['id'])->not->toBe(markupTree("# x\n\n## b")[0]['attrs']['id']);
  78. expect(markupTree("# a\n\n## b"))->toBe(markupTree("# a\n\n## b"));
  79. });
  80. it('renders emphasis, strong, strikethrough and nesting', function () {
  81. $tree = markupTree('*em* **strong** ~~del~~ ***both***');
  82. expect(array_values(array_filter(array_column($tree, 'tag'))))->toBe(['em', 'strong', 'del', 'em']);
  83. expect($tree[6]['children'][0]['tag'])->toBe('strong');
  84. });
  85. it('renders links and images and drops unsafe urls', function () {
  86. $tree = markupTree('[site](https://wikipali.org "t") [bad](javascript:alert(1)) ![pic](/attachments/a.png)');
  87. expect($tree[0])->toMatchArray(['tag' => 'a', 'attrs' => ['href' => 'https://wikipali.org', 'title' => 't']]);
  88. expect($tree[2]['tag'])->toBe('a')->and($tree[2])->not->toHaveKey('attrs');
  89. expect($tree[4])->toMatchArray(['tag' => 'img', 'attrs' => ['src' => '/attachments/a.png', 'alt' => 'pic']]);
  90. });
  91. it('prefixes site images with the app url in html', function () {
  92. config(['app.url' => 'https://example.test']);
  93. expect(markupRender('![pic](/attachments/a.png)', Format::Html))
  94. ->toBe('<span><img src="https://example.test/attachments/a.png" alt="pic" /></span>');
  95. });
  96. it('renders autolinks', function () {
  97. expect(markupTree('see https://wikipali.org now')[1])
  98. ->toMatchArray(['tag' => 'a', 'attrs' => ['href' => 'https://wikipali.org']]);
  99. });
  100. it('renders hard and soft line breaks', function () {
  101. $tree = markupTree("a \nb\nc");
  102. expect($tree[1]['tag'])->toBe('br');
  103. expect($tree[2])->toBe(['type' => 'text', 'value' => "b\nc"]);
  104. });
  105. it('renders block quotes', function () {
  106. $tree = markupTree("> quoted\n>\n> two");
  107. expect($tree[0]['tag'])->toBe('blockquote')
  108. ->and(array_column($tree[0]['children'], 'tag'))->toBe(['p', 'p']);
  109. });
  110. it('renders tight, loose, ordered and nested lists', function () {
  111. $tight = markupTree("- a\n- b\n - c");
  112. expect($tight[0]['tag'])->toBe('ul');
  113. expect($tight[0]['children'][0]['children'][0])->toBe(['type' => 'text', 'value' => 'a']);
  114. expect($tight[0]['children'][1]['children'][1]['tag'])->toBe('ul');
  115. $loose = markupTree("- a\n\n- b");
  116. expect($loose[0]['children'][0]['children'][0]['tag'])->toBe('p');
  117. $ordered = markupTree("3. x\n4. y");
  118. expect($ordered[0])->toMatchArray(['tag' => 'ol', 'attrs' => ['start' => '3']]);
  119. });
  120. it('renders task lists', function () {
  121. $tree = markupTree("- [x] done\n- [ ] todo");
  122. expect($tree[0]['children'][0]['children'][0])
  123. ->toMatchArray(['tag' => 'input', 'attrs' => ['type' => 'checkbox', 'disabled' => 'disabled', 'checked' => 'checked']]);
  124. expect($tree[0]['children'][1]['children'][0]['attrs'])->not->toHaveKey('checked');
  125. });
  126. it('renders tables with alignment', function () {
  127. $tree = markupTree("| a | b |\n|:--|--:|\n| 1 | 2 |");
  128. expect($tree[0]['tag'])->toBe('table');
  129. [$head, $body] = $tree[0]['children'];
  130. expect($head['tag'])->toBe('thead')->and($body['tag'])->toBe('tbody');
  131. expect($head['children'][0]['children'][0])->toMatchArray(['tag' => 'th', 'attrs' => ['align' => 'left']]);
  132. expect($body['children'][0]['children'][1])->toMatchArray(['tag' => 'td', 'attrs' => ['align' => 'right']]);
  133. });
  134. it('renders thematic breaks', function () {
  135. expect(array_column(markupTree("a\n\n---\n\nb"), 'tag'))->toBe(['p', 'hr', 'p']);
  136. });
  137. it('renders indented code literally', function () {
  138. $tree = markupTree("para\n\n {{note|x}} *y*");
  139. expect($tree[1]['tag'])->toBe('pre');
  140. expect($tree[1]['children'][0]['children'][0]['value'])->toBe("{{note|x}} *y*\n");
  141. });
  142. it('escapes text in html output', function () {
  143. expect(markupRender('a < b & "c"', Format::Html))->toBe('<span>a &lt; b &amp; &quot;c&quot;</span>');
  144. });
  145. it('returns empty output for empty content', function () {
  146. expect(markupTree(''))->toBe([]);
  147. expect(markupRender(" \n", Format::Html))->toBe('');
  148. });
  149. });
  150. describe('raw html', function () {
  151. it('keeps allowed inline tags and nests them', function () {
  152. $tree = markupTree('a <u>under <b>bold</b></u> 10<sup>12</sup>');
  153. expect($tree[1])->toMatchArray(['tag' => 'u']);
  154. expect($tree[1]['children'][1]['tag'])->toBe('b');
  155. expect($tree[3]['tag'])->toBe('sup');
  156. });
  157. it('keeps br as a void element', function () {
  158. expect(markupTree('a<br>b')[1])->toBe(['type' => 'el', 'tag' => 'br', 'children' => []]);
  159. });
  160. it('turns unknown tags into spans with data-tag', function () {
  161. $tree = markupTree('<r>(abhi-ṭṭha-1-291)</r>');
  162. expect($tree[0])->toMatchArray(['tag' => 'span', 'attrs' => ['data-tag' => 'r']]);
  163. expect($tree[0]['children'][0]['value'])->toBe('(abhi-ṭṭha-1-291)');
  164. });
  165. it('drops scripts, iframes and svg with their content', function () {
  166. $tree = markupTree('a<script>alert(1)</script>b<iframe src="x"></iframe>c');
  167. expect(markupText($tree))->toBe('abc');
  168. $block = markupTree("<svg><text>x</text></svg>\n\nafter");
  169. expect(markupText($block))->toBe('after');
  170. });
  171. it('drops event handlers and styles', function () {
  172. $tree = markupTree('<span class="k" style="color:red" onclick="x()">t</span>');
  173. expect($tree[0]['attrs'])->toBe(['class' => 'k']);
  174. });
  175. it('drops javascript links in raw html', function () {
  176. $tree = markupTree('<a href="javascript:alert(1)">x</a> <a href="JaVa script:alert(1)">y</a>');
  177. expect($tree[0])->not->toHaveKey('attrs');
  178. });
  179. it('parses html blocks through the dom', function () {
  180. $tree = markupTree("<div class=\"box\">\n<p>inside</p>\n</div>");
  181. expect($tree[0])->toMatchArray(['tag' => 'div', 'attrs' => ['class' => 'box']]);
  182. expect($tree[0]['children'][1]['tag'])->toBe('p');
  183. });
  184. it('renders templates inside html blocks', function () {
  185. $tree = markupTree("<nt>\n{{note|inner}}\n</nt>");
  186. expect($tree[0]['attrs']['data-tag'])->toBe('nt');
  187. expect(markupTemplates($tree)[0]['name'])->toBe('note');
  188. });
  189. });
  190. describe('templates: syntax', function () {
  191. it('parses named and positional params, counting named ones as positions', function () {
  192. $tree = markupTree('{{note|text=body|②}}');
  193. expect($tree[0])->toMatchArray(['type' => 'tpl', 'name' => 'note', 'props' => ['trigger' => '②']]);
  194. expect($tree[0]['children'])->toBe([['type' => 'text', 'value' => 'body']]);
  195. });
  196. it('drops the newline right after a pipe', function () {
  197. $tree = markupTree("{{note|\nbody}}");
  198. expect($tree[0]['children'])->toBe([['type' => 'text', 'value' => 'body']]);
  199. });
  200. it('nests templates in params', function () {
  201. $tree = markupTree('{{note|see {{note|deeper}} and [[dhamma]]}}');
  202. $names = array_column(markupTemplates($tree), 'name');
  203. expect($names)->toBe(['note', 'note', 'term']);
  204. });
  205. it('keeps an unclosed {{ as text', function () {
  206. expect(markupText(markupTree('a {{note|b')))->toBe('a {{note|b');
  207. });
  208. it('keeps backslash-escaped braces as text', function () {
  209. expect(markupTree('\{{note|x}}'))->toBe([['type' => 'text', 'value' => '{{note|x}}']]);
  210. });
  211. it('renders unknown templates as an unknown component and as source text', function () {
  212. $tree = markupTree('{{nosuch|a}}');
  213. expect($tree[0])->toMatchArray(['name' => 'unknown', 'props' => ['name' => 'nosuch', 'source' => '{{nosuch|a}}']]);
  214. expect(markupRender('{{nosuch|a}}', Format::Text))->toBe('{{nosuch|a}}');
  215. });
  216. it('keeps template pipes from splitting table cells', function () {
  217. $tree = markupTree("| a | b |\n|---|---|\n| {{note|x|y}} | 2 |");
  218. $cells = $tree[0]['children'][1]['children'][0]['children'];
  219. expect($cells)->toHaveCount(2);
  220. expect($cells[0]['children'][0])->toMatchArray(['name' => 'note', 'props' => ['trigger' => 'y']]);
  221. });
  222. it('recognizes the sentence shorthand and the sent template', function () {
  223. $short = markupTree('{{65-464-2-27}}');
  224. $long = markupTree('{{sent|id=65-464-2-27}}');
  225. expect($short[0])->toMatchArray(['type' => 'tpl', 'name' => 'sentread']);
  226. expect($short[0]['props']['id'])->toBe('65-464-2-27');
  227. expect($long[0]['props']['id'])->toBe('65-464-2-27');
  228. expect(markupTree('{{65-464-2-27}}', ['mode' => 'edit'])[0]['name'])->toBe('sentedit');
  229. expect(markupTree('{{sent|id=65-464-2-27|mode=edit}}')[0]['name'])->toBe('sentedit');
  230. });
  231. it('reports an invalid sentence id', function () {
  232. expect(markupTree('{{1-2-3}}')[0]['props']['error'])->toBe('invalid sentence id');
  233. });
  234. });
  235. describe('footnotes', function () {
  236. it('accepts note and n', function () {
  237. expect(markupTree('{{n|short}}')[0]['name'])->toBe('note');
  238. expect(markupTree('{{note|short}}')[0]['name'])->toBe('note');
  239. });
  240. it('allows line breaks, blank lines and blocks inside an inline note', function () {
  241. $tree = markupTree("正文{{n|第一段\n\n- 列表\n- 两项}}继续正文");
  242. expect($tree[0]['type'])->toBe('text')->and($tree[2])->toBe(['type' => 'text', 'value' => '继续正文']);
  243. $note = $tree[1];
  244. expect($note['name'])->toBe('note');
  245. expect(array_column($note['children'], 'tag'))->toBe(['p', 'ul']);
  246. });
  247. it('joins a note fence into the sentence around it (CJK: no space)', function () {
  248. $tree = markupTree("前半句\n```note\ncontent\n```\n后半句");
  249. expect($tree)->toHaveCount(3);
  250. expect($tree[0])->toBe(['type' => 'text', 'value' => '前半句']);
  251. expect($tree[1])->toMatchArray(['type' => 'tpl', 'name' => 'note']);
  252. expect($tree[2])->toBe(['type' => 'text', 'value' => '后半句']);
  253. });
  254. it('joins a note fence with a space between latin words', function () {
  255. $tree = markupTree("first half\n```note\ncontent\n```\nsecond half");
  256. expect($tree[0]['value'])->toBe('first half');
  257. expect($tree[2]['value'])->toBe(' second half');
  258. });
  259. it('keeps blank lines and pipes inside a note fence', function () {
  260. $tree = markupTree("文\n```note\n第一段 a|b }}\n\n第二段\n```");
  261. $note = $tree[1];
  262. expect(array_column($note['children'], 'tag'))->toBe(['p', 'p']);
  263. expect($note['children'][0]['children'][0]['value'])->toBe('第一段 a|b }}');
  264. });
  265. it('attaches a note fence surrounded by blank lines to the end of the previous paragraph', function () {
  266. $tree = markupTree("一段话。\n\n```note\n整段注\n```\n\n下一段");
  267. expect(array_column($tree, 'tag'))->toBe(['p', 'p']);
  268. expect(end($tree[0]['children']))->toMatchArray(['type' => 'tpl', 'name' => 'note']);
  269. });
  270. it('reads trigger from the note fence info string', function () {
  271. expect(markupTree("a\n```note trigger=\"①\"\nx\n```")[1]['props'])->toBe(['trigger' => '①']);
  272. });
  273. it('supports a longer fence so that the note can contain code', function () {
  274. $tree = markupTree("文\n````note\n```\ncode\n```\n````", ['contentType' => 'markdown-e2']);
  275. expect($tree[1]['children'][0]['tag'])->toBe('pre');
  276. });
  277. });
  278. describe('legacy dialect (markdown)', function () {
  279. it('treats `x` as a single-line note', function () {
  280. $tree = markupTree('依法`这里是注释`,故为');
  281. expect($tree[1])->toMatchArray(['name' => 'note']);
  282. expect($tree[1]['children'][0]['value'])->toBe('这里是注释');
  283. });
  284. it('treats a bare ``` fence as a multi-line note joined into the sentence', function () {
  285. $tree = markupTree("[[bodhisatta]]讲述。\n```\n或译为:亲依止缘。\n```");
  286. expect(array_column(markupTemplates($tree), 'name'))->toBe(['term', 'note']);
  287. });
  288. it('also accepts ```note', function () {
  289. expect(markupTree("a\n```note\nb\n```")[1]['name'])->toBe('note');
  290. });
  291. it('still treats ```lang fences as code', function () {
  292. $tree = markupTree("```python\nx = 1\n```");
  293. expect($tree[0]['tag'])->toBe('pre');
  294. expect($tree[0]['children'][0])->toMatchArray(['tag' => 'code', 'attrs' => ['class' => 'language-python']]);
  295. });
  296. it('treats any lowercase name= segment as a named param', function () {
  297. expect(markupTree('{{note|x=1}}')[0]['children'])->toBe([]);
  298. });
  299. });
  300. describe('E2 dialect (markdown-e2)', function () {
  301. it('renders backticks as code, not notes', function () {
  302. $tree = markupE2('use `{{note|x}}` and `[[term]]`');
  303. expect($tree[1])->toMatchArray(['tag' => 'code', 'children' => [['type' => 'text', 'value' => '{{note|x}}']]]);
  304. expect($tree[3]['children'][0]['value'])->toBe('[[term]]');
  305. expect(markupTemplates($tree))->toBe([]);
  306. });
  307. it('renders bare fences as code', function () {
  308. $tree = markupE2("```\n{{note|x}}\n```");
  309. expect($tree[0]['tag'])->toBe('pre');
  310. expect($tree[0]['children'][0]['children'][0]['value'])->toBe("{{note|x}}\n");
  311. });
  312. it('keeps pipes and a=b inside note content', function () {
  313. $tree = markupE2('{{n|含 | 竖线 a=b|trigger=①}}');
  314. expect($tree[0]['props'])->toBe(['trigger' => '①']);
  315. expect($tree[0]['children'][0]['value'])->toBe('含 | 竖线 a=b');
  316. });
  317. it('only treats declared names as named params', function () {
  318. $tree = markupE2('{{quality|x=featured}}');
  319. expect($tree[0]['props']['value'])->toBe('pending');
  320. expect(markupE2('{{quality|value=featured}}')[0]['props']['value'])->toBe('featured');
  321. });
  322. it('still supports note fences', function () {
  323. expect(markupE2("a\n```note\nb\n```\nc")[1]['name'])->toBe('note');
  324. });
  325. });
  326. describe('templates without data', function () {
  327. it('renders mermaid fences in both dialects', function () {
  328. foreach (['markdown', 'markdown-e2'] as $type) {
  329. $tree = markupTree("```mermaid\ngraph TD\nA-->B\n```", ['contentType' => $type]);
  330. expect($tree[0])->toMatchArray(['type' => 'tpl', 'name' => 'mermaid', 'props' => ['text' => "graph TD\nA-->B"]]);
  331. }
  332. });
  333. it('renders mermaid per format', function () {
  334. $source = "```mermaid\ngraph TD\nA-->B\n```";
  335. expect(markupRender($source, Format::Html))->toBe("<pre class=\"mermaid\">graph TD\nA--&gt;B</pre>");
  336. expect(markupRender($source, Format::Text))->toBe('mermaid');
  337. expect(markupRender($source, Format::Prompt))->toBe("```mermaid\ngraph TD\nA-->B\n```");
  338. expect(markupRender($source, Format::Markdown))->toBe($source);
  339. });
  340. it('renders quality and falls back to pending', function () {
  341. expect(markupTree('{{quality|featured}}')[0]['props'])->toBe(['value' => 'featured']);
  342. expect(markupTree('{{quality|bogus}}')[0]['props'])->toBe(['value' => 'pending']);
  343. expect(markupRender('{{quality|draft}}', Format::Html))->toBe('<span><span class="tag-quality draft">draft</span></span>');
  344. });
  345. it('renders category, para, dict-pref and ai', function () {
  346. expect(markupTree('{{category|vinaya}}')[0])->toMatchArray(['name' => 'category', 'props' => ['name' => 'vinaya']]);
  347. expect(markupTree('{{para|id=160-9|title=aa|style=card}}', ['channels' => ['c1', 'c2']])[0]['props'])
  348. ->toBe(['id' => '160-9', 'title' => 'aa', 'style' => 'card', 'channel' => 'c1_c2']);
  349. expect(markupTree('{{dict-pref|2}}')[0]['props'])->toBe(['currPage' => 2, 'pageSize' => 100]);
  350. expect(markupTree('{{ai|gpt}}')[0]['props'])->toBe(['model' => 'gpt']);
  351. expect(markupRender('x{{ai|gpt}}', Format::Prompt))->toBe('x');
  352. expect(markupRender('x{{ai|gpt}}', Format::Text))->toBe('xai');
  353. });
  354. it('renders video and strips unsafe urls', function () {
  355. expect(markupTree('{{v|https://v.example/a.mp4}}')[0]['props'])
  356. ->toBe(['url' => 'https://v.example/a.mp4', 'title' => '', 'style' => 'modal']);
  357. // 显式给空值时用空值(v2 口径:参数存在就不取默认值)
  358. expect(markupTree('{{v|https://v.example/a.mp4||intro}}')[0]['props'])
  359. ->toBe(['url' => 'https://v.example/a.mp4', 'title' => 'intro', 'style' => '']);
  360. expect(markupTree('{{v|javascript:alert(1)}}')[0]['props']['url'])->toBe('');
  361. });
  362. it('renders exercise and article', function () {
  363. expect(markupTree('{{exercise|id=e1|title=Q1}}', ['channels' => ['c1']])[0]['props'])
  364. ->toBe(['id' => 'e1', 'title' => 'Q1', 'channel' => 'c1']);
  365. $article = markupTree('{{article|type=chapter|book=168|paragraphs=915|title=T}}')[0]['props'];
  366. expect($article)->toMatchArray(['type' => 'chapter', 'id' => '168-915', 'title' => 'T', 'book' => '168']);
  367. expect(markupRender('{{article|type=article|id=x|title=T}}', Format::Text))->toBe('T');
  368. expect(markupRender('{{article|type=article|id=x|title=T}}', Format::Prompt))->toBe('');
  369. });
  370. it('renders nissaya templates and nissaya channel lines', function () {
  371. expect(markupTree('{{nissaya|evaṃ|ဤသို့=thus}}')[0]['props'])
  372. ->toMatchArray(['pali' => 'evaṃ', 'meaning' => ['ဤသို့', 'thus']]);
  373. $lines = markupTree("evaṃ=ဤသို့\nme\$ငါ", ['channelType' => 'nissaya']);
  374. expect(array_column(markupTemplates($lines), 'name'))->toBe(['nissaya', 'nissaya']);
  375. expect(markupTemplates($lines)[1]['props']['pali'])->toBe('me');
  376. });
  377. it('renders nissaya json content', function () {
  378. $json = json_encode([
  379. ['original' => 'evaṃ', 'translation' => 'ဤသို့>thus', 'note' => 'n1'],
  380. ['sn' => [1], 'word' => ['value' => 'me'], 'meaning' => ['value' => 'ငါ']],
  381. ]);
  382. $tree = markupTree($json, ['channelType' => 'nissaya', 'contentType' => 'json']);
  383. expect(array_column(markupTemplates($tree), 'name'))->toBe(['nissaya', 'note', 'nissaya']);
  384. expect(markupTemplates($tree)[0]['props']['meaning'])->toBe(['ဤသို့', 'thus']);
  385. });
  386. it('turns a list item holding only a div into a toggle', function () {
  387. $tree = markupTree("- <div>title</div>\n- plain");
  388. $first = $tree[0]['children'][0]['children'][0];
  389. expect($first)->toMatchArray(['type' => 'tpl', 'name' => 'toggle']);
  390. expect($first['children'][0]['tag'])->toBe('div');
  391. expect($tree[0]['children'][1]['children'][0]['type'])->toBe('text');
  392. });
  393. it('renders a term without a channel as the plain word', function () {
  394. $tree = markupTree('[[dhamma]]');
  395. expect($tree[0])->toMatchArray(['name' => 'term', 'props' => ['word' => 'dhamma', 'parentChannelId' => null, 'parentStudioId' => null]]);
  396. expect($tree[0]['children'])->toBe([['type' => 'text', 'value' => 'dhamma']]);
  397. expect(markupTree('[[dhamma#法]]')[0]['props']['meaning'])->toBe('法');
  398. });
  399. });
  400. describe('output formats', function () {
  401. $source = "# 标题\n\n正文[[dhamma]]{{n|注释 **粗**|trigger=①}}结束。\n\n- 一\n- 二";
  402. it('renders html with sidenotes and an endnote list', function () use ($source) {
  403. $html = markupRender($source, Format::Html, ['footnote' => true]);
  404. expect($html)
  405. ->toContain('<h1 id="')
  406. ->toContain('<span class="term-ref term_invalid" data-id="" data-term="dhamma">dhamma</span>')
  407. ->toMatch('/<label for="(sn-[0-9a-f]{8}-1)" class="margin-toggle sidenote-number">①<\/label><input type="checkbox" id="\1" class="margin-toggle"\/>/')
  408. ->toContain('<span class="sidenote"><span>注释 <strong>粗</strong></span></span>')
  409. ->toContain('<ul><li>一</li><li>二</li></ul>')
  410. ->toContain('<div><h1>endnote</h1><p><a name="footnote-1">[1]</a> <span>注释 <strong>粗</strong></span></p></div>');
  411. });
  412. it('omits the endnote list without the footnote option', function () use ($source) {
  413. expect(markupRender($source, Format::Html))->not->toContain('endnote');
  414. });
  415. it('renders text, simple and prompt', function () use ($source) {
  416. expect(markupRender($source, Format::Text))->toBe("标题\n正文dhamma①结束。\n一\n二");
  417. expect(markupRender($source, Format::Simple))->toBe("标题\n正文dhamma结束。\n一\n二");
  418. expect(markupRender($source, Format::Prompt))->toBe("标题\n正文dhamma结束。\n一\n二");
  419. });
  420. it('renders markdown with footnote references', function () use ($source) {
  421. expect(markupRender($source, Format::Markdown, ['footnote' => true]))
  422. ->toBe("# 标题\n\n正文dhamma[^1]结束。\n\n- 一\n- 二\n\n[^1]: 注释 **粗**");
  423. });
  424. it('renders legacy backtick notes as markdown footnotes', function () {
  425. expect(markupRender('a`b`c', Format::Markdown, ['footnote' => true]))->toBe("a[^1]c\n\n[^1]: b");
  426. });
  427. it('prefixes attachment links in markdown', function () {
  428. config(['app.url' => 'https://example.test']);
  429. expect(markupRender('![a](/attachments/x.png)', Format::Markdown))->toBe('![a](https://example.test/attachments/x.png)');
  430. });
  431. it('numbers footnotes per document in renderMany', function () {
  432. $out = app(MarkupService::class)->renderMany(['a' => '{{n|x}}', 'b' => '{{n|y}}'], new RenderOptions(format: Format::Markdown, footnote: true));
  433. expect($out)->toBe(['a' => "[^1]\n\n[^1]: x", 'b' => "[^1]\n\n[^1]: y"]);
  434. });
  435. });
  436. describe('legacy backtick edge cases (from real data)', function () {
  437. it('treats a pair of double backticks as the note boundary', function () {
  438. // 库里的写法:``注释``。v2 的正则把正文算成「`注释」、剩一个反引号;作者本意是整对是边界
  439. $tree = markupTree('婆罗门。``x y``后文');
  440. expect($tree[1])->toMatchArray(['name' => 'note']);
  441. expect($tree[1]['children'][0]['value'])->toBe('x y');
  442. expect($tree[2])->toBe(['type' => 'text', 'value' => '后文']);
  443. });
  444. it('accepts a backtick note whose backticks sit on their own lines', function () {
  445. $tree = markupTree("然后她们(宫女)\n`\n环绕(parikkhipiṃsu)一词用的是复数。\n`\n用屏风围上。");
  446. expect(array_column(markupTemplates($tree), 'name'))->toBe(['note']);
  447. expect(markupRender("她们\n`\n注释\n`\n用屏风", Format::Text))->toBe("她们\n\n用屏风");
  448. });
  449. it('pairs backtick notes across lines like the authors meant', function () {
  450. // 库里的真实写法:两条多行注释。v2 只在单行内配对,配成了「奉为」一条 + 一个跨行代码 span
  451. $tree = markupTree("炬口王` 炬口王 (Okkāka):……\n其音、义皆与巴利语不同。\n`奉为` 奉为 (dahanti):直译为放置。`祖父。");
  452. expect(array_column(markupTemplates($tree), 'name'))->toBe(['note', 'note']);
  453. expect(markupTemplates($tree)[1]['children'][0]['value'])->toBe('奉为 (dahanti):直译为放置。');
  454. expect(markupRender("炬口王`注1\n续`奉为`注2`祖父", Format::Text))->toBe('炬口王奉为祖父');
  455. });
  456. it('does not pair backticks across a blank line', function () {
  457. $tree = markupTree("王` 炬口王\n\n奉为`");
  458. expect(markupTemplates($tree))->toBe([]);
  459. expect(markupText($tree))->toBe('王` 炬口王奉为`');
  460. });
  461. it('does not join a note fence with a following thematic break', function () {
  462. $tree = markupTree("瞿低迦經\n```\n莊春江 譯\n```\n***\n第三");
  463. expect(array_column($tree, 'tag'))->toBe(['p', 'hr', 'p']);
  464. });
  465. });
  466. /*
  467. * 新服务不准回头依赖 v2 的渲染与帮手类(v2 继续用它们,新服务各自独立)。
  468. * 一个类一条:pest-plugin-arch 的数组写法会空过。
  469. */
  470. foreach (['App\Http\Api\MdRender', 'App\Http\Api\TemplateRender', 'App\Http\Api\ChannelApi', 'App\Http\Controllers\CorpusController'] as $legacy) {
  471. arch("App\\Services\\Markup does not use {$legacy}")
  472. ->expect('App\Services\Markup')
  473. ->not->toUse($legacy);
  474. }
  475. describe('emphasis next to CJK punctuation (markdown-cjk-friendly)', function () {
  476. it('renders bold that wraps CJK punctuation', function (string $source, string $html) {
  477. expect(markupRender($source, Format::Html))->toBe("<span>{$html}</span>");
  478. })->with([
  479. '引号在粗体里' => ['从**“tuccha”**的角度', '从<strong>“tuccha”</strong>的角度'],
  480. '括号在粗体里' => ['是**(括号)**吗', '是<strong>(括号)</strong>吗'],
  481. '标点在粗体外' => ['“**粗**”', '“<strong>粗</strong>”'],
  482. '句号紧跟' => ['**粗**。', '<strong>粗</strong>。'],
  483. '斜体' => ['这是*“斜”*体', '这是<em>“斜”</em>体'],
  484. ]);
  485. it('keeps CommonMark behaviour when neither side is CJK', function (string $source, string $html) {
  486. expect(markupRender($source, Format::Html))->toBe("<span>{$html}</span>");
  487. })->with([
  488. ['a **b** c', 'a <strong>b</strong> c'],
  489. ['a**b**c', 'a<strong>b</strong>c'],
  490. ['**ramu** + tano', '<strong>ramu</strong> + tano'],
  491. ['**foo "*bar*" foo**', '<strong>foo &quot;<em>bar</em>&quot; foo</strong>'],
  492. ['*(**foo**)*', '<em>(<strong>foo</strong>)</em>'],
  493. ['a * b * c', 'a * b * c'],
  494. ['a**"foo"**', 'a**&quot;foo&quot;**'],
  495. ['**foo**bar', '<strong>foo</strong>bar'],
  496. ]);
  497. it('only differs from the CommonMark flanking rule when a CJK character is involved', function () {
  498. $commonmark = function (string $before, string $after): array {
  499. $as = preg_match('/^\pZ|\s/u', $after) === 1;
  500. $ap = preg_match('/^[\p{P}\p{S}]/u', $after) === 1;
  501. $bs = preg_match('/^\pZ|\s/u', $before) === 1;
  502. $bp = preg_match('/^[\p{P}\p{S}]/u', $before) === 1;
  503. return [! $as && (! $ap || $bs || $bp), ! $bs && (! $bp || $as || $ap)];
  504. };
  505. $chars = ['a', 'Z', '0', ' ', "\n", '"', '(', ')', '.', ',', '-', 'ā', 'ṃ', '“', '”', ',', '。', '(', ')', '€'];
  506. foreach ($chars as $before) {
  507. foreach ($chars as $after) {
  508. expect(CjkFriendlyEmphasisParser::flanking($before, $after))
  509. ->toBe($commonmark($before, $after), "before={$before} after={$after}");
  510. }
  511. }
  512. });
  513. });
  514. describe('toc', function () {
  515. $source = "{{toc}}\n\n# 一级\n\n## 甲 [[dhamma]]\n\n### 甲一\n\n#### 甲一a\n\n## 乙\n\n正文";
  516. /** 树里的目录节点 */
  517. $toc = function (array $tree): array {
  518. foreach ($tree as $node) {
  519. if (($node['name'] ?? null) === 'toc') {
  520. return $node;
  521. }
  522. foreach ($node['children'] ?? [] as $child) {
  523. if (($child['name'] ?? null) === 'toc') {
  524. return $child;
  525. }
  526. }
  527. }
  528. return [];
  529. };
  530. /** 列表 → [[层级, 链接 href, 文字], …] */
  531. $items = function (array $list, int $depth = 1) use (&$items): array {
  532. $out = [];
  533. foreach ($list['children'] as $li) {
  534. foreach ($li['children'] as $child) {
  535. if ($child['tag'] === 'a') {
  536. $out[] = [$depth, $child['attrs']['href'], markupText($child['children'])];
  537. } elseif ($child['tag'] === 'ul') {
  538. array_push($out, ...$items($child, $depth + 1));
  539. }
  540. }
  541. }
  542. return $out;
  543. };
  544. it('lists h2 and h3 by default, nested, linking to the heading ids', function () use ($source, $toc, $items) {
  545. $tree = markupTree($source);
  546. $node = $toc($tree);
  547. expect($node['props'])->toBe(['title' => '目录']);
  548. $h2 = collect($tree)->where('tag', 'h2')->values();
  549. expect($items($node['children'][0]))->toBe([
  550. [1, '#'.$h2[0]['attrs']['id'], '甲 dhamma'],
  551. [2, '#'.collect($tree)->firstWhere('tag', 'h3')['attrs']['id'], '甲一'],
  552. [1, '#'.$h2[1]['attrs']['id'], '乙'],
  553. ]);
  554. });
  555. it('keeps templates inside heading text', function () use ($source, $toc) {
  556. $link = $toc(markupTree($source))['children'][0]['children'][0]['children'][0];
  557. expect(collect($link['children'])->firstWhere('name', 'term')['props']['word'])->toBe('dhamma');
  558. });
  559. it('skips a hand-written heading right before it', function () use ($toc, $items) {
  560. $tree = markupTree("## 目录\n\n{{toc}}\n\n## 甲\n\n## 乙");
  561. expect(array_column($items($toc($tree)['children'][0]), 2))->toBe(['甲', '乙']);
  562. // 已经有手写标题,不再显示第二个
  563. expect($toc($tree)['props'])->toBe([]);
  564. expect(markupRender("## 目录\n\n{{toc}}\n\n## 甲", Format::Html))->not->toContain('toc-title');
  565. });
  566. it('accepts a level range or a single level', function () use ($source, $toc, $items) {
  567. expect(array_column($items($toc(markupTree(str_replace('{{toc}}', '{{toc|2-4}}', $source)))['children'][0]), 2))
  568. ->toBe(['甲 dhamma', '甲一', '甲一a', '乙']);
  569. expect(array_column($items($toc(markupTree(str_replace('{{toc}}', '{{toc|2}}', $source)))['children'][0]), 2))
  570. ->toBe(['甲 dhamma', '乙']);
  571. });
  572. it('takes a custom title, or none', function () use ($source, $toc) {
  573. expect($toc(markupTree(str_replace('{{toc}}', '{{toc|title=本条目录}}', $source)))['props'])->toBe(['title' => '本条目录']);
  574. expect($toc(markupTree(str_replace('{{toc}}', '{{toc|title=}}', $source)))['props'])->toBe([]);
  575. });
  576. it('renders html, text and markdown', function () use ($source) {
  577. expect(markupRender($source, Format::Html))
  578. ->toMatch('/^<nav class="toc"><div class="toc-title">目录<\/div><ul><li><a href="#(h-[0-9a-f]{8}-2)">甲 .*<\/a><ul><li><a href="#h-[0-9a-f]{8}-3">甲一<\/a><\/li><\/ul><\/li><li><a href="#h-[0-9a-f]{8}-5">乙<\/a><\/li><\/ul><\/nav><h1 id="h-[0-9a-f]{8}-1">一级<\/h1><h2 id="\1">/');
  579. expect(markupRender($source, Format::Text))->toStartWith("目录\n甲 dhamma\n 甲一\n乙\n");
  580. expect(markupRender($source, Format::Markdown))->toMatch('/^目录\n- \[甲 dhamma\]\(#h-[0-9a-f]{8}-2\)\n - \[甲一\]\(#h-[0-9a-f]{8}-3\)\n- \[乙\]\(#h-[0-9a-f]{8}-5\)\n/');
  581. });
  582. it('renders nothing when there are no headings', function () {
  583. expect(markupTree('{{toc}} 正文')[0])->toMatchArray(['name' => 'toc', 'children' => []]);
  584. expect(markupRender('{{toc}}', Format::Html))->toBe('');
  585. });
  586. });