TestMdRender.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\MdRender;
  4. use App\Services\Template\TemplateService;
  5. use App\Tools\Markdown;
  6. use App\Tools\Tools;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Str;
  10. class TestMdRender extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. * php artisan test:md.render term --format=unity --driver=str
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'test:md.render {item?} {--format=html} {--driver=morus}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Command description';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. if (Tools::isStop()) {
  42. return 0;
  43. }
  44. $content = <<<'md'
  45. # 测试
  46. ## 测试
  47. {{note|text=这是一个普通信息提示|type=info}}
  48. 下面是一个警告信息:
  49. {{warning|message=请注意这个重要提示|title=重要}}
  50. 你也可以使用位置参数:
  51. {{note|
  52. 成功完成操作|
  53. success}}
  54. 支持嵌套模板:
  55. {{info|
  56. content=外层信息 {{note|内嵌提示|warning}} 继续外层|
  57. title=嵌套示例}}
  58. **粗体文本** 和 *斜体文本* 也被支持。
  59. md;
  60. $parser = new TemplateService(false);
  61. $render = $parser->parseAndRender($content, 'json');
  62. var_dump($render);
  63. return 0;
  64. Log::info('md render start item='.$this->argument('item'));
  65. $data = [];
  66. $data['bold'] = <<<'md'
  67. **三十位** 经在[中间]六处为**[licchavi]**,在极果为**慧解脱**
  68. md;
  69. $data['sentence'] = <<<'md'
  70. {{168-916-2-9}}
  71. md;
  72. $data['link'] = <<<'md'
  73. aa `[link](wikipali.org/aa.php?view=b&c=d)` bb
  74. md;
  75. $data['term'] = <<<'md'
  76. ## term
  77. [[bhagavantu]]
  78. md;
  79. $data['noteMulti'] = <<<'md'
  80. ## heading
  81. [点击](http://127.0.0.1:3000/my/article/para/168-876?mode=edit&channel=00ae2c48-c204-4082-ae79-79ba2740d506&book=168&par=876)
  82. ----
  83. dfef
  84. ```
  85. bla **content**
  86. {{99-556-8-12}}
  87. bla **content**
  88. ```
  89. md;
  90. $data['note'] = '`bla **bold** _em_ bla`';
  91. $data['noteTpl'] = <<<'md'
  92. {{note|trigger=kacayana|text=bla **bold** _em_ bla}}
  93. md;
  94. $data['noteTpl2'] = <<<'md'
  95. {{note|trigger=kacayana|text={{99-556-8-12}}}}
  96. md;
  97. $data['trigger'] = <<<'md'
  98. ## heading
  99. ddd
  100. - title
  101. content-1
  102. - title-2
  103. content-2
  104. aaa bbb
  105. md;
  106. $data['exercise'] = <<<'md'
  107. {{168-916-10-37}}
  108. {{exercise|1|((168-916-10-37))}}
  109. {{exercise|
  110. id=1|
  111. content={{168-916-10-37}}
  112. }}
  113. {{exercise|
  114. id=2|
  115. content=# ddd}}
  116. md;
  117. $data['article'] = <<<'md'
  118. {{article|
  119. type=article|
  120. id=27ade9ad-2d0c-4f66-b857-e9335252cc08|
  121. title=第一章 戒律概说(Vinaya)|
  122. style=modal}}
  123. md;
  124. $data['footnote'] = <<<'md'
  125. # title
  126. content `note content` `note2 content`
  127. md;
  128. $data['paragraph'] = <<<'md'
  129. # title
  130. content
  131. {{168-916-10-37}}
  132. {{168-916-10-37}}
  133. the end
  134. md;
  135. $data['img'] = <<<'md'
  136. # title
  137. content
  138. ![aaa](/images/aaa.jpg)
  139. the end
  140. md;
  141. $data['empty'] = '';
  142. Markdown::driver($this->option('driver'));
  143. $format = $this->option('format');
  144. if (empty($format)) {
  145. $formats = ['react', 'unity', 'text', 'tex', 'html', 'simple'];
  146. } else {
  147. $formats = [$format];
  148. }
  149. foreach ($formats as $format) {
  150. $this->info("format:{$format}");
  151. foreach ($data as $key => $value) {
  152. $_item = $this->argument('item');
  153. if (! empty($_item) && $key !== $_item) {
  154. continue;
  155. }
  156. $mdRender = new MdRender([
  157. 'format' => $format,
  158. 'footnote' => true,
  159. 'paragraph' => true,
  160. ]);
  161. $output = $mdRender->convert($value, ['00ae2c48-c204-4082-ae79-79ba2740d506']);
  162. echo $output;
  163. }
  164. }
  165. return 0;
  166. }
  167. }