MarkupSyntaxTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 and dict-pref', 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. });
  351. it('renders the ai notice with default title, positional title and markdown message', function () {
  352. $default = markupTemplates(markupTree('{{ai}}', ['lang' => 'zh-Hans']))[0];
  353. expect($default['name'])->toBe('ai');
  354. expect($default['props'])->toBe(['title' => 'AI 生成内容', 'style' => 'block']);
  355. expect(markupTemplates(markupTree('{{ai|机器翻译}}'))[0]['props'])->toBe(['title' => '机器翻译', 'style' => 'block']);
  356. $full = markupTemplates(markupTree('{{ai|title=草稿|message=未经**校对**|style=tag}}'))[0];
  357. expect($full['props'])->toBe(['title' => '草稿', 'style' => 'tag']);
  358. expect(json_encode($full['children'], JSON_UNESCAPED_UNICODE))->toContain('校对');
  359. // 未知 style 按 block
  360. expect(markupTemplates(markupTree('{{ai|style=bogus}}', ['lang' => 'en']))[0]['props'])
  361. ->toBe(['title' => 'AI-generated content', 'style' => 'block']);
  362. });
  363. it('renders the ai notice in html, text and prompt', function () {
  364. // 单段文档整体按行内输出(外层 span);多段时块级模板独占一段不包 <p>
  365. $block = markupRender("前文\n\n{{ai|草稿|未经校对}}", Format::Html);
  366. expect($block)->toContain('<p>前文</p><span class="markup-ai markup-ai-block"')
  367. ->toContain('<span class="markup-ai-title"><svg')
  368. ->toContain('草稿')
  369. ->toContain('<span class="markup-ai-message"><span>未经校对</span></span>');
  370. $tag = markupRender('文字{{ai|草稿|未经校对|style=tag}}', Format::Html);
  371. expect($tag)->toContain('文字<span class="markup-ai markup-ai-tag"')
  372. ->toContain('AI<span class="markup-ai-popup" role="tooltip">');
  373. expect(markupRender('x{{ai|草稿|未经校对}}', Format::Text))->toBe('x[草稿] 未经校对');
  374. expect(markupRender('x{{ai|草稿}}', Format::Prompt))->toBe('x');
  375. });
  376. it('renders video and strips unsafe urls', function () {
  377. expect(markupTree('{{v|https://v.example/a.mp4}}')[0]['props'])
  378. ->toBe(['url' => 'https://v.example/a.mp4', 'title' => '', 'style' => 'modal']);
  379. // 显式给空值时用空值(v2 口径:参数存在就不取默认值)
  380. expect(markupTree('{{v|https://v.example/a.mp4||intro}}')[0]['props'])
  381. ->toBe(['url' => 'https://v.example/a.mp4', 'title' => 'intro', 'style' => '']);
  382. expect(markupTree('{{v|javascript:alert(1)}}')[0]['props']['url'])->toBe('');
  383. });
  384. it('renders exercise and article', function () {
  385. expect(markupTree('{{exercise|id=e1|title=Q1}}', ['channels' => ['c1']])[0]['props'])
  386. ->toBe(['id' => 'e1', 'title' => 'Q1', 'channel' => 'c1']);
  387. $article = markupTree('{{article|type=chapter|book=168|paragraphs=915|title=T}}')[0]['props'];
  388. expect($article)->toMatchArray(['type' => 'chapter', 'id' => '168-915', 'title' => 'T', 'book' => '168']);
  389. expect(markupRender('{{article|type=article|id=x|title=T}}', Format::Text))->toBe('T');
  390. expect(markupRender('{{article|type=article|id=x|title=T}}', Format::Prompt))->toBe('');
  391. });
  392. it('renders nissaya templates and nissaya channel lines', function () {
  393. expect(markupTree('{{nissaya|evaṃ|ဤသို့=thus}}')[0]['props'])
  394. ->toMatchArray(['pali' => 'evaṃ', 'meaning' => ['ဤသို့', 'thus']]);
  395. $lines = markupTree("evaṃ=ဤသို့\nme\$ငါ", ['channelType' => 'nissaya']);
  396. expect(array_column(markupTemplates($lines), 'name'))->toBe(['nissaya', 'nissaya']);
  397. expect(markupTemplates($lines)[1]['props']['pali'])->toBe('me');
  398. });
  399. it('renders nissaya json content', function () {
  400. $json = json_encode([
  401. ['original' => 'evaṃ', 'translation' => 'ဤသို့>thus', 'note' => 'n1'],
  402. ['sn' => [1], 'word' => ['value' => 'me'], 'meaning' => ['value' => 'ငါ']],
  403. ]);
  404. $tree = markupTree($json, ['channelType' => 'nissaya', 'contentType' => 'json']);
  405. expect(array_column(markupTemplates($tree), 'name'))->toBe(['nissaya', 'note', 'nissaya']);
  406. expect(markupTemplates($tree)[0]['props']['meaning'])->toBe(['ဤသို့', 'thus']);
  407. });
  408. it('turns a list item holding only a div into a toggle', function () {
  409. $tree = markupTree("- <div>title</div>\n- plain");
  410. $first = $tree[0]['children'][0]['children'][0];
  411. expect($first)->toMatchArray(['type' => 'tpl', 'name' => 'toggle']);
  412. expect($first['children'][0]['tag'])->toBe('div');
  413. expect($tree[0]['children'][1]['children'][0]['type'])->toBe('text');
  414. });
  415. it('renders a term without a channel as the plain word', function () {
  416. $tree = markupTree('[[dhamma]]');
  417. expect($tree[0])->toMatchArray(['name' => 'term', 'props' => ['word' => 'dhamma', 'parentChannelId' => null, 'parentStudioId' => null]]);
  418. expect($tree[0]['children'])->toBe([['type' => 'text', 'value' => 'dhamma']]);
  419. expect(markupTree('[[dhamma#法]]')[0]['props']['meaning'])->toBe('法');
  420. });
  421. });
  422. describe('output formats', function () {
  423. $source = "# 标题\n\n正文[[dhamma]]{{n|注释 **粗**|trigger=①}}结束。\n\n- 一\n- 二";
  424. it('renders html with sidenotes and an endnote list', function () use ($source) {
  425. $html = markupRender($source, Format::Html, ['footnote' => true]);
  426. expect($html)
  427. ->toContain('<h1 id="')
  428. ->toContain('<span class="term-ref term_invalid" data-id="" data-term="dhamma">dhamma</span>')
  429. ->toMatch('/<label for="(sn-[0-9a-f]{8}-1)" class="margin-toggle sidenote-number">①<\/label><input type="checkbox" id="\1" class="margin-toggle"\/>/')
  430. ->toContain('<span class="sidenote"><span>注释 <strong>粗</strong></span></span>')
  431. ->toContain('<ul><li>一</li><li>二</li></ul>')
  432. ->toContain('<div><h1>endnote</h1><p><a name="footnote-1">[1]</a> <span>注释 <strong>粗</strong></span></p></div>');
  433. });
  434. it('omits the endnote list without the footnote option', function () use ($source) {
  435. expect(markupRender($source, Format::Html))->not->toContain('endnote');
  436. });
  437. it('renders text, simple and prompt', function () use ($source) {
  438. expect(markupRender($source, Format::Text))->toBe("标题\n正文dhamma①结束。\n一\n二");
  439. expect(markupRender($source, Format::Simple))->toBe("标题\n正文dhamma结束。\n一\n二");
  440. expect(markupRender($source, Format::Prompt))->toBe("标题\n正文dhamma结束。\n一\n二");
  441. });
  442. it('renders markdown with footnote references', function () use ($source) {
  443. expect(markupRender($source, Format::Markdown, ['footnote' => true]))
  444. ->toBe("# 标题\n\n正文dhamma[^1]结束。\n\n- 一\n- 二\n\n[^1]: 注释 **粗**");
  445. });
  446. it('renders legacy backtick notes as markdown footnotes', function () {
  447. expect(markupRender('a`b`c', Format::Markdown, ['footnote' => true]))->toBe("a[^1]c\n\n[^1]: b");
  448. });
  449. it('prefixes attachment links in markdown', function () {
  450. config(['app.url' => 'https://example.test']);
  451. expect(markupRender('![a](/attachments/x.png)', Format::Markdown))->toBe('![a](https://example.test/attachments/x.png)');
  452. });
  453. it('numbers footnotes per document in renderMany', function () {
  454. $out = app(MarkupService::class)->renderMany(['a' => '{{n|x}}', 'b' => '{{n|y}}'], new RenderOptions(format: Format::Markdown, footnote: true));
  455. expect($out)->toBe(['a' => "[^1]\n\n[^1]: x", 'b' => "[^1]\n\n[^1]: y"]);
  456. });
  457. });
  458. describe('legacy backtick edge cases (from real data)', function () {
  459. it('treats a pair of double backticks as the note boundary', function () {
  460. // 库里的写法:``注释``。v2 的正则把正文算成「`注释」、剩一个反引号;作者本意是整对是边界
  461. $tree = markupTree('婆罗门。``x y``后文');
  462. expect($tree[1])->toMatchArray(['name' => 'note']);
  463. expect($tree[1]['children'][0]['value'])->toBe('x y');
  464. expect($tree[2])->toBe(['type' => 'text', 'value' => '后文']);
  465. });
  466. it('accepts a backtick note whose backticks sit on their own lines', function () {
  467. $tree = markupTree("然后她们(宫女)\n`\n环绕(parikkhipiṃsu)一词用的是复数。\n`\n用屏风围上。");
  468. expect(array_column(markupTemplates($tree), 'name'))->toBe(['note']);
  469. expect(markupRender("她们\n`\n注释\n`\n用屏风", Format::Text))->toBe("她们\n\n用屏风");
  470. });
  471. it('pairs backtick notes across lines like the authors meant', function () {
  472. // 库里的真实写法:两条多行注释。v2 只在单行内配对,配成了「奉为」一条 + 一个跨行代码 span
  473. $tree = markupTree("炬口王` 炬口王 (Okkāka):……\n其音、义皆与巴利语不同。\n`奉为` 奉为 (dahanti):直译为放置。`祖父。");
  474. expect(array_column(markupTemplates($tree), 'name'))->toBe(['note', 'note']);
  475. expect(markupTemplates($tree)[1]['children'][0]['value'])->toBe('奉为 (dahanti):直译为放置。');
  476. expect(markupRender("炬口王`注1\n续`奉为`注2`祖父", Format::Text))->toBe('炬口王奉为祖父');
  477. });
  478. it('does not pair backticks across a blank line', function () {
  479. $tree = markupTree("王` 炬口王\n\n奉为`");
  480. expect(markupTemplates($tree))->toBe([]);
  481. expect(markupText($tree))->toBe('王` 炬口王奉为`');
  482. });
  483. it('does not join a note fence with a following thematic break', function () {
  484. $tree = markupTree("瞿低迦經\n```\n莊春江 譯\n```\n***\n第三");
  485. expect(array_column($tree, 'tag'))->toBe(['p', 'hr', 'p']);
  486. });
  487. });
  488. /*
  489. * 新服务不准回头依赖 v2 的渲染与帮手类(v2 继续用它们,新服务各自独立)。
  490. * 一个类一条:pest-plugin-arch 的数组写法会空过。
  491. */
  492. foreach (['App\Http\Api\MdRender', 'App\Http\Api\TemplateRender', 'App\Http\Api\ChannelApi', 'App\Http\Controllers\CorpusController'] as $legacy) {
  493. arch("App\\Services\\Markup does not use {$legacy}")
  494. ->expect('App\Services\Markup')
  495. ->not->toUse($legacy);
  496. }
  497. describe('emphasis next to CJK punctuation (markdown-cjk-friendly)', function () {
  498. it('renders bold that wraps CJK punctuation', function (string $source, string $html) {
  499. expect(markupRender($source, Format::Html))->toBe("<span>{$html}</span>");
  500. })->with([
  501. '引号在粗体里' => ['从**“tuccha”**的角度', '从<strong>“tuccha”</strong>的角度'],
  502. '括号在粗体里' => ['是**(括号)**吗', '是<strong>(括号)</strong>吗'],
  503. '标点在粗体外' => ['“**粗**”', '“<strong>粗</strong>”'],
  504. '句号紧跟' => ['**粗**。', '<strong>粗</strong>。'],
  505. '斜体' => ['这是*“斜”*体', '这是<em>“斜”</em>体'],
  506. ]);
  507. it('keeps CommonMark behaviour when neither side is CJK', function (string $source, string $html) {
  508. expect(markupRender($source, Format::Html))->toBe("<span>{$html}</span>");
  509. })->with([
  510. ['a **b** c', 'a <strong>b</strong> c'],
  511. ['a**b**c', 'a<strong>b</strong>c'],
  512. ['**ramu** + tano', '<strong>ramu</strong> + tano'],
  513. ['**foo "*bar*" foo**', '<strong>foo &quot;<em>bar</em>&quot; foo</strong>'],
  514. ['*(**foo**)*', '<em>(<strong>foo</strong>)</em>'],
  515. ['a * b * c', 'a * b * c'],
  516. ['a**"foo"**', 'a**&quot;foo&quot;**'],
  517. ['**foo**bar', '<strong>foo</strong>bar'],
  518. ]);
  519. it('only differs from the CommonMark flanking rule when a CJK character is involved', function () {
  520. $commonmark = function (string $before, string $after): array {
  521. $as = preg_match('/^\pZ|\s/u', $after) === 1;
  522. $ap = preg_match('/^[\p{P}\p{S}]/u', $after) === 1;
  523. $bs = preg_match('/^\pZ|\s/u', $before) === 1;
  524. $bp = preg_match('/^[\p{P}\p{S}]/u', $before) === 1;
  525. return [! $as && (! $ap || $bs || $bp), ! $bs && (! $bp || $as || $ap)];
  526. };
  527. $chars = ['a', 'Z', '0', ' ', "\n", '"', '(', ')', '.', ',', '-', 'ā', 'ṃ', '“', '”', ',', '。', '(', ')', '€'];
  528. foreach ($chars as $before) {
  529. foreach ($chars as $after) {
  530. expect(CjkFriendlyEmphasisParser::flanking($before, $after))
  531. ->toBe($commonmark($before, $after), "before={$before} after={$after}");
  532. }
  533. }
  534. });
  535. });
  536. describe('toc', function () {
  537. $source = "{{toc}}\n\n# 一级\n\n## 甲 [[dhamma]]\n\n### 甲一\n\n#### 甲一a\n\n## 乙\n\n正文";
  538. /** 树里的目录节点 */
  539. $toc = function (array $tree): array {
  540. foreach ($tree as $node) {
  541. if (($node['name'] ?? null) === 'toc') {
  542. return $node;
  543. }
  544. foreach ($node['children'] ?? [] as $child) {
  545. if (($child['name'] ?? null) === 'toc') {
  546. return $child;
  547. }
  548. }
  549. }
  550. return [];
  551. };
  552. /** 列表 → [[层级, 链接 href, 文字], …] */
  553. $items = function (array $list, int $depth = 1) use (&$items): array {
  554. $out = [];
  555. foreach ($list['children'] as $li) {
  556. foreach ($li['children'] as $child) {
  557. if ($child['tag'] === 'a') {
  558. $out[] = [$depth, $child['attrs']['href'], markupText($child['children'])];
  559. } elseif ($child['tag'] === 'ul') {
  560. array_push($out, ...$items($child, $depth + 1));
  561. }
  562. }
  563. }
  564. return $out;
  565. };
  566. it('lists h2 and h3 by default, nested, linking to the heading ids', function () use ($source, $toc, $items) {
  567. $tree = markupTree($source);
  568. $node = $toc($tree);
  569. expect($node['props'])->toBe(['title' => '目录']);
  570. $h2 = collect($tree)->where('tag', 'h2')->values();
  571. expect($items($node['children'][0]))->toBe([
  572. [1, '#'.$h2[0]['attrs']['id'], '甲 dhamma'],
  573. [2, '#'.collect($tree)->firstWhere('tag', 'h3')['attrs']['id'], '甲一'],
  574. [1, '#'.$h2[1]['attrs']['id'], '乙'],
  575. ]);
  576. });
  577. it('keeps templates inside heading text', function () use ($source, $toc) {
  578. $link = $toc(markupTree($source))['children'][0]['children'][0]['children'][0];
  579. expect(collect($link['children'])->firstWhere('name', 'term')['props']['word'])->toBe('dhamma');
  580. });
  581. it('skips a hand-written heading right before it', function () use ($toc, $items) {
  582. $tree = markupTree("## 目录\n\n{{toc}}\n\n## 甲\n\n## 乙");
  583. expect(array_column($items($toc($tree)['children'][0]), 2))->toBe(['甲', '乙']);
  584. // 已经有手写标题,不再显示第二个
  585. expect($toc($tree)['props'])->toBe([]);
  586. expect(markupRender("## 目录\n\n{{toc}}\n\n## 甲", Format::Html))->not->toContain('toc-title');
  587. });
  588. it('accepts a level range or a single level', function () use ($source, $toc, $items) {
  589. expect(array_column($items($toc(markupTree(str_replace('{{toc}}', '{{toc|2-4}}', $source)))['children'][0]), 2))
  590. ->toBe(['甲 dhamma', '甲一', '甲一a', '乙']);
  591. expect(array_column($items($toc(markupTree(str_replace('{{toc}}', '{{toc|2}}', $source)))['children'][0]), 2))
  592. ->toBe(['甲 dhamma', '乙']);
  593. });
  594. it('takes a custom title, or none', function () use ($source, $toc) {
  595. expect($toc(markupTree(str_replace('{{toc}}', '{{toc|title=本条目录}}', $source)))['props'])->toBe(['title' => '本条目录']);
  596. expect($toc(markupTree(str_replace('{{toc}}', '{{toc|title=}}', $source)))['props'])->toBe([]);
  597. });
  598. it('renders html, text and markdown', function () use ($source) {
  599. expect(markupRender($source, Format::Html))
  600. ->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">/');
  601. expect(markupRender($source, Format::Text))->toStartWith("目录\n甲 dhamma\n 甲一\n乙\n");
  602. 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/');
  603. });
  604. it('renders nothing when there are no headings', function () {
  605. expect(markupTree('{{toc}} 正文')[0])->toMatchArray(['name' => 'toc', 'children' => []]);
  606. expect(markupRender('{{toc}}', Format::Html))->toBe('');
  607. });
  608. });